<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/sparklyr-r-interface-for-apache-spark/"></div></div><div><a data-url="https://www.r-bloggers.com/sparklyr-r-interface-for-apache-spark/" data-counturl="https://www.r-bloggers.com/sparklyr-r-interface-for-apache-spark/" data-count="horizontal" data-text="sparklyr — R interface for Apache Spark" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://blog.rstudio.org/2016/09/27/sparklyr-r-interface-for-apache-spark/"> RStudio Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>We’re excited today to announce <a target="_blank" href="http://spark.rstudio.com/">sparklyr</a>, a new package that provides an interface between R and <a target="_blank" href="http://spark.apache.org/">Apache Spark</a>.</p>
<p><img height="276" alt="" width="510" src="https://i2.wp.com/spark.rstudio.com/images/sparklyr-illustration.png"></p>
<p>Over the past couple of years we’ve heard time and time again that people want a native <a target="_blank" href="https://github.com/hadley/dplyr">dplyr</a> interface to Spark, so we built one! sparklyr also provides interfaces to Spark’s distributed machine learning algorithms and much more. Highlights include:</p>
<div>
<ul>
<li>Interactively manipulate Spark data using both <a target="_blank" href="https://github.com/hadley/dplyr">dplyr</a> and SQL (via DBI).</li>
<li>Filter and aggregate Spark datasets then bring them into R for analysis and visualization.</li>
<li>Orchestrate distributed machine learning from R using either <a target="_blank" href="http://spark.rstudio.com/mllib.html">Spark MLlib</a> or <a target="_blank" href="http://spark.rstudio.com/h2o.html">H2O SparkingWater</a>.</li>
<li>Create <a target="_blank" href="http://spark.rstudio.com/extensions.html">extensions</a> that call the full Spark API and provide interfaces to Spark packages.</li>
<li>Integrated support for establishing Spark connections and browsing Spark data frames within the RStudio IDE.</li>
</ul>
</div>
<div>
<p>We’re also excited to be working with several industry partners. <a target="_blank" href="http://www.ibm.com/">IBM</a> is incorporating sparklyr into their Data Science Experience, <a target="_blank" href="http://www.cloudera.com/">Cloudera</a> is working with us to ensure that sparklyr meets the requirements of their enterprise customers, and <a target="_blank" href="http://www.h2o.ai/">H2O</a> has provided an integration between sparklyr and H2O Sparkling Water.</p>
<h2>Getting Started</h2>
<div>
<p>You can install sparklyr from CRAN as follows:</p>
<pre><code><span>install.packages</span><span>(</span><span>&quot;sparklyr&quot;</span><span>)</span>
</code></pre>
<p>You should also install a local version of Spark for development purposes:</p>
<pre><code><span>library</span><span>(</span><span>sparklyr</span><span>)</span>
<span>spark_install</span><span>(</span><span>version</span> <span>=</span> <span>&quot;1.6.2&quot;</span><span>)</span></code></pre>
<p>If you use the RStudio IDE, you should also download the latest <a target="_blank" href="https://www.rstudio.com/products/rstudio/download/preview/">preview release</a> of the IDE which includes several enhancements for interacting with Spark.</p>
<p>Extensive documentation and examples are available at <a target="_blank" href="http://spark.rstudio.com/">http://spark.rstudio.com</a>.</p>
</div>
</div>
<h2>Connecting to Spark</h2>
<div>
<div>
<p>You can connect to both local instances of Spark as well as remote Spark clusters. Here we’ll connect to a local instance of Spark:</p>
<pre><code><span>library</span><span>(</span><span>sparklyr</span><span>)</span>
<span>sc</span> <span>&lt;-</span> <span>spark_connect</span><span>(</span><span>master</span> <span>=</span> <span>&quot;local&quot;</span><span>)</span></code></pre>
<p>The returned Spark connection (<code>sc</code>) provides a remote dplyr data source to the Spark cluster.</p>
<h2>Reading Data</h2>
</div>
<div>
<p>You can copy R data frames into Spark using the dplyr copy_to function (more typically though you’ll read data within the Spark cluster using the <a target="_blank" href="http://spark.rstudio.com/dplyr.html#reading_data">spark_read</a> family of functions). For the examples below we’ll copy some datasets from R into Spark (note that you may need to install the nycflights13 and Lahman packages in order to execute this code):</p>
<pre><code><span>library</span><span>(</span><span>dplyr</span><span>)</span>
<span>iris_tbl</span> <span>&lt;-</span> <span>copy_to</span><span>(</span><span>sc</span>, <span>iris</span><span>)</span>
<span>flights_tbl</span> <span>&lt;-</span> <span>copy_to</span><span>(</span><span>sc</span>, <span>nycflights13</span><span>:</span><span>:</span><span>flights</span>, <span>&quot;flights&quot;</span><span>)</span>
<span>batting_tbl</span> <span>&lt;-</span> <span>copy_to</span><span>(</span><span>sc</span>, <span>Lahman</span><span>:</span><span>:</span><span>Batting</span>, <span>&quot;batting&quot;</span><span>)</span></code></pre>
</div>
<div>
<h2>Using dplyr</h2>
<p>We can now use all of the available dplyr verbs against the tables within the cluster. Here’s a simple filtering example:</p>
<pre><code># filter by departure delay
<span>flights_tbl</span> <span>%>%</span> <span>filter</span><span>(</span><span>dep_delay</span> <span>==</span> <span>2</span><span>)</span></code></pre>
<p><a target="_blank" href="https://cran.rstudio.com/web/packages/dplyr/vignettes/introduction.html">Introduction to dplyr</a> provides additional dplyr examples you can try. For example, consider the last example from the tutorial which plots data on flight delays:</p>
<pre><code><span>delay</span> <span>&lt;-</span> <span>flights_tbl</span> <span>%>%</span> 
  <span>group_by</span><span>(</span><span>tailnum</span><span>)</span> <span>%>%</span>
  <span>summarise</span><span>(</span><span>count</span> <span>=</span> <span>n</span><span>(</span><span>)</span>, <span>dist</span> <span>=</span> <span>mean</span><span>(</span><span>distance</span><span>)</span>, <span>delay</span> <span>=</span> <span>mean</span><span>(</span><span>arr_delay</span><span>)</span><span>)</span> <span>%>%</span>
  <span>filter</span><span>(</span><span>count</span> <span>></span> <span>20</span>, <span>dist</span> <span>&lt;</span> <span>2000</span>, <span>!</span><span>is.na</span><span>(</span><span>delay</span><span>)</span><span>)</span> <span>%>%</span>
  <span>collect</span><span>(</span><span>)</span>

