【SparkAPI JAVA版】JavaPairRDD——aggregate(一)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sdut406/article/details/87927863

JavaPairRDD的aggregate方法讲解

官方文档说明
 /**
   * Aggregate the elements of each partition, and then the results for all the partitions, using
   * given combine functions and a neutral "zero value". This function can return a different result
   * type, U, than the type of this RDD, T. Thus, we need one operation for merging a T into an U
   * and one operation for merging two U's, as in scala.TraversableOnce. Both of these functions are
   * allowed to modify and return their first argument instead of creating a new U to avoid memory
   * allocation.
   *
   * @param zeroValue the initial value for the accumulated result of each partition for the
   *                  `seqOp` operator, and also the initial value for the combine results from
   *                  different partitions for the `combOp` operator - this will typically be the
   *                  neutral element (e.g. `Nil` for list concatenation or `0` for summation)
   * @param seqOp an operator used to accumulate results within a partition
   * @param combOp an associative operator used to combine results from different partitions
   */
中文含义

aggregate先对每个分区的元素做聚集,然后对所有分区的结果做聚集,聚集过程中,使用的是给定的聚集函数以及初始值”zero value”。这个函数能返回一个与原始RDD不同的类型U,因此,需要一个合并RDD类型T到结果类型U的函数,还需要一个合并类型U的函数。这两个函数都可以修改和返回他们的第一个参数,而不是重新新建一个U类型的参数以避免重新分配内存。
参数zeroValue:seqOp运算符的每个分区的累积结果的初始值以及combOp运算符的不同分区的组合结果的初始值 - 这通常将是初始元素(例如“Nil”表的列表 连接或“0”表示求和)
参数seqOp: 每个分区累积结果的聚集函数。

方法原型
// Scala
def aggregate[U: ClassTag](zeroValue: U)(seqOp: (U, T) => U, combOp: (U, U) => U): U
// java
public static <U> U aggregate(U zeroValue,
                              Function2<U,T,U> seqOp,
                              Function2<U,U,U> combOp)
实例
public class Aggregate {

    public static void main(String[] args) {

        System.setProperty("hadoop.home.dir","F:\\hadoop-2.7.1");

        SparkConf conf = new SparkConf().setMaster("local").setAppName("TestSpark");
        JavaSparkContext sc = new JavaSparkContext(conf);
        JavaPairRDD<String,Integer> javaPairRDD =
                sc.parallelizePairs(Lists.<Tuple2<String, Integer>>newArrayList(new Tuple2<String, Integer>("cat",34),
                        new Tuple2<String, Integer>("cat",34),new Tuple2<String, Integer>("dog",34),new Tuple2<String, Integer>("tiger",34)),2);

        // 打印样例数据

        javaPairRDD.foreach(new VoidFunction<Tuple2<String, Integer>>() {
            public void call(Tuple2<String, Integer> stringIntegerTuple2) throws Exception {
                System.out.println("样例数据>>>>>>>" + stringIntegerTuple2);
            }
        });
        Integer integer = javaPairRDD.aggregate(0, new Function2<Integer, Tuple2<String, Integer>, Integer>() {
            public Integer call(Integer v1, Tuple2<String, Integer> v2) throws Exception {

                System.out.println("seqOp>>>>>  参数One:"+v1+"--参数Two:"+v2);
                return v1+v2._2();
            }
        }, new Function2<Integer, Integer, Integer>() {
            public Integer call(Integer v1, Integer v2) throws Exception {
                System.out.println("combOp>>>>>  参数One:"+v1+"--参数Two:"+v2);
                return v1+v2;
            }
        });
        System.out.println("result:"+integer);
    }
}
结果
// 打印样例数据 因为是2个分片 所以会隔开打印 如果只有一个分片 中间是没有日志的
样例数据>>>>>>>(cat,34)
样例数据>>>>>>>(cat,34)
19/02/27 23:06:21 INFO Executor: Finished task 0.0 in stage 0.0 (TID 0). 665 bytes result sent to driver
19/02/27 23:06:21 INFO TaskSetManager: Starting task 1.0 in stage 0.0 (TID 1, localhost, executor driver, partition 1, PROCESS_LOCAL, 4907 bytes)
19/02/27 23:06:21 INFO Executor: Running task 1.0 in stage 0.0 (TID 1)
样例数据>>>>>>>(dog,34)
样例数据>>>>>>>(tiger,34)
// 进行seqOp 或者也可以说是分片元素聚合 0这个初始值 会被添加进去 

seqOp>>>>>  参数One:0--参数Two:(cat,34)
seqOp>>>>>  参数One:34--参数Two:(cat,34)
19/02/27 23:06:21 INFO Executor: Finished task 0.0 in stage 1.0 (TID 2). 659 bytes result sent to driver
19/02/27 23:06:21 INFO TaskSetManager: Starting task 1.0 in stage 1.0 (TID 3, localhost, executor driver, partition 1, PROCESS_LOCAL, 4907 bytes)
19/02/27 23:06:21 INFO Executor: Running task 1.0 in stage 1.0 (TID 3)
seqOp>>>>>  参数One:0--参数Two:(dog,34)
seqOp>>>>>  参数One:34--参数Two:(tiger,34)

// 进行combOp 也可以说是分片结果聚合 
combOp>>>>>  参数One:0--参数Two:68
combOp>>>>>  参数One:68--参数Two:68
// 最终结果
result:136
解析

从上述过程中,我们就能明白流程是什么了。

  1. seqOp(分片元素聚合)
    开始我们的数据是:
    分片1:(cat,34) (cat,34)
    分片2:(dog,34) (tiger,34)

     	// 这里只有两个分片 所以写两个过程 
     	
        第一个分片开始元素聚合过程:
        zeroValue + 第一个元素值 = 结果1,
        结果1+第二个元素值 = 结果2,
        。。。。
        结果n-1 + 结果n = 分片结果1。
        
     	第二个分片开始元素聚合过程:
        zeroValue + 第一个元素值 = 结果1,
        结果1+第二个元素值 = 结果2,
        。。。。
        结果n-1 + 结果n = 分片结果2。
    
  2. combOp(分片结果聚合)

     	开始分片结果聚合过程:
     	zeroValue + 分片结果1  = 最终结果1,
        最终结果1+ 分片结果2  = 最终结果2
    
     	最终得到的最终结果2 就是返回的结果 136
    

如果有什么不明白的评论留言即可。

猜你喜欢

转载自blog.csdn.net/sdut406/article/details/87927863