January 2019
Beginner to intermediate
154 pages
4h 31m
English
A reduceByKey() transformation is available on Pair RDD. It allows aggregation of data by minimizing the data shuffle and performs operations on each key in parallel. A reduceByKey() transformation first performs the local aggregation within the executor and then shuffles the aggregated data between each node. In the following example, we calculate the sum for each key using reduceByKey:
#PythonpairRDD = spark.sparkContext.parallelize([(1, 5),(1, 10),(2, 4),(3, 1),(2, 6)])pairRDD.reduceByKey(lambda x,y : x+y).collect()Output:[(1, 15), (2, 10), (3, 1)]
The following code performs the same operation in Scala:
//Scalaval pairRDD = spark.sparkContext.parallelize(Array((1, 5),(1, 10),(2, 4),(3, 1),(2, 6)))pairRDD.reduceByKey(_+_).collect() ...
Read now
Unlock full access