-
Notifications
You must be signed in to change notification settings - Fork 474
/
Copy pathpyspark-session-2020-04-16.txt
executable file
·67 lines (65 loc) · 2.04 KB
/
pyspark-session-2020-04-16.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
$ ./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.
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 0x117a227b8>
>>>
>>> input_path = '/Users/mparsian/spark-2.4.4/zbin/foxdata.txt'
>>> input_path
'/Users/mparsian/spark-2.4.4/zbin/foxdata.txt'
>>>
>>> recs = spark.sparkContext.textFile(input_path)
>>>
>>> recs.collect()
['red fox jumped high', 'fox jumped over high fence', 'red fox jumped']
>>> recs.count()
3
>>>
>>>
>>> words = recs.map(lambda r: r.split(" "))
>>> words.collect()
[['red', 'fox', 'jumped', 'high'], ['fox', 'jumped', 'over', 'high', 'fence'], ['red', 'fox', 'jumped']]
>>>
>>> words.take(1)
[['red', 'fox', 'jumped', 'high']]
>>> words.take(2)
[['red', 'fox', 'jumped', 'high'], ['fox', 'jumped', 'over', 'high', 'fence']]
>>> # recs : RDD[String]
...
>>> # words : RDD[[String]]
...
>>> x = "fox jumped"
>>> y = x.split(" ")
>>> y
['fox', 'jumped']
>>>
>>>
>>> single_words = words.flatMap(lambda x: x)
>>> single_words.collect()
['red', 'fox', 'jumped', 'high', 'fox', 'jumped', 'over', 'high', 'fence', 'red', 'fox', 'jumped']
>>> words.count()
3
>>> single_words.count()
12
>>> # single_words : RDD[String]
...
>>>
>>> pairs = single_words.map(lambda x : (x, 1))
>>> pairs.collect()
[('red', 1), ('fox', 1), ('jumped', 1), ('high', 1), ('fox', 1), ('jumped', 1), ('over', 1), ('high', 1), ('fence', 1), ('red', 1), ('fox', 1), ('jumped', 1)]
>>>
>>> pairs.collect()
[('red', 1), ('fox', 1), ('jumped', 1), ('high', 1), ('fox', 1), ('jumped', 1), ('over', 1), ('high', 1), ('fence', 1), ('red', 1), ('fox', 1), ('jumped', 1)]
>>> freq = pairs.reduceByKey(lambda a, b : a+b)
>>> freq.collect()
[('high', 2), ('fence', 1), ('red', 2), ('fox', 3), ('jumped', 3), ('over', 1)]