Spark 2 Workbook Answers Apr 2026

If the workbook includes a **mini‑project** (e.g., “process a log dataset and produce a daily report”), you can outline the full pipeline:

1. **Ingestion** – `spark.read.json` or `textFile`. 2. **Parsing** – `withColumn` + `from_unixtime`, `regexp_extract`. 3. **Cleaning** – filter out malformed rows, `na.drop`. 4. **Enrichment** – join with a static lookup table (broadcast). 5. **Aggregation** – `groupBy(date, status).agg(count("*").as("cnt"))`. 6. **Output** – write to Parquet partitioned by `date` **or** stream to console for debugging.

words = lines.flatMap(lambda line: line.split()) # optional cleaning cleaned = words.map(lambda w: w.lower().strip('.,!?"\'')) distinct_words = cleaned.distinct() count = distinct_words.count() spark 2 workbook answers

val df = spark.read .option("header","true") .option("inferSchema","true") .csv("hdfs:///data/employees.csv")

---

---

## 6. Quick Reference Cheatsheet (Spark 2.4) If the workbook includes a **mini‑project** (e

# 4️⃣ Action – trigger the computation and collect the count unique_word_count = distinct_words.count()

sc = SparkContext(appName="DistinctWordCount") avg($"salary").as("avg_salary")) .filter($"emp_cnt" &gt

```python from pyspark import SparkContext

val result = df .groupBy($"department") .agg(count("*").as("emp_cnt"), avg($"salary").as("avg_salary")) .filter($"emp_cnt" > 5)