-
Notifications
You must be signed in to change notification settings - Fork 0
Tutorial Instructions
Christopher Rost edited this page Sep 3, 2020
·
5 revisions
Here you will find the tasks of the Gradoop tutorial.
Lessons |
---|
Subgraph |
Grouping |
Pattern Matching |
Complex social network analysis |
- Create a subgraph that contains ‘person’ vertices and ‘knows’ edges only.
Solution
Use the ByLabel
class to filter for vertices/edges of a specific label.
graph = graph.subgraph(new ByLabel<>("person"), new ByLabel<>("knows"));
- Get universities and persons that used the browser Firefox.
- Include all relationships between these vertices.
Some help
- Use the function
.vertexInducedSubgraph()
to only filter for vertices and get all edges between them. - Use the
ByLabel
orLabelIsIn
classes to filter for vertices/edges of a specific label. - Use the
ByProperty
class to filter for elements with a specific property and optional a value. - Use the
And
andOr
classes to build boolean relations.
Solution
graph = graph.vertexInducedSubgraph(
new Or<>(
new ByLabel<>("university"),
new And<>(new ByLabel<>("person"), new ByProperty<>("browserUsed", PropertyValue.create("Firefox")))));
- Some edge types have a
creationDate
property. Get all edges that contain this property and are crated in the period[2012-01-01 00:00 , 2012-01-05 00:00)
by using a user-defined edge filter function. - Only the source and target vertices of these edges should be included.
- Optional: Use parameters (by adding a constructor) to define the lower and upper bound for the condition.
Some help
- Use the function
.edgeInducedSubgraph()
to only filter for edges and get all connected vertices. - You can check the availability of a property by
edge.hasProperty("myProperty")
. - You get the value of a propety by calling
edge.getPropertyValue("myProperty").get{dataType}()
Solution
This is the solution of the task with the optional subtask included.
graph = graph.edgeInducedSubgraph(
new CreatedInPeriod<>(LocalDateTime.of(2012, 1, 1, 0, 0), LocalDateTime.of(2012, 1, 5, 0, 0)));
private static class CreatedInPeriod<E extends Edge> implements FilterFunction<E> {
private LocalDateTime lowerBound, upperBound;
public CreatedInPeriod(LocalDateTime start, LocalDateTime end) {
this.lowerBound = start;
this.upperBound = end;
}
@Override
public boolean filter(E edge) throws Exception {
if (edge.hasProperty("creationDate")) {
LocalDateTime creationDate = edge.getPropertyValue("creationDate").getDateTime();
return (creationDate.isEqual(lowerBound) || creationDate.isAfter(lowerBound)) &&
creationDate.isBefore(upperBound);
}
return false;
}
}
Have a look at the Wiki pages of Gradoop for further details about this operator. There are several pre-defined aggregations available.
- 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;
}
...
}