其他分享
首页 > 其他分享> > Spark GraphX 应用示例

Spark GraphX 应用示例

作者:互联网

构建用户合作关系属性图

        顶点属性

                用户名

                职业

        边属性

                合作关系

 

import org.apache.spark.graphx.{Edge, Graph}
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.SparkSession

object GraphDemo2 {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder().appName("sparkGraph")
      .master("local[*]").getOrCreate()
    val sc = spark.sparkContext
    //没注释的是通过内存创建rdd
//还有一种是读取本地文件
//val graph2: Graph[Int, Int] = GraphLoader.edgeListFile(sc,"in/graph.txt")
    val users: RDD[(Long, (String, String))] = sc.makeRDD(
      Array(
        (3L, ("rxin", "student")),
        (7L, ("jgonzal", "postdoc")),
        (5L, ("franklin", "professor")),
        (2L, ("istocia", "professor"))
      )
    )
//创建边集合
    val relations: RDD[Edge[String]] = sc.makeRDD(
      Array(
      Edge(3L, 7L, "Collaborator"),
      Edge(5L, 3L, "Advisor"),
      Edge(2L, 5L, "Colleague"),
      Edge(5L, 7L, "PI")
    ))
//将得到的顶点rdd和边rdd放入到Graph中
    val graph: Graph[(String, String), String] = Graph(users,relations)
   // graph.triplets.foreach(println)//打印最完整的关系
   // graph.vertices.foreach(println)//打印顶点集合
    graph.edges.foreach(println)//打印边集合
  }
}

标签:String,val,示例,Graph,Edge,graph,spark,Spark,GraphX
来源: https://blog.csdn.net/m0_46620354/article/details/121982392