-
GayathriMurali authored
## What changes were proposed in this pull request? Make user guide changes to SparkR documentation for all changes that happened in 2.0 to Machine Learning APIs Author: GayathriMurali <gayathri.m@intel.com> Closes #13285 from GayathriMurali/SPARK-15129.
GayathriMurali authored## What changes were proposed in this pull request? Make user guide changes to SparkR documentation for all changes that happened in 2.0 to Machine Learning APIs Author: GayathriMurali <gayathri.m@intel.com> Closes #13285 from GayathriMurali/SPARK-15129.
- Overview
- SparkR DataFrames
- Starting Up: SparkContext, SQLContext
- Starting Up from RStudio
- Creating DataFrames
- From local data frames
- Displays the content of the DataFrame to stdout
- eruptions waiting
- From Data Sources
- age name
- SparkR automatically infers the schema from the JSON file
- root
- |-- age: long (nullable = true)
- |-- name: string (nullable = true)
- From Hive tables
- Queries can be expressed in HiveQL.
- results is now a DataFrame
- key value
- 1 238 val_238
- 2 86 val_86
- 3 311 val_311
- DataFrame Operations
- Selecting rows, columns
- Get basic information about the DataFrame
- SparkDataFrame[eruptions:double, waiting:double]
- Select only the "eruptions" column
- eruptions
- You can also pass in column name as strings
- Filter the DataFrame to only retain rows with wait times shorter than 50 mins
- eruptions waiting
- Grouping, Aggregation
- We use the n operator to count the number of times each waiting time appears
- waiting count
- We can also sort the output from the aggregation to get the most common waiting times
- waiting count
- Operating on Columns
- Convert waiting time from hours to seconds.
- Note that we can assign this to a new column in the same DataFrame
- eruptions waiting waiting_secs
- Running SQL Queries from SparkR
- Register this DataFrame as a table.
- SQL statements can be run by using the sql method
- name
- Machine Learning
- Model persistence
- R Function Name Conflicts
- Migration Guide
- Upgrading From SparkR 1.5.x to 1.6.x
- Upgrading From SparkR 1.6.x to 2.0
layout: global
displayTitle: SparkR (R on Spark)
title: SparkR (R on Spark)
- This will become a table of contents (this text will be scraped). {:toc}
Overview
SparkR is an R package that provides a light-weight frontend to use Apache Spark from R. In Spark {{site.SPARK_VERSION}}, SparkR provides a distributed data frame implementation that supports operations like selection, filtering, aggregation etc. (similar to R data frames, dplyr) but on large datasets. SparkR also supports distributed machine learning using MLlib.
SparkR DataFrames
A DataFrame is a distributed collection of data organized into named columns. It is conceptually equivalent to a table in a relational database or a data frame in R, but with richer optimizations under the hood. DataFrames can be constructed from a wide array of sources such as: structured data files, tables in Hive, external databases, or existing local R data frames.
All of the examples on this page use sample data included in R or the Spark distribution and can be run using the ./bin/sparkR
shell.
Starting Up: SparkContext, SQLContext
Starting Up from RStudio
You can also start SparkR from RStudio. You can connect your R program to a Spark cluster from
RStudio, R shell, Rscript or other R IDEs. To start, make sure SPARK_HOME is set in environment
(you can check Sys.getenv),
load the SparkR package, and call sparkR.init
as below. In addition to calling sparkR.init
, you
could also specify certain Spark driver properties. Normally these
Application properties and
Runtime Environment cannot be set programmatically, as the
driver JVM process would have been started, in this case SparkR takes care of this for you. To set
them, pass them as you would other configuration properties in the sparkEnvir
argument to
sparkR.init()
.
The following options can be set in sparkEnvir
with sparkR.init
from RStudio:
Property Name | Property group |
spark-submit equivalent |
---|---|---|
spark.driver.memory |
Application Properties | --driver-memory |
spark.driver.extraClassPath |
Runtime Environment | --driver-class-path |
spark.driver.extraJavaOptions |
Runtime Environment | --driver-java-options |
spark.driver.extraLibraryPath |
Runtime Environment | --driver-library-path |
Creating DataFrames
With a SQLContext
, applications can create DataFrame
s from a local R data frame, from a Hive table, or from other data sources.
From local data frames
The simplest way to create a data frame is to convert a local R data frame into a SparkR DataFrame. Specifically we can use createDataFrame
and pass in the local R data frame to create a SparkR DataFrame. As an example, the following creates a DataFrame
based using the faithful
dataset from R.
Displays the content of the DataFrame to stdout
head(df)
eruptions waiting
##1 3.600 79 ##2 1.800 54 ##3 3.333 74
{% endhighlight %}
From Data Sources
SparkR supports operating on a variety of data sources through the DataFrame
interface. This section describes the general methods for loading and saving data using Data Sources. You can check the Spark SQL programming guide for more specific options that are available for the built-in data sources.
The general method for creating DataFrames from data sources is read.df
. This method takes in the SQLContext
, the path for the file to load and the type of data source. SparkR supports reading JSON, CSV and Parquet files natively and through Spark Packages you can find data source connectors for popular file formats like Avro. These packages can either be added by
specifying --packages
with spark-submit
or sparkR
commands, or if creating context through init
you can specify the packages with the packages
argument.
We can see how to use data sources using an example JSON input file. Note that the file that is used here is not a typical JSON file. Each line in the file must contain a separate, self-contained valid JSON object. As a consequence, a regular multi-line JSON file will most often fail.
{% highlight r %} people <- read.df(sqlContext, "./examples/src/main/resources/people.json", "json") head(people)
age name
##1 NA Michael ##2 30 Andy ##3 19 Justin
SparkR automatically infers the schema from the JSON file
printSchema(people)
root
|-- age: long (nullable = true)
|-- name: string (nullable = true)
{% endhighlight %}
The data sources API can also be used to save out DataFrames into multiple file formats. For example we can save the DataFrame from the previous example
to a Parquet file using write.df
(Until Spark 1.6, the default mode for writes was append
. It was changed in Spark 1.7 to error
to match the Scala API)
From Hive tables
You can also create SparkR DataFrames from Hive tables. To do this we will need to create a HiveContext which can access tables in the Hive MetaStore. Note that Spark should have been built with Hive support and more details on the difference between SQLContext and HiveContext can be found in the SQL programming guide.
sql(hiveContext, "CREATE TABLE IF NOT EXISTS src (key INT, value STRING)") sql(hiveContext, "LOAD DATA LOCAL INPATH 'examples/src/main/resources/kv1.txt' INTO TABLE src")
Queries can be expressed in HiveQL.
results <- sql(hiveContext, "FROM src SELECT key, value")
results is now a DataFrame
head(results)
key value
1 238 val_238
2 86 val_86
3 311 val_311
{% endhighlight %}
DataFrame Operations
SparkR DataFrames support a number of functions to do structured data processing. Here we include some basic examples and a complete list can be found in the API docs:
Selecting rows, columns
Get basic information about the DataFrame
df
SparkDataFrame[eruptions:double, waiting:double]
Select only the "eruptions" column
head(select(df, df$eruptions))
eruptions
##1 3.600 ##2 1.800 ##3 3.333
You can also pass in column name as strings
head(select(df, "eruptions"))
Filter the DataFrame to only retain rows with wait times shorter than 50 mins
head(filter(df, df$waiting < 50))
eruptions waiting
##1 1.750 47 ##2 1.750 47 ##3 1.867 48
{% endhighlight %}
Grouping, Aggregation
SparkR data frames support a number of commonly used functions to aggregate data after grouping. For example we can compute a histogram of the waiting
time in the faithful
dataset as shown below
n
operator to count the number of times each waiting time appears
We use the head(summarize(groupBy(df, dfwaiting), count = n(dfwaiting)))
waiting count
##1 70 4 ##2 67 1 ##3 69 2
We can also sort the output from the aggregation to get the most common waiting times
waiting_counts <- summarize(groupBy(df, dfwaiting), count = n(dfwaiting)) head(arrange(waiting_counts, desc(waiting_counts$count)))
waiting count
##1 78 15 ##2 83 14 ##3 81 13
{% endhighlight %}
Operating on Columns
SparkR also provides a number of functions that can directly applied to columns for data processing and during aggregation. The example below shows the use of basic arithmetic functions.
Convert waiting time from hours to seconds.
Note that we can assign this to a new column in the same DataFrame
dfwaiting_secs <- dfwaiting * 60 head(df)
eruptions waiting waiting_secs
##1 3.600 79 4740 ##2 1.800 54 3240 ##3 3.333 74 4440
{% endhighlight %}
Running SQL Queries from SparkR
A SparkR DataFrame can also be registered as a temporary table in Spark SQL and registering a DataFrame as a table allows you to run SQL queries over its data.
The sql
function enables applications to run SQL queries programmatically and returns the result as a DataFrame
.
Register this DataFrame as a table.
registerTempTable(people, "people")
SQL statements can be run by using the sql method
teenagers <- sql(sqlContext, "SELECT name FROM people WHERE age >= 13 AND age <= 19") head(teenagers)
name
##1 Justin
{% endhighlight %}
Machine Learning
SparkR supports the following Machine Learning algorithms.
- Generalized Linear Regression Model spark.glm()
- Naive Bayes spark.naiveBayes()
- KMeans spark.kmeans()
- AFT Survival Regression spark.survreg()
Generalized Linear Regression can be used to train a model from a specified family. Currently the Gaussian, Binomial, Poisson and Gamma families are supported. We support a subset of the available R formula operators for model fitting, including '~', '.', ':', '+', and '-'.
The summary() function gives the summary of a model produced by different algorithms listed above. It produces the similar result compared with R summary function.
Model persistence
- write.ml allows users to save a fitted model in a given input path
- read.ml allows users to read/load the model which was saved using write.ml in a given path
Model persistence is supported for all Machine Learning algorithms for all families.
The examples below show how to build several models:
- GLM using the Gaussian and Binomial model families
- AFT survival regression model
- Naive Bayes model
- K-Means model
{% include_example r/ml.R %}
R Function Name Conflicts
When loading and attaching a new package in R, it is possible to have a name conflict, where a function is masking another function.
The following functions are masked by the SparkR package:
Masked function | How to Access |
---|---|
cov in package:stats
|
|
filter in package:stats
|
|
sample in package:base
|
base::sample(x, size, replace = FALSE, prob = NULL) |
Since part of SparkR is modeled on the dplyr
package, certain functions in SparkR share the same names with those in dplyr
. Depending on the load order of the two packages, some functions from the package loaded first are masked by those in the package loaded after. In such case, prefix such calls with the package name, for instance, SparkR::cume_dist(x)
or dplyr::cume_dist(x)
.
You can inspect the search path in R with search()
Migration Guide
Upgrading From SparkR 1.5.x to 1.6.x
- Before Spark 1.6.0, the default mode for writes was
append
. It was changed in Spark 1.6.0 toerror
to match the Scala API. - SparkSQL converts
NA
in R tonull
and vice-versa.
Upgrading From SparkR 1.6.x to 2.0
- The method
table
has been removed and replaced bytableToDF
. - The class
DataFrame
has been renamed toSparkDataFrame
to avoid name conflicts. - The
sqlContext
parameter is no longer required for these functions:createDataFrame
,as.DataFrame
,read.json
,jsonFile
,read.parquet
,parquetFile
,read.text
,sql
,tables
,tableNames
,cacheTable
,uncacheTable
,clearCache
,dropTempTable
,read.df
,loadDF
,createExternalTable