Skip to Content
Course content

79: Working with Big Data via sparklyr

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Look, I've seen this happen to almost every R developer the first time they touch sparklyr. You've got a dataset that's too big for your RAM—maybe a few hundred gigabytes of server logs or transaction data—and you install sparklyr thinking, "Great, now my dplyr code will just run faster."

The Myth: "It's just R, but faster"

The biggest misconception is believing that sparklyr is simply an optimized version of R that handles big files. If you think of it as "R with a turbo button," you're going to run into a wall very quickly. Here is exactly where that mindset fails: you'll try to pass a Spark DataFrame into a standard R function—say, a custom mapping function or a niche package like ggplot2—and you'll get a cryptic error or, worse, your R session will instantly crash.

# This is where the crash happens
# Assume 'big_taxi_data' is a Spark DataFrame with 100 million rows
library(ggplot2)
ggplot(big_taxi_data, aes(x = trip_distance, y = fare)) + geom_point() 
# Result: R tries to pull 100 million rows into your local RAM. 
# Boom. Memory exhausted.

The reason this fails is that big_taxi_data isn't actually a data frame in the way you're used to. It's essentially a pointer to a table living on a Spark cluster. R isn't doing the work; Spark is.

The Reality: Translation, not Execution

When you use sparklyr, you aren't writing R code that manipulates data; you are writing R code that sparklyr translates into Spark SQL. When you call filter() or summarise() on a Spark object, R doesn't touch a single row of data. Instead, it sends a set of instructions to the Spark engine: "Hey Spark, filter these rows and give me the sum of this column."

This is called lazy evaluation. Spark doesn't actually compute anything until you explicitly ask for the result to be brought back into your R session. This is why the collect() function is the most important tool in your sparklyr arsenal. I always tell my juniors: collect() is the border crossing. Everything before it happens in the cloud/cluster; everything after it happens in your local R memory.

Here is how you actually handle a massive dataset, like the NYC Taxi trip data, without blowing up your laptop:

library(sparklyr)
library(dplyr)

# Connect to the cluster
sc <- spark_connect(master = "local")

# Reference the data (this doesn't load it into RAM!)
taxi_spark <- spark_read_parquet(sc, name = "taxi_data", path = "s3://my-bucket/taxi_trips/")

# This part is just building a "plan". No data is moving yet.
summary_stats <- taxi_spark %>%
  filter(trip_distance > 2) %>%
  group_by(vendor_id) %>%
  summarise(avg_fare = mean(fare_amount, na.rm = TRUE))

# NOW we pull the small, aggregated result back to R
final_result <- summary_stats %>% collect()

# Now it's a regular R data frame, and we can plot it
ggplot(final_result, aes(x = vendor_id, y = avg_fare)) + geom_col()

Notice the flow: Filter and aggregate on the cluster first to shrink the data down to a manageable size, and then call collect(). If you collect too early, you've defeated the whole purpose of using Spark.

  • The Spark DataFrame: A reference to data on the cluster. You can use dplyr verbs on it, but you can't use standard R functions.
  • The Translation: sparklyr converts dplyr calls into SQL. If you use a function that sparklyr doesn't know how to translate, it will throw an error.
  • The Collection: collect() brings the data from Spark into R. Use it only at the very end of your pipeline.



📋 Practical Task

Exercise: Aggregating Million-Row Taxi Trip Data with Sparklyr

You have been provided with a Spark connection sc and a Spark DataFrame named nyc_trips_spark containing 50 million rows of taxi data. Your goal is to find the average tip amount for trips that lasted more than 15 minutes, broken down by the payment type.

Requirements:

  • Do NOT use collect() until the final step.
  • Filter the data for trip_duration > 15.
  • Group by payment_type.
  • Calculate the mean of tip_amount.
  • Collect the final aggregated table into a local R data frame called tip_summary.
# Your code here:
# sc is already connected
# nyc_trips_spark is already loaded

tip_summary <- nyc_trips_spark %>%
  # Add your pipeline here
Rating
0 0

There are no comments for now.

to be the first to leave a comment.