Spark Graphx图的算子(属性算子,结构算子,join算子,计算用户粉丝数量,joinVertices和outerJoinVertices的区别)

本文介绍了Spark GraphX中的图算子,包括属性算子如mapVertices和mapEdges,结构算子如reverse和subgraph,以及Join算子joinVertices和outerJoinVertices。重点讨论了joinVertices与outerJoinVertices的区别,强调了它们在处理顶点属性更新时的不同策略。
摘要由CSDN通过智能技术生成

一.图的算子

1.属性算子

类似于RDD的map操作

class Graph[VD, ED] {
   
  def mapVertices[VD2](map: (VertexId, VD) => VD2): Graph[VD2, ED]
  def mapEdges[ED2](map: Edge[ED] => ED2): Graph[VD, ED2]
  def mapTriplets[ED2](map: EdgeTriplet[VD, ED] => ED2): Graph[VD, ED2]
}

(1)mapVertices

遍历所有的顶点(改变的是顶点的属性)

//生成新的graph:(vertextId,(name,age))=>(vartextId,(vartextId,name))
//方法一
scala> val t1_graph=userCallGraph.mapVertices{
   case(vertextId,(name,age))=>(vertextId,name)}
t1_graph: org.apache.spark.graphx.Graph[(org.apache.spark.graphx.VertexId, String),Int] = org.apache.spark.graphx.impl.GraphImpl@527afebf

scala> t1_graph.vertices.collect.foreach(println)
(4,(4,David))
(1,(1,Alice))
(6,(6,Fran))
(3,(3,Charlie))
(5,(5,Ed))
(2,(2,Bob))

//方法二
scala> val t2_graph=userCallGraph.mapVertices((id,attr)=>(id,attr._1))
t2_graph: org.apache.spark.graphx.Graph[(org.apache.spark.graphx.VertexId, String),Int] = org.apache.spark.graphx.impl.GraphImpl@7c1958de

scala> t2_graph.vertices.collect.foreach(println)
(4,(4,David))
(1,(1,Alice))
(6,(6,Fran))
(3,(3,Charlie))
(5,(5,Ed))
(2,(2,Bob))

(2)mapEdges

遍历所有的边(改变的是边的属性)

//生成新的graph((scrId,srcAttr),(dstId,dstAttr),attr)=>(scrId,dstId,attr),生成新的图
scala> val t3_graph=userCallGraph.mapEdges(e=>Edge(e.srcId,e.dstId,e.attr*7.0))
t3_graph: org.apache.spark.graphx.Graph[(String, Int),org.apache.spark.graphx.Edge[Double]] = org.apache.spark.graphx.impl.GraphImpl@4777360f


scala> t3_graph.edges.collect.foreach(println)
Edge(2,1,Edge(2,1,49.0))
Edge(2,4,Edge(2,4,14.0))
Edge(3,2,Edge(3,2,28.0))
Edge(3,6,Edge(3,6,21.0))
Edge(4,1,Edge(4,1,7.0))
Edge(5,2,Edge(5,2,14.0))
Edge(5,3,Edge(5,3,56.0))
Edge(5,6,Edge(5,6,21.0))

(3)mapTriplets

遍历所有的三元组(改变的是第三个元素)

//将srcId+10,attr*10生成新的图
scala>  val t3_graph=userCallGraph.mapTriplets(x=>(x.srcId+10,x.attr*10))
t3_graph: org.apache.spark.graphx.Graph[(String, Int),(Long, Int)] = org.apache.spark.graphx.impl.GraphImpl@4f88d48

scala> t3_graph.triplets.collect.foreach(println)
((2,(Bob,27)),(1,(Alice,28)),(12,70))
((2,(Bob,27)),(4,(David,42)),(12,20))
((3,(Charlie,65)),(2,(Bob,27)),
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值