# plot delays
<span>library</span><span>(</span><span>ggplot2</span><span>)</span>
<span>ggplot</span><span>(</span><span>delay</span>, <span>aes</span><span>(</span><span>dist</span>, <span>delay</span><span>)</span><span>)</span> <span>+</span>
  <span>geom_point</span><span>(</span><span>aes</span><span>(</span><span>size</span> <span>=</span> <span>count</span><span>)</span>, <span>alpha</span> <span>=</span> <span>1</span><span>/</span><span>2</span><span>)</span> <span>+</span>
  <span>geom_smooth</span><span>(</span><span>)</span> <span>+</span>
  <span>scale_size_area</span><span>(</span><span>max_size</span> <span>=</span> <span>2</span><span>)</span></code></pre>
<div><img height="287" width="516" src="https://i2.wp.com/spark.rstudio.com/images/ggplot2-flights.png"></div>
</div>
<div>Note that while the dplyr functions shown above look identical to the ones you use with R data frames, with sparklyr they use Spark as their back end and execute remotely in the cluster.</div>
<div>
<h3>Window Functions</h3>
<div>
<p>dplyr <a target="_blank" href="https://cran.r-project.org/web/packages/dplyr/vignettes/window-functions.html">window functions</a> are also supported, for example:</p>
<pre><code><span>batting_tbl</span> <span>%>%</span>
  <span>select</span><span>(</span><span>playerID</span>, <span>yearID</span>, <span>teamID</span>, <span>G</span>, <span>AB</span><span>:</span><span>H</span><span>)</span> <span>%>%</span>
  <span>arrange</span><span>(</span><span>playerID</span>, <span>yearID</span>, <span>teamID</span><span>)</span> <span>%>%</span>
  <span>group_by</span><span>(</span><span>playerID</span><span>)</span> <span>%>%</span>
  <span>filter</span><span>(</span><span>min_rank</span><span>(</span><span>desc</span><span>(</span><span>H</span><span>)</span><span>)</span> <span>&lt;=</span> <span>2</span> <span>&amp;</span> <span>H</span> <span>></span> <span>0</span><span>)</span></code></pre>
