Example 2 – filtering

What happens if we need to create a sub graph from the main graph and filter on person age or relationships? The example code from the second example Scala file graph2 shows how this can be done:

val c1 = graph.vertices.filter { case (id, (name, age)) => age.toLong > 40 }.count
val c2 = graph.edges.filter { case Edge(from, to, property)
=> property == "Father" | property == "Mother" }.count
println( "Vertices count : " + c1 )
println( "Edges count : " + c2 )

Two example counts have been created from the main graph: the first filters person-based vertices on age only, taking those people who are greater than forty years old. Notice that the age value, which was stored as a string, has been converted to a long for the comparison.

The second example filters the edges on the relationship property of Mother or Father. Two count values c1 and c2 are created and printed as the Spark run output, shown as follows:

Vertices count : 4
Edges count : 4
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.206.69