Spark高級算子練習(一)aggregate

轉載作者:Allen-gao

轉載地址:https://blog.csdn.net/sonicgyq_gyq/article/details/79169229

aggregate講解地址:https://blog.csdn.net/tolcf/article/details/51900440

關鍵:

  1. pairRDD.aggregateByKey(0)(_ + _ , _ + _).collect與pairRDD.reduceByKey( _ + _).collect,  
  2.     這兩個方法執行結果是一樣的,實際上底層都是調用的同一個方法:combineByKey 


正文

[plain] view plain copy
  1. package cn.allengao.exercise  
  2.   
  3. import org.apache.spark.{SparkConf, SparkContext}  
  4.   
  5. object SparkRDDTest2 {  
  6.   def main(args: Array[String]): Unit = {  
  7.     val conf = new SparkConf().setAppName("SparkRDDTest2").setMaster("local")  
  8.     val sc = new SparkContext(conf)  
  9.   
  10.     //指定爲2個分區  
  11.     val rdd1 = sc.parallelize(List(1, 2, 3, 4, 5, 6, 7), 2)  
  12.     //設定一個函數,設定分區的ID索引,數值  
  13.     val func1 = (index: Int, iter: Iterator[(Int)]) => {  
  14.       iter.toList.map(x => "[partID:" + index + ", val: " + x + "]").iterator  
  15.     }  
  16.   
  17.   
  18.     //查看每個分區的信息  
  19.     val res1 = rdd1.mapPartitionsWithIndex(func1)  
  20.   
  21.     //用aggregate,指定初始值,對rdd1進行聚合操作,先進行局部求和,再進行全局求和  
  22.     val res2 = rdd1.aggregate(0)(_ + _, _ + _)  
  23.     //將局部分區中最大的數找出來再進行求和  
  24.     val res3 = rdd1.aggregate(0)(math.max(_, _), _ + _)  
  25.     //每個分區都以10爲初始值,10用了3次  
  26.     val res4 = rdd1.aggregate(10)(_ + _, _ + _)  
  27.   
  28.     /*  
  29.     運行結果:ArrayBuffer([partID:0, val: 1], [partID:0, val: 2],  
  30.     [partID:0, val: 3], [partID:1, val: 4], [partID:1, val: 5],  
  31.     [partID:1, val: 6], [partID:1, val: 7])  
  32.      */  
  33.     //    println(res1.collect().toBuffer)  
  34.     //運行結果:28  
  35.     //    println(res2)  
  36.     //運行結果:10  
  37.     //    println(res3)  
  38.     //運行結果:58  
  39.     //    println(res4)  
  40.   
  41.   
  42.     val rdd2 = sc.parallelize(List("a", "b", "c", "d", "e", "f"), 2)  
  43.     val res5 = rdd2.aggregate("|")(_ + _, _ + _)  
  44.   
  45.     //運行結果:||abc|def  
  46.     //    println(res5)  
  47.   
  48.     val rdd3 = sc.parallelize(List("12", "23", "345", "4567"), 2)  
  49.     //兩個分區先計算出字符串的最大長度,然後合成字符串  
  50.     val res6 = rdd3.aggregate("")((x, y) => math.max(x.length, y.length).toString, (x, y) => x + y)  
  51.     //運行結果:24 或者 42,體現了並行化的特點  
  52.     //    println(res6)  
  53.   
  54.     val rdd4 = sc.parallelize(List("12", "23", "345", ""), 2)  
  55.     val res7 = rdd4.aggregate("")((x, y) => math.min(x.length, y.length).toString, (x, y) => x + y)  
  56.     //運行結果:01 或者 10,值"0".toString的長度爲0,"0".toString.length的長度爲1  
  57.     /*  
  58.     math.min("".length, "12".length ) 的結果是:0 , math.min("0".length, "23".length ) 的結果是:1  
  59.     math.min("".length, "345".length) 的結果是:0 , math.min("0".length, "".length) 的結果是:0  
  60.      */  
  61.     //    println(res7)  
  62.   
  63.     val rdd5 = sc.parallelize(List("12", "23", "", "345"), 2)  
  64.     val res8 = rdd5.aggregate("")((x, y) => math.min(x.length, y.length).toString, (x, y) => x + y)  
  65.     //運行結果:11  
  66.     /*  
  67.     math.min("".length, "12".length ) 的結果是:0 , math.min("0".length, "23".length ) 的結果是:1  
  68.     math.min("".length, "".length) 的結果是:0 , math.min("0".length, "345".length) 的結果是:1  
  69.      */  
  70.     //        println(res8)  
  71.   
  72.     //aggregateByKey可以先進行局部操作,再進行全局操作。  
  73.     val pairRDD = sc.parallelize(List(("cat", 2), ("cat", 5), ("mouse", 4), ("cat", 12), ("dog", 12), ("mouse", 2)), 2)  
  74.   
  75.     def func2(index: Int, iter: Iterator[(String, Int)]): Iterator[String] = {  
  76.       iter.toList.map(x => "[partID:" + index + ", val: " + x + "]").iterator  
  77.     }  
  78.   
  79.     //    println(pairRDD.mapPartitionsWithIndex(func2).collect.toBuffer)  
  80.     //把每種類型的最大的次數取出來  
  81.     //運行結果:ArrayBuffer((dog,12), (cat,17), (mouse,6))  
  82.     println(pairRDD.aggregateByKey(0)(math.max(_, _), _ + _).collect.toBuffer)  
  83.     //運行結果:ArrayBuffer((dog,12), (cat,22), (mouse,20))  
  84.     println(pairRDD.aggregateByKey(10)(math.max(_, _), _ + _).collect.toBuffer)  
  85.     /*  
  86.     pairRDD.aggregateByKey(0)(_ + _ , _ + _).collect與pairRDD.reduceByKey( _ + _).collect,  
  87.     這兩個方法執行結果是一樣的,實際上底層都是調用的同一個方法:combineByKey  
  88.      */  
  89.   
  90.   
  91.   }  
  92.   
  93. }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章