Spark Graph的outerJoinVertices操作实战!

Spark Graph的outerJoinVertices操作实战!


一、outerJoinVertices是什么?

图的vertices进行join操作,就要用到outerJoinVertices。

  /**
   * Joins the vertices with entries in the `table` RDD and merges the results using `mapFunc`.
   * The input table should contain at most one entry for each vertex.  If no entry in `other` is
   * provided for a particular vertex in the graph, the map function receives `None`.
   **/

其中mapFunc是用来对值进行操作的。

如果第二个不存在,则返回none,也就是说,跟LeftOuterJoin操作一样。

二、实战操作

   //设置运行环境
    val conf = new SparkConf().setAppName("SNSAnalysisGraphX").setMaster("local[4]")
    val sc = new SparkContext(conf)

    //创建点RDD
    val usersVertices: RDD[(VertexId, (String, String))] = sc.parallelize(Array(
      (1L, ("Spark", "scala")), (2L, ("Hadoop", "java")),
      (3L, ("Kafka", "scala")), (4L, ("Zookeeper", "Java "))))
    //创建边RDD
    val usersEdges: RDD[Edge[String]] = sc.parallelize(Array(
      Edge(2L, 1L, "study"), Edge(3L, 2L, "train"),
      Edge(1L, 2L, "exercise"), Edge(4L, 1L, "None")))

    val salaryVertices :RDD[(VertexId,(String,Long))] =sc.parallelize(Array(
      (1L,("Spark",30L)),(2L, ("Hadoop", 15L)),
      (3L, ("Kafka", 10L)), (5L, ("parameter server", 40L))
    ))
    val salaryEdges: RDD[Edge[String]] = sc.parallelize(Array(
      Edge(2L, 1L, "study"), Edge(3L, 2L, "train"),
      Edge(1L, 2L, "exercise"), Edge(5L, 1L, "None")))

    //构造Graph
    val graph = Graph(usersVertices, usersEdges)
    val graph1 = Graph(salaryVertices, salaryEdges)
    //outerJoinVertices操作,
    val joinGraph = graph.outerJoinVertices(graph1.vertices) { (id, attr, deps) =>
      deps match {
        case Some(deps) => deps
        case None => 0 
      }
    }

    joinGraph.vertices.collect.foreach(println)
    sc.stop()

三、运行结果

(4,0)
(1,(Spark,30))
(2,(Hadoop,15))
(3,(Kafka,10))

可以看见,第一个graph的4个顶点都在,而且1/2/3内容都换成了第二个的,但是4顶点,因为第二个图不存在,内容为0,而第二个图的第5顶点被过滤掉了!

完成!

欢迎加入DT大数据梦工厂王家林老师蘑菇云行动!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值