Skip to content

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.

Test Data Schema

LDBC Schema

All labels start with a lowercase letter in our dataset, so please use 'person' instead of 'Person' in your tasks.

Task 1 - Schema graph

  • 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())
    )
  );

Task 2 - Attributed grouping

  • 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 and university nodes and knows and studyAt 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())
    )
  );

Task 3 - User-defined key function

  • 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 and knows 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;
  }

 ...
}
Clone this wiki locally