-
Notifications
You must be signed in to change notification settings - Fork 474
/
Copy pathpyspark-session_2019-10-07.txt
executable file
·299 lines (278 loc) · 7.7 KB
/
pyspark-session_2019-10-07.txt
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
$ cat /tmp/foxdata.txt
red fox jumped high
fox jumped over high fence
red fox jumped
$ cat /tmp/wordcount.py
from __future__ import print_function
import sys
from operator import add
from pyspark.sql import SparkSession
if __name__ == "__main__":
#
print ("This is the name of the script: ", sys.argv[0])
print ("Number of arguments: ", len(sys.argv))
print ("The arguments are: " , str(sys.argv))
#
# DEFINE your input path
input_path = sys.argv[1]
print("input_path: ", input_path)
# CREATE an instance of a SparkSession object
spark = SparkSession\
.builder\
.appName("PythonWordCount")\
.getOrCreate()
# CREATE a new RDD[String]
lines = spark.sparkContext.textFile(input_path)
# APPLY a SET of TRANSFORMATIONS...
counts = lines.flatMap(lambda x: x.split(' ')) \
.map(lambda x: (x, 1)) \
.reduceByKey(add)
# .reduceByKey(lambda a,b : a+b)
# output = [(word1, count1), (word2, count2), ...]
output = counts.collect()
for (word, count) in output:
print("%s: %i" % (word, count))
# DONE!
spark.stop()
$ ./bin/spark-submit /tmp/wordcount.py /tmp/foxdata.txt
19/10/07 19:59:19 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
This is the name of the script: /Users/mparsian/spark-2.4.4/zbin/wordcount.py
Number of arguments: 2
The arguments are: ['/tmp/wordcount.py', '/tmp/foxdata.txt']
input_path: /tmp/foxdata.txt
19/10/07 19:59:19 INFO SparkContext: Running Spark version 2.4.4
...
...
...
high: 2
fence: 1
red: 2
fox: 3
jumped: 3
over: 1
19/10/07 19:59:22 INFO SparkUI: Stopped Spark web UI at http://172.31.63.69:4041
...
$ cat /tmp/ratings.txt
U1,M4,4
U1,M4,3
U1,M2,5
U1,M2,0
U1,M3,2
U2,M4,3
U2,M4,4
U2,M4,5
U3,M1,1
U3,M5,6
U3,M4,4
U3,M4,5
U4,M2,3
U4,M1,1
U4,M1,4
U4,M1,5
U5,M1,3
U5,M1,1
U6,M1,3
U6,M9,4
mparsian@Mahmouds-MacBook ~/spark-2.4.4 $ ./bin/pyspark
Python 3.7.2 (v3.7.2:9a3ffc0492, Dec 24 2018, 02:44:43)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
19/10/07 18:51:12 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties
Setting default log level to "WARN".
To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).
Welcome to
____ __
/ __/__ ___ _____/ /__
_\ \/ _ \/ _ `/ __/ '_/
/__ / .__/\_,_/_/ /_/\_\ version 2.4.4
/_/
Using Python version 3.7.2 (v3.7.2:9a3ffc0492, Dec 24 2018 02:44:43)
SparkSession available as 'spark'.
>>>
>>>
>>> spark
<pyspark.sql.session.SparkSession object at 0x105c4cda0>
>>>
>>> input_path = "/tmp/ratings.txt"
>>> input_path
'/tmp/ratings.txt'
>>>
>>>
>>> records = spark.sparkContext.textFile(input_path)
>>> records.count()
20
>>> records.collect()
['U1,M4,4', 'U1,M4,3', 'U1,M2,5', 'U1,M2,0', 'U1,M3,2', 'U2,M4,3', 'U2,M4,4', 'U2,M4,5', 'U3,M1,1', 'U3,M5,6', 'U3,M4,4', 'U3,M4,5', 'U4,M2,3', 'U4,M1,1', 'U4,M1,4', 'U4,M1,5', 'U5,M1,3', 'U5,M1,1', 'U6,M1,3', 'U6,M9,4']
>>>
>>>
>>> records.getNumPartitions()
2
>>> records2 = spark.sparkContext.textFile(input_path, 5)
>>> records2.getNumPartitions()
5
>>> records.saveAsTextFile('/tmp/myoutput')
>>>
>>>
>>> input_data = ['fox ah jumped', 'blue of fox jumped', 'blue fox jumped']
>>> input_data
['fox ah jumped', 'blue of fox jumped', 'blue fox jumped']
>>>
>>> rdd = spark.sparkContext.parallelize(input_data)
>>> rdd.collect()
['fox ah jumped', 'blue of fox jumped', 'blue fox jumped']
>>> rdd.count()
3
>>>
>>> words = rdd.flatMap(lambda x: x.split(" "))
>>> words.count()
10
>>> words.collect()
['fox', 'ah', 'jumped', 'blue', 'of', 'fox', 'jumped', 'blue', 'fox', 'jumped']
>>>
>>> x = "fox ah jumped"
>>> mylist = x.split(" ")
>>> mylist
['fox', 'ah', 'jumped']
>>>
>>> words.collect()
['fox', 'ah', 'jumped', 'blue', 'of', 'fox', 'jumped', 'blue', 'fox', 'jumped']
>>> pairs = words.map(lambda w: (w, 1))
>>> pairs.collect()
[('fox', 1), ('ah', 1), ('jumped', 1), ('blue', 1), ('of', 1), ('fox', 1), ('jumped', 1), ('blue', 1), ('fox', 1), ('jumped', 1)]
>>>
>>>
>>> freqs = pairs.reduceByKey(lambda a, b: a+b)
>>> freqs.collect()
[('of', 1), ('fox', 3), ('ah', 1), ('blue', 2), ('jumped', 3)]
>>>
>>> rdd = spark.sparkContext.parallelize([1, 2, 40, 60])
>>> rdd.collect()
[1, 2, 40, 60]
>>> spark
<pyspark.sql.session.SparkSession object at 0x11935ada0>
>>>
>>> input_path = "/tmp/ratings.txt"
>>> input_path
'/tmp/ratings.txt'
>>>
>>>
>>> records = spark.sparkContext.textFile(input_path)
>>> records.count()
20
>>> records.collect()
['U1,M4,4', 'U1,M4,3', 'U1,M2,5', 'U1,M2,0', 'U1,M3,2', 'U2,M4,3', 'U2,M4,4', 'U2,M4,5', 'U3,M1,1', 'U3,M5,6', 'U3,M4,4', 'U3,M4,5', 'U4,M2,3', 'U4,M1,1', 'U4,M1,4', 'U4,M1,5', 'U5,M1,3', 'U5,M1,1', 'U6,M1,3', 'U6,M9,4']
>>>
>>>
>>> records.getNumPartitions()
2
>>> records2 = spark.sparkContext.textFile(input_path, 5)
>>> records2.getNumPartitions()
5
>>> records.saveAsTextFile('/tmp/myoutput')
>>>
>>> input_data = ['fox ah jumped', 'blue of fox jumped', 'blue fox jumped']
>>> input_data
['fox ah jumped', 'blue of fox jumped', 'blue fox jumped']
>>>
>>> rdd = spark.sparkContext.parallelize(input_data)
>>> rdd.collect()
['fox ah jumped', 'blue of fox jumped', 'blue fox jumped']
>>> rdd.count()
3
>>>
>>> words = rdd.flatMap(lambda x: x.split(" "))
>>> words.count()
10
>>> words.collect()
['fox', 'ah', 'jumped', 'blue', 'of', 'fox', 'jumped', 'blue', 'fox', 'jumped']
>>> words.collect()
['fox', 'ah', 'jumped', 'blue', 'of', 'fox', 'jumped', 'blue', 'fox', 'jumped']
>>>
>>> pairs = words.map(lambda w: (w, 1))
>>> pairs.collect()
[('fox', 1), ('ah', 1), ('jumped', 1), ('blue', 1), ('of', 1), ('fox', 1), ('jumped', 1), ('blue', 1), ('fox', 1), ('jumped', 1)]
>>>
>>>
>>> freqs = pairs.reduceByKey(lambda a, b: a+b)
>>> freqs.collect()
[('of', 1), ('fox', 3), ('ah', 1), ('blue', 2), ('jumped', 3)]
>>> spark
<pyspark.sql.session.SparkSession object at 0x10e636da0>
>>>
>>>
>>> coll = ['fox ah jumped', 'ah blue fox jumped', 'blue fox jumped']
>>> len(coll)
3
>>> rdd = spark.sparkContext.parallelize(coll)
>>> rdd.collect()
['fox ah jumped', 'ah blue fox jumped', 'blue fox jumped']
>>>
>>> x ="A B C"
>>> mylist = x.split(" ")
>>> mylist
['A', 'B', 'C']
>>>
>>>
>>> words = rdd.flatMap(lambda x: x.split(" "))
>>> words.count()
10
>>> words.collect()
['fox', 'ah', 'jumped', 'ah', 'blue', 'fox', 'jumped', 'blue', 'fox', 'jumped']
>>> words
PythonRDD[6] at collect at <stdin>:1
>>> pairs = words.map(lambda word: (word, 1))
>>> pairs.collect()
[('fox', 1), ('ah', 1), ('jumped', 1), ('ah', 1), ('blue', 1), ('fox', 1), ('jumped', 1), ('blue', 1), ('fox', 1), ('jumped', 1)]
>>>
>>> freqs = pairs.reduceByKey(lambda a, b: a+b)
>>> freqs.collect()
[('fox', 3), ('ah', 2), ('blue', 2), ('jumped', 3)]
>>>
>>>
>>>
>>>
>>> data = [1, 2, 3, 4, 40, 50, 60]
>>> data
[1, 2, 3, 4, 40, 50, 60]
>>> rdd = spark.sparkContext.parallelize(data)
>>> rdd
ParallelCollectionRDD[13] at parallelize at PythonRDD.scala:195
>>> rdd.collect()
[1, 2, 3, 4, 40, 50, 60]
>>> rdd.count()
7
>>>
>>> rdd
ParallelCollectionRDD[13] at parallelize at PythonRDD.scala:195
>>> rdd.collect()
[1, 2, 3, 4, 40, 50, 60]
>>>
>>> def custom_fun(n):
... if n < 40:
... return [n, 100]
... elif n < 50:
... return [200, 300, 400]
... else:
... return []
...
>>> custom_fun(30)
[30, 100]
>>> custom_fun(50)
[]
>>> custom_fun(60)
[]
>>> target = rdd.flatMap(custom_fun)
>>> target.collect()
[1, 100, 2, 100, 3, 100, 4, 100, 200, 300, 400]
>>> rdd.getNumPartitions()
8
>>>
>>>
>>>
>>> data
[1, 2, 3, 4, 40, 50, 60]
>>> rdd = spark.sparkContext.parallelize(data, 3)
>>> rdd.getNumPartitions()
3
>>>