<p>For additional documentation on using dplyr with Spark see the <a target="_blank" href="http://spark.rstudio.com/dplyr.html">dplyr</a> section of the sparklyr website.</p>
<h2>Using SQL</h2>
</div>
</div>
<div>
<p>It’s also possible to execute SQL queries directly against tables within a Spark cluster. The <code>spark_connection</code> object implements a <a target="_blank" href="https://github.com/rstats-db/DBI">DBI</a> interface for Spark, so you can use <code>dbGetQuery</code> to execute SQL and return the result as an R data frame:</p>
<pre><code><span>library</span><span>(</span><span>DBI</span><span>)</span>
<span>iris_preview</span> <span>&lt;-</span> <span>dbGetQuery</span><span>(</span><span>sc</span>, <span>&quot;SELECT * FROM iris LIMIT 10&quot;</span><span>)</span>
</code></pre>
</div>
</div>
<h2>Machine Learning</h2>
<p>You can orchestrate machine learning algorithms in a Spark cluster via either <a target="_blank" href="http://spark.rstudio.org/mllib.html">Spark MLlib</a> or via the <a target="_blank" href="http://spark.rstudio.org/h2o.html">H2O Sparkling Water</a> extension package. Both provide a set of high-level APIs built on top of DataFrames that help you create and tune machine learning workflows.</p>
<h3>Spark MLlib</h3>
<div>
<div>
<p>In this example we’ll use ml_linear_regression to fit a linear regression model. We’ll use the built-in <code>mtcars</code> dataset, and see if we can predict a car’s fuel consumption (<code>mpg</code>) based on its weight (<code>wt</code>) and the number of cylinders the engine contains (<code>cyl</code>). We’ll assume in each case that the relationship between <code>mpg</code> and each of our features is linear.</p>
<pre><code># copy mtcars into spark
<span>mtcars_tbl</span> <span>&lt;-</span> <span>copy_to</span><span>(</span><span>sc</span>, <span>mtcars</span><span>)</span>

# transform our data set, and then partition into 'training', 'test'
<span>partitions</span> <span>&lt;-</span> <span>mtcars_tbl</span> <span>%>%</span>
  <span>filter</span><span>(</span><span>hp</span> <span>>=</span> <span>100</span><span>)</span> <span>%>%</span>
  <span>mutate</span><span>(</span><span>cyl8</span> <span>=</span> <span>cyl</span> <span>==</span> <span>8</span><span>)</span> <span>%>%</span>
  <span>sdf_partition</span><span>(</span><span>training</span> <span>=</span> <span>0.5</span>, <span>test</span> <span>=</span> <span>0.5</span>, <span>seed</span> <span>=</span> <span>1099</span><span>)</span>

# fit a linear model to the training dataset
<span>fit</span> <span>&lt;-</span> <span>partitions</span><span>$</span><span>training</span> <span>%>%</span>
  <span>ml_linear_regression</span><span>(</span><span>response</span> <span>=</span> <span>&quot;mpg&quot;</span>, <span>features</span> <span>=</span> <span>c</span><span>(</span><span>&quot;wt&quot;</span>, <span>&quot;cyl&quot;</span><span>)</span><span>)</span></code></pre>
