
Data Algorithms: Recipes for Scaling Up with Hadoop and Spark¶
Source code for the book Data Algorithms by Mahmoud Parsian (O'Reilly Media, July 2015): distributed algorithms for data analysis, bioinformatics and machine learning, each implemented with Spark (Java, Java 8 lambdas, Scala) and MapReduce/Hadoop.
All programs build with Maven, Gradle or Ant and run on Spark 4.3.0; every program is run in CI on Java 17 and 21. Website: https://mahmoudparsian.github.io/data-algorithms-book/ (one page per chapter, with a command to run every program).
2nd edition: Data Algorithms with Spark (O'Reilly, 2022) covers the same ideas with PySpark.
Contents¶
- Quick start
- Requirements
- Building
- Running the programs
- Chapters
- Repository layout
- What's new (Spark 4.3.0)
- More resources
- Author
Quick start¶
With Java 17+ and Spark 4.3.0 in ~/spark-4.3.0
(or set SPARK_HOME):
git clone https://github.com/mahmoudparsian/data-algorithms-book.git
cd data-algorithms-book
scripts/build_with_maven.sh # or: scripts/build_with_ant.sh, ./gradlew build
scripts/run_spark.sh org.dataalgorithms.chap03.spark.Top10 data/chap03/cats.txt
The last lines of the output are the top 10 cats by weight (two cats share the weight 1000):
1000--cat100
1000--cat200
Full output
args[0]: <input-path>=data/chap03/cats.txt
key=cat1 value= 12
key=cat2 value= 13
key=cat3 value= 14
key=cat4 value= 15
key=cat5 value= 10
key=cat100 value= 1000
key=cat200 value= 1000
key=cat300 value= 20
key=cat8 value= 30
key=cat9 value= 31
key=cat10 value= 40
key=cat11 value= 1
12--cat1
13--cat2
14--cat3
15--cat4
20--cat300
30--cat8
31--cat9
40--cat10
1000--cat100
1000--cat200
Requirements¶
| Software | Version | Notes |
|---|---|---|
| Java | 17 or 21 | required by Spark 4.3.0 |
| Spark | 4.3.0 | download, or pip install -r requirements.txt |
| Build tool | Maven 3.9+, Gradle 8.14.3 (wrapper included), or Ant 1.10+ | any one of them |
| Hadoop | not needed | MapReduce programs run in Hadoop's local mode with Spark's Hadoop 3.5.0 jars |
| Python | 3.11+ | only for tools/run_programs.py and the PySpark program |
Building¶
| Build | Command | Output |
|---|---|---|
| Maven | scripts/build_with_maven.sh |
target/data-algorithms-book.jar |
| Ant | scripts/build_with_ant.sh |
dist/data_algorithms_book.jar |
| Gradle | ./gradlew build |
build/libs/data-algorithms-book.jar (and dist/) |
All three compile the same Java and Scala sources (Scala 2.13.18, Java 17 bytecode). Details: Maven · Ant · Running the programs.
Running the programs¶
# Spark programs (Java and Scala), local mode
scripts/run_spark.sh <class-name> [arguments...]
# MapReduce programs, Hadoop local mode (no Hadoop installation needed)
scripts/run_mapreduce.sh <class-name> [arguments...]
# every program with sample inputs (about 10 minutes)
python3 tools/run_programs.py
python3 tools/run_programs.py chap14 # only the programs of chapter 14
The arguments of all 177 programs, with sample inputs from data/,
are listed in tools/programs.json.
On a cluster, use spark-submit --master yarn or hadoop jar with the same jar
(examples).
Chapters¶
| # | Chapter | Programs |
|---|---|---|
| 1 | Secondary Sort: Introduction | Java (7) · Scala (1) · Python (1) |
| 2 | Secondary Sort: A Detailed Example | Java (1) |
| 3 | Top 10 List | Java (9) · Scala (2) |
| 4 | Left Outer Join | Java (4) · Scala (3) |
| 5 | Order Inversion | Java (3) · Scala (2) |
| 6 | Moving Average | Java (3) · Scala (2) |
| 7 | Market Basket Analysis | Java (3) · Scala (1) |
| 8 | Common Friends | Java (4) · Scala (1) |
| 9 | Recommendation Engines Using MapReduce | Java (1) · Scala (1) |
| 10 | Content-Based Recommendation: Movies | Java (2) · Scala (1) |
| 11 | Smarter Email Marketing with the Markov Model | Java (7) · Scala (1) |
| 12 | K-Means Clustering | Scala (1) |
| 13 | k-Nearest Neighbors | Java (3) · Scala (1) |
| 14 | Naive Bayes | Java (4) · Scala (2) |
| 15 | Sentiment Analysis | — |
| 16 | Finding, Counting, and Listing All Triangles in Large Graphs | Java (3) · Scala (1) |
| 17 | K-mer Counting | Java (4) · Scala (1) |
| 18 | DNA Sequencing | — |
| 19 | Cox Regression | — |
| 20 | Cochran-Armitage Test for Trend | — |
| 21 | Allelic Frequency | — |
| 22 | The T-Test | Java (2) · Scala (1) |
| 23 | Pearson Correlation | Java (4) · Scala (1) |
| 24 | DNA Base Count | Java (5) · Scala (4) |
| 25 | RNA Sequencing | — |
| 26 | Gene Aggregation | Java (2) · Scala (2) |
| 27 | Linear Regression | — |
| 28 | MapReduce and Monoids | Java (3) · Scala (2) |
| 29 | The Small Files Problem | Java (3) |
| 30 | Huge Cache for MapReduce | — |
| 31 | The Bloom Filter | — |
Chapters marked "—" have no source code in this repository.
Bonus chapters (about them):
| # | Bonus chapter | Programs |
|---|---|---|
| B01 | Word Count | Java (3) |
| B02 | Sorted Word Count | Java (2) |
| B03 | Per-Key Average | Java (2) |
| B04 | Rank Product | Java (4) |
| B05 | Anagram Finder | Java (7) |
| B06 | Outlier Detection | Java (2) |
| B07 | SQL on Spark | Java (2) |
| B08 | Log Query | Java (2) |
| B09 | Character Count | Java (6) |
| B10 | Friend Recommendation | Java (3) |
| B11 | Cartesian Product | Java (1) |
| B12 | Document Word Count | Java (3) |
| B13 | Submitting Spark Jobs from Java | Java (4) |
| B14 | Min and Max | Java (3) |
Machine learning with Spark MLlib: K-means, linear and logistic regression, Naive Bayes (21 programs).
Repository layout¶
| Path | Contents |
|---|---|
src/main/java/org/dataalgorithms/ |
Java programs (spark, sparkwithlambda, mapreduce packages per chapter) |
src/main/scala/org/dataalgorithms/ |
Scala programs |
src/python/ |
PySpark program |
data/ |
sample inputs |
scripts/ |
build scripts and program launchers |
tools/ |
run_programs.py and the program list programs.json |
misc/ |
documentation, webinars, figures |
pom.xml, build.gradle, build.xml |
Maven, Gradle and Ant builds |
What's new (Spark 4.3.0)¶
The code was refreshed in 2026 for Spark 4.3.0, Scala 2.13 and Java 17+:
three builds (the old lib/ jars are gone), launchers that run every program on
a laptop, sample inputs for every program, CI on Java 17 and 21, and fixes for
bugs and APIs removed from Spark. The program code changed only where needed; see
the list of changes.
Thanks to @willy-b for the typo and link fixes, the chapter 3 Top-10 fix and the Gradle build (PR #43), and to all contributors.
More resources¶
- Java 8 lambda expressions with Spark
- Scala Spark solutions
- Introduction to MapReduce
- Submit a Spark job from Java code
- Spark for cancer outlier profile analysis
- Webinars and presentations
- Author book signing
- My favorite quotes
Author¶
Mahmoud Parsian
- LinkedIn · Twitter: @mahmoudparsian
- Email: mahmoud.parsian@yahoo.com
Questions and comments are welcome: please open an issue.
Licensed under the Apache License 2.0.
