您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Spark Graphx怎么求社交網(wǎng)絡(luò)中的最大年紀(jì)追求者”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
Spark Graphx提供了mapReduceTriplets來(lái)對(duì)圖進(jìn)行聚合計(jì)算,但是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) }
并舉了一個(gè)簡(jiǎn)單的例子:
* vertex * {{{ * val rawGraph: Graph[_, _] = Graph.textFile("twittergraph") * val inDeg: RDD[(VertexId, Int)] = * rawGraph.aggregateMessages[Int](ctx => ctx.sendToDst(1), _ + _) * }}}
可以看見(jiàn)能夠進(jìn)行消息傳遞和聚合操作。
案例實(shí)戰(zhàn):求社交網(wǎng)絡(luò)中的年紀(jì)最大的追求者和追求者的平均年齡:
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((_))
很好很強(qiáng)大??!
結(jié)果如下:
聚合操作
**********************************************************
找出年紀(jì)最大的追求者:
(4,(Bob,27))
(1,(David,42))
(6,(Charlie,65))
(2,(Charlie,65))
(3,(Ed,55))
**********************************************************
找出追求者的平均年紀(jì):
(4,27.0)
(1,34.5)
(6,60.0)
(2,60.0)
(3,55.0)
**********************************************************
“Spark Graphx怎么求社交網(wǎng)絡(luò)中的最大年紀(jì)追求者”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。