一个简单的联通图好友推荐
尝试联通图算法
package graphx
import org.apache.spark.graphx.{Edge, Graph, VertexId, VertexRDD}
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}
/**
* Created by IBM on 2020/4/27.
*/
object LikeFriends {
def main(args: Array[String]): Unit = {
val conf: SparkConf = new SparkConf().setAppName(this.getClass.getName).setMaster("local[*]")
val sc: SparkContext = new SparkContext(conf)
//点集合
val tuples: List[(Long, String)] = List((1L,"路飞"),(2L,"龙"),(3L,"老沙"),(4L,"索隆"),(5L,"卡普"),(6L,"大妈"),(7,"袁世凯"))
val vex: RDD[(Long, String)] = sc.makeRDD(tuples)
//线集合
val line: List[Edge[Int]] = List(Edge(1,2,0),Edge(1,3,0),Edge(1,4,0),Edge(1,5,0),Edge(1,6,0),Edge(2,5,0),Edge(3,4,0),Edge(4,6,0),Edge(6,7,0))
val unit: RDD[Edge[Int]] = sc.makeRDD(line)
//生成图
val unit1: Graph[String, Int] = Graph(vex,unit)
/*连通图
*connectedComponents,将图模拟出来
*verties:将所有的点和最小点连接
*/
val vertices: VertexRDD[VertexId] = unit1.connectedComponents().vertices
vertices.join(vex).map(x=>(x._2._1,x._2._2)).reduceByKey((a,b)=>a+"know"+b).foreach(println(_))
}
}