Spark Counting co-occurrence - Algorithm for efficient multi-pass filtering of huge collections
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Apache Spark has become a prominent choice for handling large-scale data processing tasks. With its rich ecosystem and distributed computing capabilities, Spark provides an exceptional framework for performing complex operations on massive datasets efficiently. One such task is counting co-occurrence, which involves determining the frequency with which items appear together within a given set of data records. This article explores an algorithm for efficient multi-pass filtering of huge collections in Spark, aimed at counting co-occurrence.
Problem Statement
Counting co-occurrence is critical in various data processing applications, such as market basket analysis, social network analysis, and recommendation systems. The primary challenge lies in managing and processing massive collections where traditional methods may become inefficient due to resource limitations.
Algorithm Overview
The proposed algorithm employs a multi-pass method to filter through extensive datasets, ensuring efficient co-occurrence counting. The underlying strategy involves segmenting data processing into distinct phases, whereby each phase incrementally refines the dataset, leading to optimized performance.
Step 1: Data Preparation
Data preparation is the initial step, where input data is cleaned and transformed into a suitable structure for processing. This generally involves:
- Normalizing Data: Converting all textual data to a standard format (e.g., lowercase) to ensure uniformity.
- Item Pair Generation: Extracting all possible item pairs from each data record for subsequent counting.
- Threshold Determination: A threshold should be computed or selected beforehand. This could be based on percentiles, data distribution, or empirical methods.
- Significance Testing: Calculate statistical significance for each pair to retain only the ones surpassing the threshold.
- Use Spark's caching mechanisms to store intermediate datasets that are used multiple times, reducing recomputation.
- Optimize data shuffling by controlling the number of partitions based on available cluster resources. Adjust partition count using `repartition()` or `coalesce()`.
- Utilize Spark’s broadcast variables to efficiently distribute small datasets or configuration parameters across nodes.
Related reading
- Spark Dataframe write to kafka topic in avro format?
- Spark DStream periodically call saveAsObjectFile using transform does not work as expected
- Spark Executor Managed memory leak detected
- Spark executor metrics don't reach prometheus sink
- Spark What is the time complexity of the connected components algorithm used in GraphX?
- Spatial data structure for finding all points greater than or less than a value in each cartesian dimension
- Spark executor self-exiting due to driver disassociated in Kubernetes with client deploy-mode
- Spark executors fails to run on kubernetes cluster

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.