interface 2015 tutorial- sparkr · tutorial:*an*introduc0on*to*sparkr* hao$lin$(purdue$university)$...

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

Upload: others

Post on 23-Jun-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Interface 2015 Tutorial- SparkR · Tutorial:*An*Introduc0on*to*SparkR* Hao$Lin$(Purdue$University)$ Morgantown,$WV$ Jun$12,$2015$ Part$of$the$slides$are$modified$from$Shivaram’s$slides$

Tutorial:*An*Introduc0on*to*SparkR*

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

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

Page 2: Interface 2015 Tutorial- SparkR · Tutorial:*An*Introduc0on*to*SparkR* Hao$Lin$(Purdue$University)$ Morgantown,$WV$ Jun$12,$2015$ Part$of$the$slides$are$modified$from$Shivaram’s$slides$

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/sparkrUubuntuU1204Uinterface15.zip$$Examples$&$Datasets:$$hHp://web.ics.purdue.edu/~lin116/examples.tar.gz$hHp://web.ics.purdue.edu/~lin116/data.tar.gz$$

Page 3: Interface 2015 Tutorial- SparkR · Tutorial:*An*Introduc0on*to*SparkR* Hao$Lin$(Purdue$University)$ Morgantown,$WV$ Jun$12,$2015$ Part$of$the$slides$are$modified$from$Shivaram’s$slides$

Outline*Overview$and$Environment$Setup$SparkR$APIs$New$DataFrame$APIs$Basic$Examples$More$Examples$Q$&$A$

Page 4: Interface 2015 Tutorial- SparkR · Tutorial:*An*Introduc0on*to*SparkR* Hao$Lin$(Purdue$University)$ Morgantown,$WV$ Jun$12,$2015$ Part$of$the$slides$are$modified$from$Shivaram’s$slides$

Overview*

Fast$

Scalable$

Flexible$

StaQsQcal$

DataFrame$

Packages$Plots$

Page 5: Interface 2015 Tutorial- SparkR · Tutorial:*An*Introduc0on*to*SparkR* Hao$Lin$(Purdue$University)$ Morgantown,$WV$ Jun$12,$2015$ Part$of$the$slides$are$modified$from$Shivaram’s$slides$

Overview*

HDFS$/$HBase$/$Cassandra$...$

Mesos$/$YARN$

Spark$

SparkR$

Data$Storage$

Cluster$Management$

Data$Processing$Engine$

Data$Science$Interface$

Page 6: Interface 2015 Tutorial- SparkR · Tutorial:*An*Introduc0on*to*SparkR* Hao$Lin$(Purdue$University)$ Morgantown,$WV$ Jun$12,$2015$ Part$of$the$slides$are$modified$from$Shivaram’s$slides$

Environment*Setup*Install$R$&$RStudio:$hHp://cran.rUproject.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/amplabUextras/SparkRUpkg$

Other$packages$like$HDFS$if$necessary$

Page 7: Interface 2015 Tutorial- SparkR · Tutorial:*An*Introduc0on*to*SparkR* Hao$Lin$(Purdue$University)$ Morgantown,$WV$ Jun$12,$2015$ Part$of$the$slides$are$modified$from$Shivaram’s$slides$

Environment*Setup*Docker:$hHps://registry.hub.docker.com/u/beniyama/sparkrUdocker$

Amazon$EC2:$hHps://github.com/amplabUextras/SparkRUpkg/wiki/SparkRUonUEC2$

MulQple$node$(Distributed$mode)$with$HDFS$$Install$Hadoop:$hHp://goo.gl/OXt1mC$$Spark$Standalone:$hHps://spark.apache.org/docs/latest/sparkUstandalone.html$

$YARN:$hHps://spark.apache.org/docs/latest/runningUonUyarn.html$$

Page 8: Interface 2015 Tutorial- SparkR · Tutorial:*An*Introduc0on*to*SparkR* Hao$Lin$(Purdue$University)$ Morgantown,$WV$ Jun$12,$2015$ Part$of$the$slides$are$modified$from$Shivaram’s$slides$

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&<P&sparkR.init(master="local[n]")&

$$

Page 9: Interface 2015 Tutorial- SparkR · Tutorial:*An*Introduc0on*to*SparkR* Hao$Lin$(Purdue$University)$ Morgantown,$WV$ Jun$12,$2015$ Part$of$the$slides$are$modified$from$Shivaram’s$slides$

Quick*Start*Also,$you$can$always$use$terminal$

$For$Spark$1.4:$cd&$SPARK_HOME;&./bin/sparkR&PPmaster&“local[n]”&$For$Spark$1.3$with$SparkRUpkg:$cd&SparkRPpkg;&./sparkR&PPmaster&“local[n]”&&Spark$Context$will$automaQcally$be$created,$call$sc&

$

Page 10: Interface 2015 Tutorial- SparkR · Tutorial:*An*Introduc0on*to*SparkR* Hao$Lin$(Purdue$University)$ Morgantown,$WV$ Jun$12,$2015$ Part$of$the$slides$are$modified$from$Shivaram’s$slides$

Spark*Distributed*Dataset*(RDD)*APIs*

