1
+ package graphx
2
+
3
+ import org .apache .spark ._
4
+ import org .apache .spark .graphx .{Graph , _ }
5
+
6
+ object InputDataFlow {
7
+
8
+ def parseNames (line : String ): Option [(VertexId , String )] = {
9
+ val fields = line.split('\t ' )
10
+ if (fields.length > 1 )
11
+ Some (fields(0 ).trim().toLong, fields(1 ))
12
+ else None
13
+ }
14
+
15
+ def makeEdges (line : String ) : List [Edge [Int ]] = {
16
+ import scala .collection .mutable .ListBuffer
17
+ var edges = new ListBuffer [Edge [Int ]]()
18
+ val fields = line.split(" " )
19
+ val origin = fields(0 )
20
+ (1 until fields.length)
21
+ .foreach { p => edges += Edge (origin.toLong, fields(p).toLong, 0 ) }
22
+ edges.toList
23
+ }
24
+
25
+ }
26
+
27
+ class GraphX (sc : SparkContext ) {
28
+ private def verts = sc.textFile(USER_NAMES_FILE ).flatMap(InputDataFlow .parseNames)
29
+
30
+ private def edges = sc.textFile(USER_GRAPH_FILE ).flatMap(InputDataFlow .makeEdges)
31
+
32
+ private def graph = Graph (verts, edges).cache()
33
+
34
+
35
+ def getMostConnectedUsers (amount: Int ): Array [(VertexId , (PartitionID , String ))] = {
36
+ graph.degrees.join(verts)
37
+ .sortBy( {case ( (_, (userName, _))) => userName }, ascending= false ).take(amount)
38
+ }
39
+
40
+ private def getBfs (root: VertexId ) = {
41
+ val initialGraph = graph.mapVertices((id, _) => if (id == root) 0.0 else Double .PositiveInfinity )
42
+
43
+ val bfs = initialGraph.pregel(Double .PositiveInfinity , 10 )(
44
+ (_, attr, msg) => math.min(attr, msg),
45
+ triplet => {
46
+ if (triplet.srcAttr != Double .PositiveInfinity ) {
47
+ Iterator ((triplet.dstId, triplet.srcAttr+ 1 ))
48
+ } else {
49
+ Iterator .empty
50
+ }
51
+ },
52
+ (a,b) => math.min(a,b)).cache()
53
+ bfs
54
+ }
55
+
56
+ def degreeOfSeparationSingleUser (root: VertexId ) = {
57
+ getBfs(root).vertices.join(verts).take(100 )
58
+ }
59
+
60
+ def degreeOfSeparationTwoUser (firstUser: VertexId , secondUser: VertexId ) = {
61
+ getBfs(firstUser)
62
+ .vertices
63
+ .filter{case (vertexId, _) => vertexId == secondUser}
64
+ .collect
65
+ }
66
+ }
0 commit comments