-
Notifications
You must be signed in to change notification settings - Fork 475
/
Copy pathpyspark-session-2019-10-09.txt
executable file
·153 lines (151 loc) · 3.93 KB
/
pyspark-session-2019-10-09.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
/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/09 18:57:29 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'.
>>>
>>>
>>>
>>>
>>> numbers = [1, 2, 3, 1, 2, 3, 4, 4, 5, 6]
>>> numbers
[1, 2, 3, 1, 2, 3, 4, 4, 5, 6]
>>> rdd = spark.sparkContext.parallelize(numbers)
>>> rdd.collect()
[1, 2, 3, 1, 2, 3, 4, 4, 5, 6]
>>> rdd.count()
10
>>> rdd2 = rdd.filter(lambda x : x > 3)
>>> rdd2.collect()
[4, 4, 5, 6]
>>>
>>>
>>> def custom_filter(x):
... if x > 3:
... return True
... else:
... return False
... ^D
>>>
>>> x = custom_filter(10)
>>> x
True
>>> x = custom_filter(2)
>>> x
False
>>> rdd3 = rdd.filter(custom_filter)
>>> rdd3.collect()
[4, 4, 5, 6]
>>> rdd2.collect()
[4, 4, 5, 6]
>>>
>>>
>>> data = [('A', 2), ('A', 3), ('A', 4), ('A', 5), ('B', 2),('B', 7)]
>>> data
[('A', 2), ('A', 3), ('A', 4), ('A', 5), ('B', 2), ('B', 7)]
>>>
>>> rdd = spark.sparkContext.parallelize(data)
>>> rdd.collect()
[('A', 2), ('A', 3), ('A', 4), ('A', 5), ('B', 2), ('B', 7)]
>>>
>>>
>>>
>>>
>>> total = rdd.reduceByKey(lambda x, y: x+y)
>>> total.collect()
[('B', 9), ('A', 14)]
>>>
>>>
>>> rdd.collect()
[('A', 2), ('A', 3), ('A', 4), ('A', 5), ('B', 2), ('B', 7)]
>>> grouped = rdd.groupByKey()
>>> grouped.collect()
[
('B', <pyspark.resultiterable.ResultIterable object at 0x114ef18d0>),
('A', <pyspark.resultiterable.ResultIterable object at 0x114ef19e8>)
]
>>> grouped.map(lambda x: (x[0], list(x[1])).collect()
[('B', [2, 7]), ('A', [2, 3, 4, 5])]
>>> total2 = grouped.map(lambda x: (x[0], sum(x[1])))
>>> total2.collect()
[('B', 9), ('A', 14)]
>>>
>>>
>>> spark
<pyspark.sql.session.SparkSession object at 0x11848eda0>
>>> numbers = [-1, 2, 3, -55, 88, 99, -99, 66, 777]
>>> numbers
[-1, 2, 3, -55, 88, 99, -99, 66, 777]
>>> rdd = spark.sparkContext.parallelize(numbers)
>>> rdd.collect()
[-1, 2, 3, -55, 88, 99, -99, 66, 777]
>>>
>>> positives = rdd.filter(lambda x : x > 0)
>>> positives.collect()
[2, 3, 88, 99, 66, 777]
>>>
>>> negatives = rdd.filter(lambda x : x < 0)
>>> negatives.collect()
[-1, -55, -99]
>>> def keep_positives(n):
... if (n > 0):
... return True
... else:
... return False
... ^D
>>>
>>> a = keep_positives(100)
>>> a
True
>>> a = keep_positives(-9)
>>> a
False
>>> pos2 = rdd.filter(keep_positives)
>>> pos2.collect()
[2, 3, 88, 99, 66, 777]
>>> pos2222 = pos2.filter(lambda x : True)
>>> pos2222.collect()
[2, 3, 88, 99, 66, 777]
>>>
>>>
>>> pairs = [('A', 2), ('A', 3), ('A', 4),('A', 5), ('A', 6), ('B', 10), ('B', 2)]
>>> pairs
[('A', 2), ('A', 3), ('A', 4), ('A', 5), ('A', 6), ('B', 10), ('B', 2)]
>>>
>>>
>>> rdd = spark.sparkContext.parallelize(pairs)
>>> rdd.collect()
[('A', 2), ('A', 3), ('A', 4), ('A', 5), ('A', 6), ('B', 10), ('B', 2)]
>>> totals = rdd.reduceByKey(lambda a, b : a+b)
>>> result = totals.collect()
>>> result
[('B', 12), ('A', 20)]
>>>
>>>
>>> rdd.collect()
[('A', 2), ('A', 3), ('A', 4), ('A', 5), ('A', 6), ('B', 10), ('B', 2)]
>>> grouped = rdd.groupByKey()
>>> grouped.collect()
[
('B', <pyspark.resultiterable.ResultIterable object at 0x1184f3b70>),
('A', <pyspark.resultiterable.ResultIterable object at 0x1184f3c88>)
]
>>>
>>> grouped.map(lambda x: (x[0], list(x[1]))).collect()
[('B', [10, 2]), ('A', [2, 3, 4, 5, 6])]
>>>
>>> sum2 = grouped.map(lambda x: (x[0], sum(x[1])))
>>> sum2.collect()
[('B', 12), ('A', 20)]
>>>