Spark Graphx怎么求社交网络中的最大年纪追求者

发布时间:2022-01-14 17:23:32 作者:iii
来源:亿速云 阅读:111

本篇内容介绍了“Spark Graphx怎么求社交网络中的最大年纪追求者”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

Spark Graphx提供了mapReduceTriplets来对图进行聚合计算,但是1.2以后不再推荐使用,源代码如下:

@deprecated("use aggregateMessages", "1.2.0")
def mapReduceTriplets[A: ClassTag](
    mapFunc: EdgeTriplet[VD, ED] => Iterator[(VertexId, A)],
    reduceFunc: (A, A) => A,
    activeSetOpt: Option[(VertexRDD[_], EdgeDirection)] = None)
  : VertexRDD[A]
* Aggregates values from the neighboring edges and vertices of each vertex.  The user supplied
* `mapFunc` function is invoked on each edge of the graph, generating 0 or more "messages" to be
* "sent" to either vertex in the edge.  The `reduceFunc` is then used to combine the output of
* the map phase destined to each vertex.
*
* This function is deprecated in 1.2.0 because of SPARK-3936. 

*

推荐使用的是aggregateMessages:

def aggregateMessages[A: ClassTag](
    sendMsg: EdgeContext[VD, ED, A] => Unit,
    mergeMsg: (A, A) => A,
    tripletFields: TripletFields = TripletFields.All)
  : VertexRDD[A] = {
  aggregateMessagesWithActiveSet(sendMsg, mergeMsg, tripletFields, None)
}

并举了一个简单的例子:

* vertex
* {{{
* val rawGraph: Graph[_, _] = Graph.textFile("twittergraph")
* val inDeg: RDD[(VertexId, Int)] =
*   rawGraph.aggregateMessages[Int](ctx => ctx.sendToDst(1), _ + _)
* }}}

可以看见能够进行消息传递和聚合操作。

案例实战:求社交网络中的年纪最大的追求者和追求者的平均年龄:

val oldestFollower: VertexRDD[(String,Int)]=userGraph.aggregateMessages[(String, Int)](
  triplet => {
      triplet.sendToDst(triplet.srcAttr.name, triplet.srcAttr.age)
  },
  (a, b) => if (a._2 > b._2) a else b
  )
oldestFollower.collect.foreach(println(_))
averageAge: VertexRDD[] = userGraph.aggregateMessages[()](
  triplet => {
    triplet.sendToDst(triplet.srcAttr.age)
  }(ab) => ((a._1 + b._1)(a._2 + b._2))
).mapValues((idp) => p._2 / p._1)
averageAge.collect().foreach((_))

很好很强大啊!

结果如下:

聚合操作

**********************************************************

找出年纪最大的追求者:

(4,(Bob,27))

(1,(David,42))

(6,(Charlie,65))

(2,(Charlie,65))

(3,(Ed,55))

**********************************************************

找出追求者的平均年纪:

(4,27.0)

(1,34.5)

(6,60.0)

(2,60.0)

(3,55.0)

**********************************************************

“Spark Graphx怎么求社交网络中的最大年纪追求者”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. 1.spark简介
  2. Spark 生态系统组件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spark graphx

上一篇:企业上ERP的五大原因是什么

下一篇:springboot整合quartz定时任务框架的方法是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》