Calculating the degree of the vertex

In this section, we will cover the total degree, then we'll split it into two parts—an in-degree and an out-degree—and we will understand how this works in the code.

For our first test, let's construct the graph that we already know about:

package com.tomekl007.chapter_7

import org.apache.spark.SparkContext
import org.apache.spark.graphx.{Edge, Graph, VertexId}
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.SparkSession
import org.scalatest.FunSuite
import org.scalatest.Matchers._

class CalculateDegreeTest extends FunSuite {
val spark: SparkContext = SparkSession.builder().master("local[2]").getOrCreate().sparkContext

test("should calculate 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")
))

We can get the degrees using the degrees method. The degrees method is returning VertexRDD because degrees is a vertex:

    val graph = Graph(users, relationships)

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

The result is as follows:

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

The preceding code explains that for the 4L instance of VertexId, there is only one relationship because there is a relationship between 2L and 4L.

Then, for the 2L instance of VertexId, there are two, so it is between 1L, 2L and 2L, 4L. For the 1L instance of VertexId, there are two, which are 1L, 2L and 1L, 3L, and for VertexId 3L, there is only one relationship, between 1L and 3L. This way, we can check how our graph is coupled and how many relationships there are. We can find out which vertex is best known by sorting them, so we can see that our test passed in the following screenshot:

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

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