Spark Graphx图计算案例实战之aggregateMessages求社交网络中的最大年纪追求者和平均年纪！

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. Use aggregateMessages instead.*`

```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)

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