Skip to content

Lesson 3 Pattern Matching

Kevin Gómez edited this page Sep 3, 2020 · 15 revisions

Have a look at the Wiki pages of Gradoop - Query for further details about this operator. There are several pre-defined aggregations available.

Task 1 - Friendship Graph

  • Find all persons that know "John Jones".
Solution
  graph = graph.query(
    // add query string here
    "MATCH (p1:person)-[:knows]->(p2:person) WHERE p2.firstName=\"John\" AND p2.lastName=\"Jones\""
  ).reduce(new ReduceCombination<>());

How many persons know him? Answer: 2

Task 2 -

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 -

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