Tutorial: An Introduc0on to SparkR Hao Lin (Purdue - - PowerPoint PPT Presentation

tutorial an introduc0on to sparkr
SMART_READER_LITE
LIVE PREVIEW

Tutorial: An Introduc0on to SparkR Hao Lin (Purdue - - PowerPoint PPT Presentation

Tutorial: An Introduc0on to SparkR Hao Lin (Purdue University) Morgantown, WV Jun 12, 2015 Part of the slides are modified from Shivarams slides


slide-1
SLIDE 1

Tutorial: ¡An ¡Introduc0on ¡to ¡SparkR ¡

Hao ¡Lin ¡(Purdue ¡University) ¡ Morgantown, ¡WV ¡ Jun ¡12, ¡2015 ¡

Part ¡of ¡the ¡slides ¡are ¡modified ¡from ¡Shivaram’s ¡slides ¡

slide-2
SLIDE 2

Outline ¡

Download ¡VMware ¡Player: ¡ hHps://my.vmware.com/web/vmware/free#desktop_end_user_compuQng/ vmware_player/7_0 ¡ ¡ Download ¡VM ¡image: ¡ ¡ hHp://web.ics.purdue.edu/~lin116/sparkr-­‑ubuntu-­‑1204-­‑interface15.zip ¡ ¡ Examples ¡& ¡Datasets: ¡ ¡ hHp://web.ics.purdue.edu/~lin116/examples.tar.gz ¡ hHp://web.ics.purdue.edu/~lin116/data.tar.gz ¡

¡

slide-3
SLIDE 3

Outline ¡

Overview ¡and ¡Environment ¡Setup ¡ SparkR ¡APIs ¡ New ¡DataFrame ¡APIs ¡ Basic ¡Examples ¡ More ¡Examples ¡ Q ¡& ¡A ¡

slide-4
SLIDE 4

Overview ¡

Fast ¡ Scalable ¡ Flexible ¡ StaQsQcal ¡ DataFrame ¡ Packages ¡ Plots ¡

slide-5
SLIDE 5

Overview ¡

HDFS ¡/ ¡HBase ¡/ ¡Cassandra ¡... ¡ Mesos ¡/ ¡YARN ¡ Spark ¡ SparkR ¡ Data ¡Storage ¡ Cluster ¡ Management ¡ Data ¡Processing ¡ Engine ¡ Data ¡Science ¡ Interface ¡

slide-6
SLIDE 6

Environment ¡Setup ¡

Install ¡R ¡& ¡RStudio: ¡hHp://cran.r-­‑project.org/, ¡hHp://www.rstudio.com/products/RStudio/ ¡ Java ¡(Scala) ¡& ¡Maven: ¡Java ¡6+, ¡Maven ¡3.0.4+ ¡ Install ¡Spark ¡

For ¡latest ¡Spark ¡1.4, ¡SparkR ¡is ¡in ¡the ¡release ¡hHps://github.com/apache/spark ¡ ¡build ¡SparkR ¡by ¡Maven, ¡following ¡hHps://github.com/apache/spark/tree/master/R ¡ For ¡Spark ¡1.3 ¡or ¡maybe ¡before: ¡download ¡Spark ¡from ¡hHps://spark.apache.org/downloads.html ¡ Also ¡install ¡SparkR ¡package ¡in ¡hHps://github.com/amplab-­‑extras/SparkR-­‑pkg ¡

Other ¡packages ¡like ¡HDFS ¡if ¡necessary ¡

slide-7
SLIDE 7

Environment ¡Setup ¡

Docker: ¡hHps://registry.hub.docker.com/u/beniyama/sparkr-­‑docker ¡ Amazon ¡EC2: ¡hHps://github.com/amplab-­‑extras/SparkR-­‑pkg/wiki/SparkR-­‑on-­‑EC2 ¡ MulQple ¡node ¡(Distributed ¡mode) ¡with ¡HDFS ¡ ¡Install ¡Hadoop: ¡hHp://goo.gl/OXt1mC ¡

¡Spark ¡Standalone: ¡hHps://spark.apache.org/docs/latest/spark-­‑standalone.html ¡ ¡YARN: ¡hHps://spark.apache.org/docs/latest/running-­‑on-­‑yarn.html ¡ ¡

slide-8
SLIDE 8

Quick ¡Start ¡

Start ¡RStudio ¡ ¡Desktop ¡version: ¡start ¡RStudio ¡applicaQon ¡

¡Server ¡version: ¡open ¡web ¡browser ¡with ¡<your ¡host>:8787 ¡

Spark ¡Context ¡IniQalizaQon ¡

¡Setup ¡SPARK_HOME: ¡alternaQvely ¡we ¡can ¡store ¡in ¡~/.Renviron ¡so ¡that ¡it ¡will ¡not ¡execute ¡every ¡Qme ¡

¡ ¡# ¡Set ¡this ¡to ¡where ¡Spark ¡is ¡installed ¡

¡ ¡Sys.setenv(SPARK_HOME="/home/sparkr/workspace/spark") ¡ ¡ ¡# ¡This ¡line ¡loads ¡SparkR ¡from ¡the ¡installed ¡directory, ¡ ¡ ¡ ¡.libPaths(c(file.path(Sys.getenv("SPARK_HOME"), ¡"R", ¡"lib"), ¡.libPaths())) ¡ ¡Load ¡SparkR ¡package: ¡library(SparkR) ¡ ¡Init ¡Spark ¡Context: ¡ ¡sc ¡<-­‑ ¡sparkR.init(master="local[n]") ¡

¡

¡

slide-9
SLIDE 9

Quick ¡Start ¡

Also, ¡you ¡can ¡always ¡use ¡terminal ¡ ¡For ¡Spark ¡1.4: ¡cd ¡$SPARK_HOME; ¡./bin/sparkR ¡-­‑-­‑master ¡“local[n]” ¡

¡For ¡Spark ¡1.3 ¡with ¡SparkR-­‑pkg: ¡cd ¡SparkR-­‑pkg; ¡./sparkR ¡-­‑-­‑master ¡“local[n]” ¡

¡Spark ¡Context ¡will ¡automaQcally ¡be ¡created, ¡call ¡sc ¡ ¡

slide-10
SLIDE 10

Spark ¡Distributed ¡Dataset ¡(RDD) ¡APIs ¡

slide-11
SLIDE 11

SparkR ¡R-­‑RDD ¡APIs ¡

Considered ¡as ¡Distributed ¡version ¡of ¡R ¡List ¡ ¡ GeneraQon ¡funcQons: ¡textFile, ¡parallelize, ¡... ¡ TransformaQon ¡funcQons: ¡lapply, ¡filter, ¡sampleRDD, ¡... ¡ persistence ¡funcQon: ¡cache, ¡persist, ¡... ¡ AcQon ¡funcQons: ¡reduce, ¡collect, ¡... ¡ Paired ¡Value ¡Shuffle ¡funcQons: ¡groupByKey, ¡reduceByKey, ¡... ¡ Binary ¡FuncQons: ¡unionRDD, ¡cogroup, ¡... ¡ Output ¡FuncQons: ¡saveAsTextFile, ¡saveAsObjectFile, ¡... ¡

slide-12
SLIDE 12

Data ¡Frames ¡

More ¡structured ¡data ¡in ¡Tables ¡ ¡Data ¡source ¡like ¡CSV, ¡JSON, ¡JDBC, ¡… ¡ ¡ Want ¡to ¡use ¡your ¡favorite ¡package ¡“dplyr” ¡? ¡DataFrame ¡type ¡in ¡SparkR ¡ ¡ ¡ Embeded ¡SQL ¡in ¡R ¡

slide-13
SLIDE 13

DataFrame ¡APIs ¡

Filter ¡-­‑-­‑ ¡Select ¡some ¡rows ¡ ¡filter(df, ¡df$col1 ¡> ¡0) ¡ Project ¡-­‑-­‑ ¡Select ¡some ¡columns ¡ ¡df$col1 ¡or ¡df[“col”] ¡

slide-14
SLIDE 14

DataFrame ¡APIs ¡

Aggregate ¡-­‑-­‑ ¡Group ¡and ¡Summarize ¡data ¡

groupDF ¡<-­‑ ¡groupBy(df, ¡df$col1) ¡ agg(groupDF, ¡sum(groupDF$col2), ¡max(groupDF$col3)) ¡

Sort ¡-­‑-­‑ ¡Sort ¡data ¡by ¡a ¡parQcular ¡column ¡

sortDF(df, ¡asc(df$col1)) ¡

slide-15
SLIDE 15

Column ¡Average ¡using ¡RDD ¡

peopleRDD ¡<-­‑ ¡textFile(sc, ¡“people.txt”) ¡ ¡ lines ¡<-­‑ ¡flatMap(peopleRDD, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡function(line) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡strsplit(line, ¡", ¡") ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡}) ¡ ¡ ageInt ¡<-­‑ ¡lapply(lines, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡function(line) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡as.numeric(line[2]) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡}) ¡ ¡ sum ¡<-­‑ ¡reduce(ageInt, ¡function(x, ¡y) ¡{x+y}) ¡ avg ¡<-­‑ ¡sum ¡/ ¡count(peopleRDD) ¡

