Spark圖計算例項
阿新 • • 發佈:2018-12-21
圖計算例項
package cn.graphx import org.apache.spark.graphx.{Edge, Graph} import org.apache.spark.{SparkConf, SparkContext} /** * 圖計算 * 構建點,邊 */ object HelloGraphx { def main(args: Array[String]) = { // SparkContext val sparkConf = new SparkConf() .setAppName("helloGraphx") .setMaster("local[*]") val sc = new SparkContext(sparkConf) // 構建點RDD[(Long, VD)] val verticesRDD = sc.makeRDD(Seq( (1L, "糖糖"), (2L, "果凍"), (133L, "豆豆"), (138L, "金金") )) // 構建邊RDD[Edge[ED]] val edgesRDD = sc.makeRDD(Seq( Edge(1, 133, 0), Edge(1, 138, 0), Edge(2, 133, 0) )) // Graph = 點RDD + 邊RDD val graph = Graph(verticesRDD, edgesRDD) /** * 呼叫連通圖演算法後,去頂點資料: * 得到的結果就是一個對偶元組,元組的第一個值就是該圖中的點的ID, 第二個元素就是這個點所處的聯通圖分支中最小的頂點ID */ val cc = graph.connectedComponents().vertices cc.map(tp => (tp._2, tp._1.toString)).reduceByKey((a, b) => a.concat("\t "+b)).foreach(println) sc.stop() } }