Module 19 · Big data with Spark · Lesson 63 of 66

RDD transformations and actions

Read text files into RDDs, transform them with map and filter, and sum by key.

In this lesson · about 25 minutes

  • Tell transformations from actions
  • Use textFile, filter, map, flatMap, collect, count, first and take
  • Group and total values with reduceByKey and sortBy

Everything starts from a SparkContext, usually called sc. sc.textFile('example.txt') makes an RDD with one item per line of the file.

RDD methods come in two kinds:

  • Transformations make a new RDD: map (apply a function to every item), filter (keep items that pass a test), flatMap (like map, but flattens lists into one list). On real Spark they're lazy: nothing runs yet; Spark just records the plan.
  • Actions return a result to you and make the work happen: collect (everything, as a list), count, first, take(n).

map versus flatMap

Exercise

Count words

Using sc, read example2.txt into text_rdd. Make words, an RDD of every word in the file (use flatMap), and end with the number of words.

Code that runs before yours
from minispark import SparkContext
sc = SparkContext()

Key-value pairs

A file of service orders, services.txt, has a header line starting with #, then one order per line: event id, date, customer, state, service id and amount. Two maps clean it up: drop the #, then split each line into fields.

Many Spark jobs work on pairs: (key, value) tuples. reduceByKey(f) combines all the values that share a key with f, two at a time. To total the amount per state: map each order to (state, amount), then reduce by key with addition. The amounts are text, so convert them with float first.

Exercise

Sales by state

From cleanServ, make state_totals: the total amount per state, as an RDD of (state, total) pairs. Skip the header row (its state field is the text 'State'), and sort from highest total to lowest. End with state_totals.collect().

Code that runs before yours
from minispark import SparkContext
sc = SparkContext()
services = sc.textFile('services.txt')
cleanServ = services.map(lambda x: x[1:] if x[0] == '#' else x).map(lambda x: x.split())

Exercises solved: 0 of 2

Tip: solve each exercise first. Press Check to test your code.

Noviq Academy's Data Science course is free and for learning. Python runs in your browser with Pyodide, which downloads about 20 MB the first time, plus a few more for scikit-learn. Your code and progress stay on this device. The salaries data is a sample of San Francisco's public pay records; the purchases, customers, housing, advertising and classified data are made up. The Titanic, tips, flights and iris data sets come from Seaborn's examples; the maps use public 2011 to 2014 US and world figures. The 911 data is a 20,000-call sample of Montgomery County, Pennsylvania's public 911 records; the movie ratings are the MovieLens 100K data from GroupLens Research; the reviews are a 3,000-review sample from the Yelp Dataset Challenge; the loans are public LendingClub data; the colleges are the College data from An Introduction to Statistical Learning; the bank notes, kyphosis and breast cancer data come from the UCI Machine Learning Repository. Spark and TensorFlow can't run in a browser, so those lessons use stand-ins that run here, and show the code for the real tools.