¡

slide-16
SLIDE 16

Column ¡Average ¡using ¡DataFrame ¡

# ¡JSON ¡File ¡contains ¡two ¡columns ¡age, ¡name ¡ df ¡<-­‑ ¡jsonFile(“people.json”) ¡ ¡ avg ¡<-­‑ ¡select(df, ¡avg(df$age)) ¡

slide-17
SLIDE 17

Pi Example

slide-18
SLIDE 18

Logistic Regression

slide-19
SLIDE 19

Predicting Customer Behavior

Demo ¡from ¡Chris ¡Freeman ¡from ¡Alteryx ¡ ¡ 3 ¡datasets: ¡

¡TransacQons ¡ ¡Demographic ¡Info ¡Per ¡Customer ¡ ¡DM ¡Treatment ¡Sample ¡

How ¡do ¡we ¡decide ¡who ¡to ¡send ¡the ¡offer ¡to? ¡ ¡

slide-20
SLIDE 20

Predicting Customer Behavior

Demo ¡from ¡Chris ¡Freeman ¡from ¡Alteryx ¡

¡

Use ¡the ¡DataFrame ¡API ¡to ¡load, ¡prepare ¡and ¡combine ¡all ¡3 ¡ datasets ¡and ¡create ¡training ¡and ¡esQmaQon ¡sets. ¡ ¡ Use ¡R’s ¡glm ¡method ¡to ¡train ¡a ¡logisQc ¡regression ¡model ¡on ¡the ¡ treatment ¡sample ¡ ¡ Profit! ¡

¡

slide-21
SLIDE 21

Reference and Guide

Starter & RDD: https://github.com/amplab-extras/SparkR-pkg/wiki/SparkR- Quick-Start Data Frame: http://people.apache.org/~pwendell/spark-releases/latest/ sparkr.html Chris’s demo: https://github.com/cafreeman/Demo_SparkR

slide-22
SLIDE 22

Ques0ons? ¡ Ques0ons? ¡

hHps://github.com/apache/spark hHps://github.com/apache/spark ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡hHps://github.com/amplab-­‑extras/SparkR-­‑pkg ¡ ¡ ¡