-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial.Rmd
More file actions
484 lines (337 loc) · 8.92 KB
/
tutorial.Rmd
File metadata and controls
484 lines (337 loc) · 8.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
---
title: "Tutorial SparkR"
authors: "Maria Pelaez & Bartek Skorulski"
output:
html_document:
toc: true
highlight: zenburn
---
# Introduction
This is a short and simple tutorial of SparkR. We
introduce SparkR and we show few simple examples.
More information you can find on the following pages.
- <https://spark.apache.org/docs/latest/api/R/index.html>
- <http://rmarkdown.rstudio.com>.
- <http://spark.apache.org>
- <http://www.r-project.org>
- <http://www.ggplot2.org>
- <https://cran.r-project.org/web/packages/pipeR>
- <https://spark.apache.org/docs/latest/api/R/index.html>
## What is R?
R is a __programming language__ that is very popular among Data
Scientist (_better Computer Scientist than an average Statistician and
better Statistician than an average Computer Scientist_).
### Magic of R
* Open Source
* Rich of statistical, graphics and general packages
* One can manipulate R objects directly in C, C++, Fortran, Java
* It can produce publication quality documents
### Limitations of R
* Single threaded
* Everything has to fit in memory
### How do Data Scientists work with R?
<center>
<img src="images/pic2.png" height="200px"/>
</center>
- Distributed Storages: Hadoop, Mesos, AWS S3, Cassandra...
- Framework: Hadoop MR, Hive, Pig,...
- Local Storages: CSV, database, ...
## What are Spark and SparkR?
### Spark:
Framework for cluster computing (you can use with Java, Scala, Python,...)
### SparkR
(spark+R): Framework for cluster computing using R
<center>
<img src="images/pic4.png" height="200px"/>
</center>
## SparkR architecture
<center>
<img src="images/pic1.png" height="200px"/>
</center>
# 1. SparkR initialization
<center>
<img src="images/spark.gif" height="200px"/>
</center>
Lets set variable that store location of spark,
```{r}
## SPARK_HOME <- Sys.getenv("SPARK_HOME")
SPARK_HOME <- "/home/bartek/programs/spark-1.5.2-bin-hadoop2.6/"
## SPARK_HOME <- "/Users/CotePelaez/Documents/spark-1.5.2-bin-hadoop2.6/"
```
## 1.1 Load SparR package
```{r LoadSparR}
.libPaths(c(.libPaths(), file.path(SPARK_HOME,"R/lib/")))
Sys.setenv('SPARKR_SUBMIT_ARGS'='"--packages" "com.databricks:spark-csv_2.10:1.2.0" "sparkr-shell"')
library(SparkR)
```
## 1.2 Load aditional packages
```{r LoadPackages}
library(rJava)
library(ggplot2)
library(pipeR)
library(whisker)
```
## 1.3 Initialization context
For educational reason Spark allows to be run on one local machine. We
get that by assigning master to local machine.
```{r}
sc <- sparkR.init(master = "local", sparkHome = SPARK_HOME)
hiveContext <- sparkRHive.init(sc)
```
Now, we can get access to Spark UI at <http://localhost:4040>
# 2. Introducing DataFrame
## 2.1 Difference between **data.frame** and **DataFrame**
DataFrame is Spark object that allows to do the computations on distributed storages directly in R.
We can create DataFrame object from standard data.frame as follows.
```{r DataFrame}
class(mtcars)
##df_mtcars <- createDataFrame(sqlContext, mtcars)
df_mtcars <- createDataFrame(hiveContext, mtcars)
class(df_mtcars)
df_mtcars
head(df_mtcars)
```
Note that this DataFrame object is not in the workspace of R; it is enterily in Spark.
## 2.2 Different ways to do a simple aggregation
```{r Aggregation}
count(filter(df_mtcars, "cyl = 6"))
```
### Method chaining (piping)
```{r}
df_mtcars %>>%
filter("cyl = 6") %>>%
count
```
### Querying
```{r}
df_mtcars %>>%
registerTempTable("mtcars")
hiveContext %>>%
tables %>>%
collect
sql(hiveContext, "select count(*) from mtcars where cyl = 6") %>>% collect
```
## 2.3 Lazy Execution
Spark does not execute calculations until we ask for results. Example:
```{r LazyExecution}
path1 <- "data/train.csv"
df1 <- read.df(hiveContext, path1,
source = "com.databricks.spark.csv",
header="true",
inferSchema = "true")
head(df1)
p <- proc.time()
df1_store3 <-
df1 %>>%
groupBy("Date") %>>%
agg(Sales="sum")
proc.time()-p
p <- proc.time()
df1_store3 %>>%
limit(10) %>>%
collect
proc.time()-p
p <- proc.time()
df1_store3 %>>%
count
proc.time()-p
```
## 2.4 Cache
```{r}
df1_store3_cache <-
df1_store3 %>>%
cache
p <- proc.time()
df1_store3_cache %>>%
limit(10) %>>%
collect
proc.time()-p
p <- proc.time()
df1_store3_cache %>>%
count
proc.time()-p
# eliminate if you dont need more
df1_store3_cache %>>% unpersist()
```
## 2.3.Basic manipulations in SparkR
<center>
<img src="images/kermit.gif" height="200px"/>
</center>
Now its time to meet few SparkR functions. There are more of them, and
they can be find in SparkR documentation.
<https://spark.apache.org/docs/latest/api/R/index.html>
* `describe`
* `filter`
* `select`
* `distinct`
* `mutate` (`withColumn`)
* `collect`
* `join`
First we load our datasets.
```{r}
states_properties_path <- "data/states_properties.csv"
states_division_path <- "data/states_division.csv"
sp_df <- read.df(hiveContext, states_properties_path,
source = "com.databricks.spark.csv",
header="true",
inferSchema = "true")
sd_df <- read.df(hiveContext, states_division_path,
source = "com.databricks.spark.csv",
header="true",
inferSchema = "true")
```
### Describe
```{r}
sp_df %>>%
describe %>>%
collect
```
### Select, distinct
```{r}
sd_df %>>%
select(sd_df$state_division) %>>%
distinct %>>%
count
```
### Mutate
```{r}
sp_df %>>%
mutate(Area_km2= (.$Area * 2.58999)) %>>%
head
sp_df %>>%
head
```
### Join
```{r}
s_df <-
sp_df %>>%
mutate(Area_km2= (.$Area * 2.58999)) %>>%
join(sd_df, .$state_abb == sd_df$state_abb)
head(s_df)
```
### GroupBy and agg
```{r}
d_df <-
s_df %>>%
groupBy("state_division") %>>%
agg(max_income=max(s_df$income), area=sum(s_df$Area))
collect(d_df)
s_df %>>%
groupBy("state_division") %>>%
avg("Area", "Income") %>>%
collect()
```
### Mixing queries and transformations
```{r}
registerTempTable(s_df, "s_table")
s2_df <-
sql(hiveContext,
"SELECT state_division, Population, Income * Population AS total_income FROM s_table")
s2_df %>>%
groupBy(.$state_division) %>>%
agg(total_income=sum(s2_df$total_income)) %>>%
collect
```
# 3. Examples
## 3.1 Reading folder with csvs
```{r}
path <- "data/subdata/test.csv"
data <- read.df(hiveContext, path,
source = "com.databricks.spark.csv",
header="true",
inferSchema = "true")
count(data)
path <- "data/subdata/"
data <- read.df(hiveContext, path,
source = "com.databricks.spark.csv",
header="true",
inferSchema = "true")
count(data)
```
## 3.2 Example with Parquet
```{r}
sqlContext <- sparkRSQL.init(sc)
train_df <-
sqlContext %>>%
read.df("data/trainParquet")
head(train_df)
printSchema(train_df)
test_df <-
sqlContext %>>%
read.df("data/testParquet")
printSchema(test_df)
````
## 3.3. Exploratory Analysis
### Question 1: How many stores are in train?
```{r DistinctStores}
train_df %>>%
select("Store") %>>% distinct %>>% count
```
### Question 2: Which day has the highest number of sales between January 2014 and July 2014? Plot evolution.
```{r DistinctStores3}
train_df %>>%
filter(.$Date <= '2014-07-31' & .$Date >= '2014-01-01') %>>%
#filter("Date <= '2014-07-31' AND Date >= '2014-01-01'") %>>%
groupBy("Date") %>>%
agg(Sales=sum(train_df$Sales)) %>>%
collect %>>%
ggplot() +
geom_bar(aes(as.Date(Date), Sales), stat="identity") +
xlab("Date") +
theme_bw()
```
### Question 3: Which day of the week has the highest number of promotions?
```{r DistinctStores2}
train_df %>>%
filter("Promo = 1") %>>%
groupBy("DayOfWeek") %>>%
agg(Promo=sum(train_df$Promo)) %>>%
orderBy(desc(.$Promo)) %>>%
collect
```
### Question 4: How to use parameters in queries.
```{r}
train_df %>>%
registerTempTable("train")
query <- "SELECT COUNT(*) FROM train GROUP BY Date"
sql(sqlContext, query) %>>%
head
query_template <-
"SELECT COUNT(*) FROM train GROUP BY {{{col}}}"
data <- list( col="Date")
query <- whisker.render(query_template, data)
sql(sqlContext, query) %>>%
head
```
## 3.4 ML
<center>
<img src="images/kids.gif" height="200px"/>
</center>
```{r}
function() {
library(MASS)
boston_df <- createDataFrame(hiveContext, Boston)
lm.fit <-
glm(medv~lstat,
data=boston_df,
family = "gaussian")
summary(lm.fit)
intercept <- summary(lm.fit)$coefficients[1]
slope <- summary(lm.fit)$coefficients[2]
boston_df %>>%
sample(FALSE, 0.1) %>>%
collect %>>%
ggplot() + geom_point(aes(lstat, medv)) + geom_abline(intercept=intercept, slope=slope)
lm.fit=glm(medv~lstat+age, data=boston_df, family = "gaussian")
summary(lm.fit)
#library(ISLR)
#Smarket_df <- createDataFrame(hiveContext, Smarket)
#glm.fit <- glm(Direction~Lag1 + Lag2 + Lag3 + Lag4 + Lag5 + Volume,
# data=Smarket_df, family = "binomial" )
}()
```
# Conclussions
Don't forget to close spark if working on cloud.
```{r}
sparkR.stop()
```