The out-degree

The out-degree explains how many vertices are going out. This time, we'll be calculating the sources of our edges, relationships, and not destinations, like we did in the in-degree method.

To get the out-degree, we will use the following code:

val degrees = graph.outDegrees.collect().toList

The outDegrees method contains both RDD and VertexRDD, which we have collected to a list using the collect and toList methods.

Here, VertexId 1L should have two outbound vertices because there is a relationship between 1L, 2L and 1L, 3L:

  test("should calculate out-degree of vertices") {
//given
val users: RDD[(VertexId, (String))] =
spark.parallelize(Array(
(1L, "a"),
(2L, "b"),
(3L, "c"),
(4L, "d")
))


val relationships =
spark.parallelize(Array(
Edge(1L, 2L, "friend"),
Edge(1L, 3L, "friend"),
Edge(2L, 4L, "wife")
))

val graph = Graph(users, relationships)

//when
val degrees = graph.outDegrees.collect().toList

//then
degrees should contain theSameElementsAs List(
(1L, 2L),
(2L, 1L)
)
}

}

Also, VertexId 2L should have one outbound vertex as there is a relationship between 2L and 4L and not the other way around, as shown in the preceding code.

We will run this test and get the following output:

..................Content has been hidden....................

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