PySpark : Aggregation operations on key-value pair RDDs [combineByKey in PySpark]

PySpark @

In this article, we will explore the use of combineByKey in PySpark, a powerful and flexible method for performing aggregation operations on key-value pair RDDs. We will provide a detailed example.

First, let’s create a PySpark RDD:

# Using combineByKey in PySpark with a Detailed Example
from pyspark import SparkContext
sc = SparkContext("local", "combineByKey Example")
data = [("America", 1), ("Botswana", 2), ("America", 3), ("Botswana", 4), ("America", 5), ("Egypt", 6)]
rdd = sc.parallelize(data)

Using combineByKey

Now, let’s use the combineByKey method to compute the average value for each key in the RDD:

def create_combiner(value):
    return (value, 1)

def merge_value(acc, value):
    sum, count = acc
    return (sum + value, count + 1)

def merge_combiners(acc1, acc2):
    sum1, count1 = acc1
    sum2, count2 = acc2
    return (sum1 + sum2, count1 + count2)

result_rdd = rdd.combineByKey(create_combiner, merge_value, merge_combiners)
average_rdd = result_rdd.mapValues(lambda acc: acc[0] / acc[1])
result_data = average_rdd.collect()

print("Average values per key:")
for key, value in result_data:
    print(f"{key}: {value:.2f}")

In this example, we used the combineByKey method on the RDD, which requires three functions as arguments:

  1. A function that initializes the accumulator for each key. In our case, it creates a tuple with the value and a count of 1.
  2. merge_value: A function that updates the accumulator for a key with a new value. It takes the current accumulator and the new value, then updates the sum and count.
  3. merge_combiners: A function that merges two accumulators for the same key. It takes two accumulators and combines their sums and counts.

We then use mapValues to compute the average value for each key by dividing the sum by the count.

The output will be:

Average values per key:
America: 3.00
Botswana: 3.00
Egypt: 6.00


RDD.combineByKey(createCombiner: Callable[[V], U], mergeValue: Callable[[U, V], U], mergeCombiners: Callable[[U, U], U], numPartitions: Optional[int] = None, partitionFunc: Callable[[K], int] = <function portable_hash>) → pyspark.rdd.RDD[Tuple[K, U]]

Turns an RDD[(K, V)] into a result of type RDD[(K, C)], for a “combined type” C. Here users can control the partitioning of the output RDD.

Spark important urls to refer

  1. Spark Examples
  2. PySpark Blogs
  3. Bigdata Blogs
  4. Spark Interview Questions
  5. Official Page
Author: user

Leave a Reply