<p>For linear regression models produced by Spark, we can use <code>summary()</code> to learn a bit more about the quality of our fit, and the statistical significance of each of our predictors.</p>
<pre><code><span>summary</span><span>(</span><span>fit</span><span>)</span></code></pre>
<p>Spark machine learning supports a wide array of algorithms and feature transformations, and as illustrated above it’s easy to chain these functions together with dplyr pipelines. To learn more see the <a target="_blank" href="http://spark.rstudio.com/mllib.html">Spark MLlib</a> section of the sparklyr website.</p>
</div>
<h3>H2O Sparkling Water</h3>
<p>Let’s walk the same <code>mtcars</code> example, but in this case use H2O’s machine learning algorithms via the <a target="_blank" href="http://spark.rstudio.com/h2o.html">H2O Sparkling Water</a> extension. The dplyr code used to prepare the data is the same, but after partitioning into test and training data we call <code>h2o.glm</code> rather than <code>ml_linear_regression</code>:</p>
<pre><code># convert to h20_frame (uses the same underlying rdd)
training <span>&lt;-</span> as_h2o_frame<span>(</span>partitions<span>$</span>training<span>)
</span>test <span>&lt;-</span> as_h2o_frame<span>(</span>partitions<span>$</span>test<span>)
# fit a linear model to the training dataset
fit <span>&lt;-</span> h2o.glm<span>(</span>x <span>=</span> c<span>(</span><span>&quot;wt&quot;</span><span>, </span><span>&quot;cyl&quot;</span><span>)</span><span>,</span>
y = &quot;mpg&quot;,
training_frame = training,
lamda_search = TRUE)
# inspect the model
print(fit)</span></code></pre>
<p>For linear regression models produced by H2O, we can use either <code>print()</code> or <code>summary()</code> to learn a bit more about the quality of our fit. The <code>summary()</code> method returns some extra information about scoring history and variable importance.</p>
<p>To learn more see the <a target="_blank" href="http://spark.rstudio.com/h2o.html">H2O Sparkling Water</a> section of the sparklyr website.</p>
<h2>Extensions</h2>
<p>The facilities used internally by sparklyr for its dplyr and machine learning interfaces are available to extension packages. Since Spark is a general purpose cluster computing system there are many potential applications for extensions (e.g. interfaces to custom machine learning pipelines, interfaces to 3rd party Spark packages, etc.).</p>
<p>The <a target="_blank" href="https://github.com/bnosac/spark.sas7bdat">sas7bdat</a> extension enables parallel reading of SAS datasets in the sas7bdat format into Spark data frames. The <a target="_blank" href="http://spark.rstudio.com/h2o.html">rsparkling</a> extension provides a bridge between sparklyr and H2O’s <a target="_blank" href="http://www.h2o.ai/product/sparkling-water/">Sparkling Water</a>.</p>
<p>We’re excited to see what other sparklyr extensions the R community creates. To learn more see the <a target="_blank" href="http://spark.rstudio.com/extensions.html">Extensions</a> section of the sparklyr website.</p>
</div>
<div>
<h2>RStudio IDE</h2>
<p>The latest RStudio <a target="_blank" href="https://www.rstudio.com/products/rstudio/download/preview/">Preview Release</a> of the RStudio IDE includes integrated support for Spark and the sparklyr package, including tools for:</p>
<div>
<ul>
<li>Creating and managing Spark connections</li>
<li>Browsing the tables and columns of Spark DataFrames</li>
<li>Previewing the first 1,000 rows of Spark DataFrames</li>
</ul>
<p>Once you’ve installed the sparklyr package, you should find a new <strong>Spark</strong> pane within the IDE. This pane includes a <strong>New Connection</strong> dialog which can be used to make connections to local or remote Spark instances:</p>
<p><img height="361" width="516" src="https://i2.wp.com/spark.rstudio.com/images/spark-connect.png"></p>
<p>Once you’ve connected to Spark you’ll be able to browse the tables contained within the Spark cluster:</p>
<p><img height="318" width="517" src="https://i0.wp.com/spark.rstudio.com/images/spark-tab.png"></p>
<p>The Spark DataFrame preview uses the standard RStudio data viewer:</p>
<p><img height="360" width="516" src="https://i1.wp.com/spark.rstudio.com/images/spark-dataview.png"></p>
<p>The RStudio IDE features for sparklyr are available now as part of the <a target="_blank" href="https://www.rstudio.com/products/rstudio/download/preview/">RStudio Preview Release</a>. The final version of RStudio IDE that includes integrated support for sparklyr will ship within the next few weeks.</p>
<h2>Partners</h2>
</div>
</div>
<p>We’re very pleased to be joined in this announcement by IBM, Cloudera, and H2O, who are working with us to ensure that sparklyr meets the requirements of enterprise customers and is easy to integrate with current and future deployments of Spark.</p>
<h3>IBM</h3>
<p><span>“With our latest contributions to Apache Spark and the release of sparklyr, we continue to emphasize R as a primary data science language within the Spark community. Additionally, we are making plans to include sparklyr in Data Science Experience to provide the tools data scientists are comfortable with to help them bring business-changing insights to their companies faster,” said Ritika Gunnar, vice president of Offering Management, IBM Analytics.</span></p>
<h3>Cloudera</h3>
<p>“At Cloudera, data science is one of the most popular use cases we see for Apache Spark as a core part of the Apache Hadoop ecosystem, yet the lack of a compelling R experience has limited data scientists’ access to available data and compute,” said Charles Zedlewski, vice president, Products at Cloudera. “We are excited to partner with RStudio to help bring sparklyr to the enterprise, so that data scientists and IT teams alike can get more value from their existing skills and infrastructure, all with the security, governance, and management our customers expect.”</p>
<h3>H2O</h3>
<div>
<p>“At H2O.ai, we’ve been focused on bringing the best of breed open source machine learning to data scientists working in R &amp; Python. However, the lack of robust tooling in the R ecosystem for interfacing with Apache Spark has made it difficult for the R community to take advantage of the distributed data processing capabilities of Apache Spark.</p>
<p>We’re excited to work with RStudio to bring the ease of use of dplyr and the distributed machine learning algorithms from H2O’s Sparkling Water to the R community via the sparklyr &amp; rsparkling packages”</p>
</div>
<div></div>
<div></div>
<div></div>
<div></div>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rstudioblog.wordpress.com/3154/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/rstudioblog.wordpress.com/3154/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/sparklyr-r-interface-for-apache-spark/"></div></div><div><a data-url="https://www.r-bloggers.com/sparklyr-r-interface-for-apache-spark/" data-counturl="https://www.r-bloggers.com/sparklyr-r-interface-for-apache-spark/" data-count="vertical" data-text="sparklyr — R interface for Apache Spark" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://blog.rstudio.org/2016/09/27/sparklyr-r-interface-for-apache-spark/"> RStudio Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/lvmmzxYuc1M"></a></div>