spark的outerjoinvertices简介

一、outerjoinvertices的介绍

graphx中的基本结构,其中有vertex 和edge vertex:包括点和属性,也就是类似于(id, attr) edge:包括原点, 目标点, 边属性,类似于(src, dst, attr)

outerjoinvertices是对顶点进行的操作,所以不涉及边

outerjoinvertices:

这个操作会把关联上的定点的属性给重新赋值,
所以这样join的话就有点leftjoin的感觉

  def outerJoinVertices[U: ClassTag, VD2: ClassTag](other: RDD[(VertexId, U)])
      (mapFunc: (VertexId, VD, Option[U]) => VD2)(implicit eq: VD =:= VD2 = null)
    : Graph[VD2, ED]

mapFunc是用来对值进行操作的。
如果第二个不存在,则返回none,也就是说,跟LeftOuterJoin操作一样。

二、例子

1)代码

def main(args: Array[String]): Unit = {
    //设置运行环境
    val conf = new SparkConf().setAppName("OuterJoinVerticesJob").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()
  }

2)运行结果

(4,0)
(1,(Spark,30))
(2,(Hadoop,15))
(3,(Kafka,10))
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值