forked from JonRoussot/spark-and-r-with-sparklyr-course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflights.R
171 lines (80 loc) · 2.67 KB
/
flights.R
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
library(sparklyr)
library(dplyr)
flights <- spark_read_parquet(sc,
name = "flights_sc",
path = "Data/2008.parquet/")
colnames(flights)
dim(flights)
tbl_vars(flights)
dim(flights_sc)
## plane is the R object that you interact with in R
## which translates your R code to operate on the actual tabular data saved on the Spark cluster
plane <- spark_read_csv(sc,
name = "plane_sc",
path = "Data/plane-data.csv")
colnames(plane)
dim(plane)
tbl_vars(plane)
airports <- spark_read_csv(sc,
name = "airports_sc",
path = "Data/airports.csv")
colnames(airports)
dim(airports)
tbl_vars(airports)
print(airports)
carriers <- spark_read_csv(sc,
name = "carriers_sc",
path = "Data/carriers.csv")
colnames(carriers)
dim(carriers)
tbl_vars(carriers)
print(carriers)
## working with dplyr on spark ------------------------------------------------------------
## have a peek of the first few rows without really executing the queries
friday <- flights %>%
filter(DayOfWeek == 5) %>%
select(-DayOfWeek) %>%
mutate(Date = paste(Year, Month, DayofMonth, sep = "-"))
friday %>% select(Year, Month, DayofMonth, Date) %>% head(5)
## actually execute the code stored in friday
collect(friday)
## show SQL queries
show_query(friday)
## exercise Page 2-6
fifteen <- flights %>%
filter(DepDelay > 15 & DepDelay < 240, DayofMonth == 15) %>%
select(Year, Month, ArrDelay, DepDelay, Distance, UniqueCarrier)
show_query(fifteen)
collect(fifteen)
## sampling
flights %>% sample_n(5) %>% collect()
flights %>% sample_n(15) %>% collect()
flights5_1 <- flights %>%
sample_n(5) %>%
collect()
flights5_2 <- flights %>%
sample_n(5) %>%
collect()
all.equal(flights5_2, flights5_1)
## creating new spark data frames
sdf_register(friday, "fridayflights")
tbl_cache(sc, "fridayflights")
## bring it back to R
fridayflights <- tbl(sc, "fridayflights")
all.equal(friday, fridayflights)
all.equal(collect(friday), collect(fridayflights))
## join, register and cache
monday <- flights %>%
filter(Origin == "SEA" & DayOfWeek == 1) %>%
left_join(airports, by = c(Dest = "iata"))
sdf_register(monday, "monday")
tbl_cache(sc, "monday")
set.seed(1)
tmp1 <- tbl(sc, "monday") %>%
sample_frac(0.2) %>%
collect()
set.seed(1)
tmp2 <- tbl(sc, "monday") %>%
sample_frac(0.2) %>%
collect()
all.equal(tmp1, tmp2)