Page 11: Interface 2015 Tutorial- SparkR · Tutorial:*An*Introduc0on*to*SparkR* Hao$Lin$(Purdue$University)$ Morgantown,$WV$ Jun$12,$2015$ Part$of$the$slides$are$modified$from$Shivaram’s$slides$

SparkR*RBRDD*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,&...&

Page 12: Interface 2015 Tutorial- SparkR · Tutorial:*An*Introduc0on*to*SparkR* Hao$Lin$(Purdue$University)$ Morgantown,$WV$ Jun$12,$2015$ Part$of$the$slides$are$modified$from$Shivaram’s$slides$

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$

Page 13: Interface 2015 Tutorial- SparkR · Tutorial:*An*Introduc0on*to*SparkR* Hao$Lin$(Purdue$University)$ Morgantown,$WV$ Jun$12,$2015$ Part$of$the$slides$are$modified$from$Shivaram’s$slides$

DataFrame*APIs*Filter$UU$Select$some$rows$

&filter(df,&df$col1&>&0)&

Project$UU$Select$some$columns$

&df$col1&or&df[“col”]&

Page 14: Interface 2015 Tutorial- SparkR · Tutorial:*An*Introduc0on*to*SparkR* Hao$Lin$(Purdue$University)$ Morgantown,$WV$ Jun$12,$2015$ Part$of$the$slides$are$modified$from$Shivaram’s$slides$

DataFrame*APIs*Aggregate$UU$Group$and$Summarize$data$

groupDF&<P&groupBy(df,&df$col1)&

agg(groupDF,&sum(groupDF$col2),&max(groupDF$col3))&

Sort$UU$Sort$data$by$a$parQcular$column$

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

Page 15: Interface 2015 Tutorial- SparkR · Tutorial:*An*Introduc0on*to*SparkR* Hao$Lin$(Purdue$University)$ Morgantown,$WV$ Jun$12,$2015$ Part$of$the$slides$are$modified$from$Shivaram’s$slides$

Column*Average*using*RDD*peopleRDD&<P&textFile(sc,&“people.txt”)&&lines&<P&flatMap(peopleRDD,&&&&&&&&&&&&&&&&&&function(line)&{&&&&&&&&&&&&&&&&&&&&strsplit(line,&",&")&&&&&&&&&&&&&&&&&&})&&ageInt&<P&lapply(lines,&&&&&&&&&&&&&&&&&&function(line)&{&&&&&&&&&&&&&&&&&&&&as.numeric(line[2])&&&&&&&&&&&&&&&&&&})&&sum&<P&reduce(ageInt,&function(x,&y)&{x+y})&avg&<P&sum&/&count(peopleRDD)&

&

Page 16: Interface 2015 Tutorial- SparkR · Tutorial:*An*Introduc0on*to*SparkR* Hao$Lin$(Purdue$University)$ Morgantown,$WV$ Jun$12,$2015$ Part$of$the$slides$are$modified$from$Shivaram’s$slides$

Column*Average*using*DataFrame*#&JSON&File&contains&two&columns&age,&name&df&<P&jsonFile(“people.json”)&&avg&<P&select(df,&avg(df$age))&

Page 17: Interface 2015 Tutorial- SparkR · Tutorial:*An*Introduc0on*to*SparkR* Hao$Lin$(Purdue$University)$ Morgantown,$WV$ Jun$12,$2015$ Part$of$the$slides$are$modified$from$Shivaram’s$slides$

Pi Example

Page 18: Interface 2015 Tutorial- SparkR · Tutorial:*An*Introduc0on*to*SparkR* Hao$Lin$(Purdue$University)$ Morgantown,$WV$ Jun$12,$2015$ Part$of$the$slides$are$modified$from$Shivaram’s$slides$

Logistic Regression

Page 19: Interface 2015 Tutorial- SparkR · Tutorial:*An*Introduc0on*to*SparkR* Hao$Lin$(Purdue$University)$ Morgantown,$WV$ Jun$12,$2015$ Part$of$the$slides$are$modified$from$Shivaram’s$slides$

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?$$

Page 20: Interface 2015 Tutorial- SparkR · Tutorial:*An*Introduc0on*to*SparkR* Hao$Lin$(Purdue$University)$ Morgantown,$WV$ Jun$12,$2015$ Part$of$the$slides$are$modified$from$Shivaram’s$slides$

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!$$

Page 21: Interface 2015 Tutorial- SparkR · Tutorial:*An*Introduc0on*to*SparkR* Hao$Lin$(Purdue$University)$ Morgantown,$WV$ Jun$12,$2015$ Part$of$the$slides$are$modified$from$Shivaram’s$slides$

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

Page 22: Interface 2015 Tutorial- SparkR · Tutorial:*An*Introduc0on*to*SparkR* Hao$Lin$(Purdue$University)$ Morgantown,$WV$ Jun$12,$2015$ Part$of$the$slides$are$modified$from$Shivaram’s$slides$

Ques0ons?*Ques0ons?*

hHps://github.com/apache/sparkhHps://github.com/apache/spark$$$$$$$$$$$$$$$$$$hHps://github.com/amplabUextras/SparkRUpkg$

$$