Key Features of SparkR
- Distributed DataFrames (SparkDataFrame)
SparkR provides a distributed implementation of data frames called SparkDataFrame, which is conceptually similar to R’s native data.frame or dplyr objects but operates on large, distributed datasets across a cluster. These data frames support common operations like:
- Selection (`select`)
- Filtering (`filter`)
- Aggregation (`groupBy`, `summarize`)
- Sorting (`arrange`)
- Column manipulation
Example:
``` r
df <- as.DataFrame(faithful)
head(filter(df, df$waiting < 50))
```
- SparkSession – Entry Point
The sparkR.session() function initializes a SparkSession, which serves as the entry point to interact with Spark. This session connects the R environment to a Spark cluster and allows configuration of settings such as memory, master URL, and integration with external packages.
Example:
``` r
sparkR.session(master = "local[*]", sparkConfig = list(spark.driver.memory = "2g"))
```
- Data Source Integration
SparkR supports reading and writing data from various formats including:
- JSON (newline-delimited)
- CSV
- Parquet
- Hive tables
- External databases
Data can be loaded using read.df() and saved via write.df().
Example:
``` r
people <- read.df("people.json", "json")
write.df(people, "output.parquet", "parquet", mode = "overwrite")
```
- SQL Support
SparkR allows running SQL queries over SparkDataFrames by registering them as temporary views:
``` r
createOrReplaceTempView(people, "people")
teenagers <- sql("SELECT name FROM people WHERE age >= 13 AND age <= 19")
```
- Machine Learning (MLlib)
SparkR provides a formula-based interface to several MLlib algorithms, enabling distributed model training and prediction. Supported algorithms include:
- **Classification**: Logistic Regression, Naive Bayes, SVM, Decision Trees
- **Regression**: Linear, Generalized Linear Models (GLM), Isotonic Regression
- **Clustering**: K-means, Gaussian Mixture Models
- **Collaborative Filtering & Frequent Pattern Mining**
Models can be saved and loaded using `write.ml()` and `read.ml()`.
- User-Defined Functions (UDFs)
SparkR supports applying custom R functions to distributed data using:
- `dapply()` / `dapplyCollect()`: Apply a function to each partition
- `gapply()` / `gapplyCollect()`: Apply a function to each group
- `spark.lapply()`: Distribute `lapply`-style computations across the cluster
- Apache Arrow Integration
For improved performance in data transfer between JVM and R processes, SparkR supports Apache Arrow, an in-memory columnar format. When enabled (spark.sql.execution.arrow.sparkr.enabled = true), it accelerates operations like collect(), createDataFrame(), dapply(), and gapply().
- Structured Streaming
SparkR supports Spark’s Structured Streaming API for scalable and fault-tolerant stream processing, allowing real-time data ingestion and transformation.
- Data Type Mapping
SparkR automatically maps R data types to corresponding Spark SQL types (e.g., numeric → double, character → string, POSIXct → timestamp).
- Eager Execution (REPL Mode)
When enabled (spark.sql.repl.eagerEval.enabled = true), SparkDataFrames display their content immediately (like regular R data frames), improving interactivity in shells and notebooks.