-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy path06_partII_distributedsystems.R
More file actions
214 lines (122 loc) · 3.87 KB
/
06_partII_distributedsystems.R
File metadata and controls
214 lines (122 loc) · 3.87 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
# initialize the input text (for simplicity as one text string)
input_text <-
"Apple Orange Mango
Orange Grapes Plum
Apple Plum Mango
Apple Apple Plum"
# Mapper splits input into lines
lines <- as.list(strsplit(input_text, "\n")[[1]])
lines[1:2]
# Mapper splits lines into key–value pairs
map_fun <-
function(x){
# remove special characters
x_clean <- gsub("[[:punct:]]", "", x)
# split line into words
keys <- unlist(strsplit(x_clean, " "))
# initialize key–value pairs
key_values <- rep(1, length(keys))
names(key_values) <- keys
return(key_values)
}
kv_pairs <- Map(map_fun, lines)
# look at the result
kv_pairs[1:2]
# order and shuffle
kv_pairs <- unlist(kv_pairs)
keys <- unique(names(kv_pairs))
keys <- keys[order(keys)]
shuffled <- lapply(keys,
function(x) kv_pairs[x == names(kv_pairs)])
shuffled[1:2]
sums <- lapply(shuffled, Reduce, f=sum)
names(sums) <- keys
sums[1:2]
# create directory for input files (typically text files)
mkdir ~/input
echo "Apple Orange Mango
Orange Grapes Plum
Apple Plum Mango
Apple Apple Plum" >> ~/input/text.txt
# run mapreduce word count
/usr/local/hadoop/bin/hadoop jar \
/usr/local/hadoop/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.10.1.jar \
wordcount
~/input ~/wc_example
cat ~/wc_example/*
# might have to switch to java version 8 first
sudo update-alternatives --config java
$ SPARK-HOME/bin/sparkR
# to install use
# devtools::install_github("cran/SparkR")
# load packages
library(SparkR)
# start session
sparkR.session()
# install.packages("SparkR")
# or, if temporarily not available on CRAN:
#if (!require('devtools')) install.packages('devtools')
#devtools::install_github('apache/spark@v2.x.x', subdir='R/pkg') # replace x.x with the version of your spark installation
# load packages
library(SparkR)
# start session
sparkR.session(sparkHome = "/home/umatter/.cache/spark/spark-3.1.2-bin-hadoop2.7")
# Import data and create a SparkDataFrame
# (a distributed collection of data, RDD)
flights <- read.df("data/flights.csv", source = "csv", header="true")
# inspect the object
class(flights)
dim(flights)
flights$dep_delay <- cast(flights$dep_delay, "double")
flights$dep_time <- cast(flights$dep_time, "double")
flights$arr_time <- cast(flights$arr_time, "double")
flights$arr_delay <- cast(flights$arr_delay, "double")
flights$air_time <- cast(flights$air_time, "double")
flights$distance <- cast(flights$distance, "double")
# filter
long_flights <- select(flights, "carrier", "year", "arr_delay", "distance")
long_flights <- filter(long_flights, long_flights$distance >= 1000)
head(long_flights)
# aggregation: mean delay per carrier
long_flights_delays<- summarize(groupBy(long_flights, long_flights$carrier),
avg_delay = mean(long_flights$arr_delay))
head(long_flights_delays)
# Convert result back into native R object
delays <- collect(long_flights_delays)
class(delays)
delays
cd SPARK-HOME
$ bin/spark-sql
{"name":"Michael", "salary":3000}
{"name":"Andy", "salary":4500}
{"name":"Justin", "salary":3500}
{"name":"Berta", "salary":4000}
SELECT *
FROM json.`examples/src/main/resources/employees.json`
;
SELECT *
FROM json.`examples/src/main/resources/employees.json`
WHERE salary <4000
;
SELECT AVG(salary) AS mean_salary
FROM json.`examples/src/main/resources/employees.json`;
# to install use
# devtools::install_github("cran/SparkR")
# load packages
library(SparkR)
# start session
sparkR.session()
# read data
flights <- read.df("data/flights.csv", source = "csv", header="true")
# register the data frame as a table
createOrReplaceTempView(flights, "flights" )
# now run SQL queries on it
query <-
"SELECT DISTINCT carrier,
year,
arr_delay,
distance
FROM flights
WHERE 1000 <= distance"
long_flights2 <- sql(query)
head(long_flights2)