-
Notifications
You must be signed in to change notification settings - Fork 0
Lesson 2 The Grouping operator
Christopher Rost edited this page Sep 4, 2020
·
5 revisions
Have a look at the Wiki pages of Gradoop for further details about this operator. There are several pre-defined aggregations available.
All labels start with a lowercase letter in our dataset, so please use 'person' instead of 'Person' in your tasks.
- Create a schema graph by grouping vertices and edges by the label.
- How many vertices and edges exist per label?
Some help
- The class
GroupingKeys
offers pre-defined key functions.
Solution
graph = graph.callForGraph(
new KeyedGrouping<>(
Arrays.asList(GroupingKeys.label()),
Arrays.asList(new Count()),
Arrays.asList(GroupingKeys.label()),
Arrays.asList(new Count())
)
);
- How many males and females are there? What is the average age per gender?
- How many males know women and vice versa?
- How many male and female people study at universities per class year?
You already have a subgraph with
person
anduniversity
nodes andknows
andstudyAt
edges.
All person vertices have now an additional property
age
as Integer.
Some help
- Use
GroupingKeys.property("myProperty")
to group on a property. - Use the aggregate
new AverageProperty("myProperty")
to calculate the average of a numerical value.
Solution
graph = graph.callForGraph(
new KeyedGrouping<>(
Arrays.asList(GroupingKeys.label(), GroupingKeys.property("gender")),
Arrays.asList(new Count(), new AverageProperty("age")),
Arrays.asList(GroupingKeys.label(), GroupingKeys.property("classYear")),
Arrays.asList(new Count())
)
);
- How many people are born on the same weekday (Mon – Sun)? You have to create a UDF that extracts the day from the property
birthday
. - How old is the youngest and oldest person in the group?
- How many know each other from these groups?
You already have a subgraph with
person
nodes andknows
edges.
All person vertices have now an additional property
age
as Integer.
Some help
- Use the aggregate
new MinProperty("myProperty")
to calculate the minimum of a numerical value. - Use the aggregate
new MaxProperty("myProperty")
to calculate the maximum of a numerical value. - You can check the availability of a property by
vertex.hasProperty("myProperty")
. - You get the value of a propety by calling
vertex.getPropertyValue("myProperty").get{dataType}()
- The Class
LocalDate
offers a function.getDayOfWeek().name()
to get the name of the weekday as string.
Solution
graph = graph.callForGraph(
new KeyedGrouping<>(
Arrays.asList(GroupingKeys.label(), new GetYearOfDate<>()),
Arrays.asList(new Count(), new MinProperty("age"), new MaxProperty("age")),
Arrays.asList(GroupingKeys.label()),
Arrays.asList(new Count())
)
);
private static class GetYearOfDate<V extends Vertex> implements KeyFunction<V, String> {
@Override
public String getKey(V vertex) {
String dayOfWeek = "unknown";
if (vertex.hasProperty("birthday")) {
dayOfWeek = vertex.getPropertyValue("birthday").getDate().getDayOfWeek().name();
}
return dayOfWeek;
}
...
}