Spark GraphX 聚合操作

package Spark_GraphX

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.graphx._
import org.apache.spark.graphx.util.GraphGenerators

/**
  * 计算每一个用户的追随者数量和追随者的平均年龄
  */
object Graphx_聚合操作 {
  def main(args: Array[String]): Unit = {
      val conf=new SparkConf().setAppName("聚合操作").setMaster("local[2]")
      val sc=new SparkContext(conf)
    //随机生成具有100个顶点的图,图的结构也随机
    val graph:Graph[Double,Int]=GraphGenerators.logNormalGraph(sc,numVertices =100).mapVertices((id,_)=>id.toDouble)
    graph.vertices.foreach(println)
    graph.edges.foreach(println)
    val olderFollowers:VertexRDD[(Int,Double)]=graph.aggregateMessages[(Int,Double)](
      triplet=>{
        if(triplet.srcAttr>triplet.dstAttr){    //如果源顶点的属性大于目的顶点的属性
          triplet.sendToDst(1,triplet.srcAttr)   //源-->目的 发送个数1 和源顶点的属性
        }
      },
    (a,b)=>(a._1+b._1,a._2+b._2)     //累加统计结果
    )
    olderFollowers.values.foreach(println)
    val avgAgeOfOlderFollowers:VertexRDD[Double]=olderFollowers.mapValues((id,value)=>value match {case (count,totalAge)=>totalAge/count})
     println("*********************")
      avgAgeOfOlderFollowers.foreach(println)
  }

}
(34,34.0)
(19,19.0)
(52,52.0)
(39,39.0)
(96,96.0)
(81,81.0)
(4,4.0)
(71,71.0)
(16,16.0)
(55,55.0)
(82,82.0)
(29,29.0)
(66,66.0)
(79,79.0)
(28,28.0)
(65,65.0)
(54,54.0)
(11,11.0)
(40,40.0)
(23,23.0)
(6,6.0)
(67,67.0)
(8,8.0)
(69,69.0)
(86,86.0)
(3,3.0)
(58,58.0)
(7,7.0)
(44,44.0)
(85,85.0)
(88,88.0)
(91,91.0)
(60,60.0)
(31,31.0)
(26,26.0)
(87,87.0)
(68,68.0)
(5,5.0)
(2,2.0)
Edge(50,1,1)
Edge(0,58,1)
Edge(50,25,1)
Edge(1,4,1)
Edge(51,0,1)
Edge(1,5,1)
Edge(51,1,1)
Edge(1,6,1)
Edge(51,2,1)
Edge(1,10,1)
Edge(1,27,1)
Edge(1,27,1)
Edge(1,29,1)
Edge(1,33,1)
Edge(1,34,1)
Edge(1,37,1)
Edge(1,39,1)
Edge(1,42,1)
Edge(51,8,1)
Edge(1,45,1)
Edge(51,9,1)
Edge(1,46,1)
Edge(51,10,1)
Edge(1,47,1)
Edge(51,13,1)
Edge(1,48,1)
Edge(51,13,1)
Edge(1,48,1)
Edge(51,14,1)
Edge(5,5,1)
Edge(5,10,1)
Edge(5,12,1)
Edge(5,13,1)
Edge(5,13,1)
Edge(5,15,1)
Edge(5,17,1)
Edge(5,17,1)
Edge(5,17,1)
Edge(5,17,1)
Edge(5,20,1)
Edge(5,20,1)
Edge(5,22,1)
...........................     省略
...........................     省略
(38,2568.0)
(51,2920.0)
(30,2155.0)
(52,3016.0)
(2,196.0)
(29,2010.0)
(50,2820.0)
(8,736.0)
(40,2247.0)
(17,1567.0)
(8,675.0)
(16,1329.0)
(30,2422.0)
(40,2713.0)
(43,2699.0)
(20,1415.0)
(10,873.0)
(15,1383.0)
(20,1663.0)
(39,2124.0)
(1,99.0)
(39,2440.0)
(37,2487.0)
(54,3089.0)
(13,1080.0)
(14,1172.0)
(21,1615.0)
(30,2079.0)
(37,2555.0)
(42,2832.0)
(12,1036.0)
(13,1118.0)
(26,1899.0)
(11,1067.0)
(61,3382.0)
(19,1675.0)
(6,555.0)
(7,667.0)
(12,994.0)
(14,1204.0)
(13,1220.0)
(17,1457.0)
(46,2838.0)
(45,2699.0)
(53,2946.0)
(63,3344.0)
(19,1433.0)
(42,2806.0)
(22,1582.0)
(51,3087.0)
(16,1394.0)
(10,926.0)
(47,2411.0)
(27,2185.0)
(53,3093.0)
(33,2355.0)
(8,734.0)
(10,930.0)
(6,572.0)
(19,1476.0)
(33,1839.0)
(34,2058.0)
(6,573.0)
(10,837.0)
(52,2814.0)
(59,3208.0)
*********************
(13,57.254901960784316)
(19,58.0)
(39,69.3103448275862)
(81,92.0)
(71,84.375)
(55,80.73333333333333)
(29,62.76744186046512)
(79,87.3)
(65,83.15)
(11,54.46153846153846)
(35,67.21621621621621)
(57,83.07692307692308)
(34,67.57894736842105)
(52,71.83333333333333)
(96,98.0)
(4,56.4)
(16,56.175)
(82,92.17647058823529)
(66,83.0625)
(28,67.825)
(54,70.75)
(80,92.2)
(98,99.0)
(30,62.56410256410256)
(14,57.2037037037037)
(50,83.71428571428571)
(36,69.3)
(24,67.42857142857143)
(64,86.0)
(92,97.0)
(74,88.15789473684211)
(90,95.28571428571429)
(72,86.0)
(70,85.70588235294117)
(18,59.977777777777774)
(12,53.07936507936508)
(38,66.80952380952381)
(20,60.529411764705884)
(78,90.88888888888889)
(10,54.36206896551724)
(94,96.85714285714286)
(84,94.85714285714286)
(56,79.47826086956522)
(76,88.25)
(22,55.484848484848484)
(46,75.97222222222223)
(48,70.38095238095238)
(32,63.86363636363637)
(0,47.06349206349206)
(62,88.75)
(42,70.54166666666667)
(40,71.09677419354838)
(6,55.21875)
(8,54.638297872340424)
(86,92.6)
(58,80.92592592592592)
(44,71.36363636363636)
(88,93.0)
(60,77.6842105263158)
(26,60.529411764705884)
(68,83.7)
(2,54.3728813559322)
(51,76.9047619047619)
(37,69.05405405405405)
(75,86.33333333333333)
(45,73.03846153846153)
(1,55.442622950819676)
(89,92.5)
(63,82.83333333333333)
(83,93.84615384615384)
(17,61.69565217391305)
(9,55.58490566037736)
(49,75.42105263157895)
(43,71.9090909090909)
(41,74.18518518518519)
(61,78.62962962962963)
(15,52.0)
(21,58.244897959183675)
(47,71.15384615384616)
(77,88.25)
(53,71.9090909090909)
(25,62.40384615384615)
(95,97.7)
(59,85.57142857142857)
(73,91.58333333333333)
(27,65.77142857142857)
(93,96.14285714285714)
(33,61.8)
(23,58.84)
(67,85.14285714285714)
(69,87.125)
(3,51.297872340425535)
(7,58.35849056603774)
(85,91.75)
(91,95.33333333333333)
(31,55.72727272727273)
(87,95.5)
(5,54.11538461538461)
原文地址:https://www.cnblogs.com/soyo/p/7890935.html