-
Notifications
You must be signed in to change notification settings - Fork 472
/
dataframe-session-2018-10-30.txt
480 lines (436 loc) · 15.1 KB
/
dataframe-session-2018-10-30.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
Input Files Used:
$ cat /Users/mparsian/zmp/pyspark_book_project/git-manning/code/chap08/sample_no_header.csv
Alex,Sunnyvale,30
Mary,Cupertino,28
Jane,Stanford,44
Bob,Ames,33
$ cat /Users/mparsian/zmp/pyspark_book_project/git-manning/code/chap08/sample_with_header.csv
name,city,age
Alex,Sunnyvale,30
Mary,Cupertino,28
Jane,Stanford,44
Bob,Ames,33
MySQL Relational Database Table:
mparsian@Mahmouds-MacBook ~/spark-2.3.0 $ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.7.18 MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use metadb
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from dept;
+-------------+------------+---------------+---------+
| dept_number | dept_name | dept_location | manager |
+-------------+------------+---------------+---------+
| 10 | ACCOUNTING | NEW YORK, NY | alex |
| 20 | RESEARCH | DALLAS, TX | alex |
| 30 | SALES | CHICAGO, IL | jane |
| 40 | OPERATIONS | BOSTON, MA | jane |
| 50 | MARKETING | Sunnyvale, CA | jane |
| 60 | SOFTWARE | Stanford, CA | jane |
| 70 | HARDWARE | BOSTON, MA | sophia |
+-------------+------------+---------------+---------+
7 rows in set (0.00 sec)
mysql>
$ ./bin/pyspark
Python 2.7.10 (default, Oct 6 2017, 22:29:07)
Welcome to
____ __
/ __/__ ___ _____/ /__
_\ \/ _ \/ _ `/ __/ '_/
/__ / .__/\_,_/_/ /_/\_\ version 2.3.0
/_/
Using Python version 2.7.10 (default, Oct 6 2017 22:29:07)
SparkSession available as 'spark'.
>>>
>>>
>>> spark
<pyspark.sql.session.SparkSession object at 0x11061cc50>
>>>
>>> input_path = "/Users/mparsian/zmp/pyspark_book_project/git-manning/code/chap08/sample_no_header.csv"
>>> input_path
'/Users/mparsian/zmp/pyspark_book_project/git-manning/code/chap08/sample_no_header.csv'
>>>
>>> df = spark\
... .read\
... .format("csv")\
... .option("header","false")\
... .option("inferSchema", "true")\
... .load(input_path)
>>>
>>> df.show()
+----+---------+---+
| _c0| _c1|_c2|
+----+---------+---+
|Alex|Sunnyvale| 30|
|Mary|Cupertino| 28|
|Jane| Stanford| 44|
| Bob| Ames| 33|
+----+---------+---+
>>> df.count()
4
>>> df.printSchema()
root
|-- _c0: string (nullable = true)
|-- _c1: string (nullable = true)
|-- _c2: integer (nullable = true)
>>> df.createOrReplaceTempView("people")
>>>
>>> df2 = spark.sql("select * from people")
>>> df2.show()
+----+---------+---+
| _c0| _c1|_c2|
+----+---------+---+
|Alex|Sunnyvale| 30|
|Mary|Cupertino| 28|
|Jane| Stanford| 44|
| Bob| Ames| 33|
+----+---------+---+
>>> df2 = spark.sql("select * from people where _c2 > 30")
>>> df2.show()
+----+--------+---+
| _c0| _c1|_c2|
+----+--------+---+
|Jane|Stanford| 44|
| Bob| Ames| 33|
+----+--------+---+
>>> df4 = df.selectExpr("_c0 as name", "_c1 as city", "_c2 as age")
>>> df4.show()
+----+---------+---+
|name| city|age|
+----+---------+---+
|Alex|Sunnyvale| 30|
|Mary|Cupertino| 28|
|Jane| Stanford| 44|
| Bob| Ames| 33|
+----+---------+---+
>>> df.createOrReplaceTempView("people_table")
>>>
>>> df4.createOrReplaceTempView("people_table")
>>> df4.show()
+----+---------+---+
|name| city|age|
+----+---------+---+
|Alex|Sunnyvale| 30|
|Mary|Cupertino| 28|
|Jane| Stanford| 44|
| Bob| Ames| 33|
+----+---------+---+
>>> df6 = spark.sql("select * from people_table where age > 30")
>>> df6.show()
+----+--------+---+
|name| city|age|
+----+--------+---+
|Jane|Stanford| 44|
| Bob| Ames| 33|
+----+--------+---+
>>> df6.printSchema()
root
|-- name: string (nullable = true)
|-- city: string (nullable = true)
|-- age: integer (nullable = true)
>>>
>>>
>>> df6.collect()
[Row(name=u'Jane', city=u'Stanford', age=44), Row(name=u'Bob', city=u'Ames', age=33)]
>>> df6.show(1)
+----+--------+---+
|name| city|age|
+----+--------+---+
|Jane|Stanford| 44|
+----+--------+---+
only showing top 1 row
>>> df6.show(2)
+----+--------+---+
|name| city|age|
+----+--------+---+
|Jane|Stanford| 44|
| Bob| Ames| 33|
+----+--------+---+
>>> df6.show(30)
+----+--------+---+
|name| city|age|
+----+--------+---+
|Jane|Stanford| 44|
| Bob| Ames| 33|
+----+--------+---+
>>>
>>>
>>>
>>> input_path = "/Users/mparsian/zmp/pyspark_book_project/git-manning/code/chap08/sample_with_header.csv"
>>>
>>>
>>> input_path
'/Users/mparsian/zmp/pyspark_book_project/git-manning/code/chap08/sample_with_header.csv'
>>>
>>>
>>>
>>>
>>> df8 = spark\
... .read\
... .format("csv")\
... .option("header","true")\
... .option("inferSchema", "true")\
... .load(input_path)
>>>
>>> df8.show()
+----+---------+---+
|name| city|age|
+----+---------+---+
|Alex|Sunnyvale| 30|
|Mary|Cupertino| 28|
|Jane| Stanford| 44|
| Bob| Ames| 33|
+----+---------+---+
>>> df8.createOrReplaceTempView("people22")
>>> df8.printSchema()
root
|-- name: string (nullable = true)
|-- city: string (nullable = true)
|-- age: integer (nullable = true)
>>> spark.sql("select name, age from people22")
DataFrame[name: string, age: int]
>>> spark.sql("select name, age from people22").show()
+----+---+
|name|age|
+----+---+
|Alex| 30|
|Mary| 28|
|Jane| 44|
| Bob| 33|
+----+---+
>>>
>>>
>>> df10 = spark.createDataFrame([(1, "a"),(2, "b"), (3, "a")],["id", "name"])
>>> df10.show()
+---+----+
| id|name|
+---+----+
| 1| a|
| 2| b|
| 3| a|
+---+----+
>>> df10 = spark.createDataFrame([(1, "a"),(2, "b"), (3, "a"), (1, "a"),(2, "b"), (3, "a")],["id", "name"])
>>> df10.show()
+---+----+
| id|name|
+---+----+
| 1| a|
| 2| b|
| 3| a|
| 1| a|
| 2| b|
| 3| a|
+---+----+
>>> df10.createOrReplaceTempView("mytable")
>>>
>>> df10
DataFrame[id: bigint, name: string]
>>>
>>>
>>> spark.sql("select id, name from mytable").show()
+---+----+
| id|name|
+---+----+
| 1| a|
| 2| b|
| 3| a|
| 1| a|
| 2| b|
| 3| a|
+---+----+
>>> spark.sql("select id, name from mytable where id > 1").show()
+---+----+
| id|name|
+---+----+
| 2| b|
| 3| a|
| 2| b|
| 3| a|
+---+----+
>>>
>>>
>>>
>>> spark.sql("select t1.id, t1.name, t2.id, t2.name from mytable t1, mytable t2 where t1.id == t2.id").show()
+---+----+---+----+
| id|name| id|name|
+---+----+---+----+
| 1| a| 1| a|
| 1| a| 1| a|
| 1| a| 1| a|
| 1| a| 1| a|
| 3| a| 3| a|
| 3| a| 3| a|
| 3| a| 3| a|
| 3| a| 3| a|
| 2| b| 2| b|
| 2| b| 2| b|
| 2| b| 2| b|
| 2| b| 2| b|
+---+----+---+----+
>>> spark.sql("select t1.id, t1.name, t2.id, t2.name from mytable t1, mytable t2 where t1.id == t2.id").show()
+---+----+---+----+
| id|name| id|name|
+---+----+---+----+
| 1| a| 1| a|
| 1| a| 1| a|
| 1| a| 1| a|
| 1| a| 1| a|
| 3| a| 3| a|
| 3| a| 3| a|
| 3| a| 3| a|
| 3| a| 3| a|
| 2| b| 2| b|
| 2| b| 2| b|
| 2| b| 2| b|
| 2| b| 2| b|
+---+----+---+----+
>>>
>>>
>>> JDBC_URL = "jdbc:mysql://localhost/metadb"
>>> JDBC_DRIVER = "com.mysql.jdbc.Driver"
>>> JDBC_USER = "root"
>>> JDBC_PASSWORD = "mp22_pass"
>>> JDBC_TABLE_NAME = "dept"
>>>
>>> JDBC_URL
'jdbc:mysql://localhost/metadb'
>>> JDBC_DRIVER
'com.mysql.jdbc.Driver'
>>> JDBC_USER
'root'
>>> JDBC_PASSWORD
'mp22_pass'
>>> JDBC_TABLE_NAME
'dept'
>>>
>>>
>>>
>>>
>>>
>>> df = spark\
... .read\
... .format("jdbc")\
... .option("url", JDBC_URL)\
... .option("driver", JDBC_DRIVER)\
... .option("dbtable", JDBC_TABLE_NAME)\
... .option("user", JDBC_USER)\
... .option("password", JDBC_PASSWORD)\
... .load()
Traceback (most recent call last):
File "<stdin>", line 8, in <module>
File "/Users/mparsian/spark-2.3.0/python/pyspark/sql/readwriter.py", line 172, in load
return self._df(self._jreader.load())
File "/Users/mparsian/spark-2.3.0/python/lib/py4j-0.10.6-src.zip/py4j/java_gateway.py", line 1160, in __call__
File "/Users/mparsian/spark-2.3.0/python/pyspark/sql/utils.py", line 63, in deco
return f(*a, **kw)
File "/Users/mparsian/spark-2.3.0/python/lib/py4j-0.10.6-src.zip/py4j/protocol.py", line 320, in get_return_value
py4j.protocol.Py4JJavaError: An error occurred while calling o501.load.
: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.apache.spark.sql.execution.datasources.jdbc.DriverRegistry$.register(DriverRegistry.scala:45)
at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions$$anonfun$6.apply(JDBCOptions.scala:79)
at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions$$anonfun$6.apply(JDBCOptions.scala:79)
at scala.Option.foreach(Option.scala:257)
at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions.<init>(JDBCOptions.scala:79)
at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions.<init>(JDBCOptions.scala:35)
at org.apache.spark.sql.execution.datasources.jdbc.JdbcRelationProvider.createRelation(JdbcRelationProvider.scala:34)
at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:340)
at org.apache.spark.sql.DataFrameReader.loadV1Source(DataFrameReader.scala:239)
at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:227)
at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:164)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
at py4j.Gateway.invoke(Gateway.java:282)
at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
at py4j.commands.CallCommand.execute(CallCommand.java:79)
at py4j.GatewayConnection.run(GatewayConnection.java:214)
at java.lang.Thread.run(Thread.java:748)
>>>
>>>
$ export JAR="/Users/mparsian/zmp/pyspark_book_project/git-manning/code/jars/mysql-connector-java-5.1.42.jar"
$ ./bin/pyspark --jars $JAR
Python 2.7.10 (default, Oct 6 2017, 22:29:07)
Welcome to
____ __
/ __/__ ___ _____/ /__
_\ \/ _ \/ _ `/ __/ '_/
/__ / .__/\_,_/_/ /_/\_\ version 2.3.0
/_/
Using Python version 2.7.10 (default, Oct 6 2017 22:29:07)
SparkSession available as 'spark'.
>>> JDBC_URL = "jdbc:mysql://localhost/metadb"
>>> JDBC_DRIVER = "com.mysql.jdbc.Driver"
>>> JDBC_USER = "root"
>>> JDBC_PASSWORD = "mp22_pass"
>>> JDBC_TABLE_NAME = "dept"
>>>
>>> df = spark\
... .read\
... .format("jdbc")\
... .option("url", JDBC_URL)\
... .option("driver", JDBC_DRIVER)\
... .option("dbtable", JDBC_TABLE_NAME)\
... .option("user", JDBC_USER)\
... .option("password", JDBC_PASSWORD)\
... .load()
Tue Oct 30 18:46:18 PDT 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
>>>
>>> df.show()
Tue Oct 30 18:46:27 PDT 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
+-----------+----------+-------------+-------+
|dept_number| dept_name|dept_location|manager|
+-----------+----------+-------------+-------+
| 10|ACCOUNTING| NEW YORK, NY| alex|
| 20| RESEARCH| DALLAS, TX| alex|
| 30| SALES| CHICAGO, IL| jane|
| 40|OPERATIONS| BOSTON, MA| jane|
| 50| MARKETING|Sunnyvale, CA| jane|
| 60| SOFTWARE| Stanford, CA| jane|
| 70| HARDWARE| BOSTON, MA| sophia|
+-----------+----------+-------------+-------+
>>>
>>> df.printSchema()
root
|-- dept_number: integer (nullable = true)
|-- dept_name: string (nullable = true)
|-- dept_location: string (nullable = true)
|-- manager: string (nullable = true)
>>> df.createOrReplaceTempView("dept")
>>> spark.sql("select * from dept").show()
Tue Oct 30 18:49:20 PDT 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
+-----------+----------+-------------+-------+
|dept_number| dept_name|dept_location|manager|
+-----------+----------+-------------+-------+
| 10|ACCOUNTING| NEW YORK, NY| alex|
| 20| RESEARCH| DALLAS, TX| alex|
| 30| SALES| CHICAGO, IL| jane|
| 40|OPERATIONS| BOSTON, MA| jane|
| 50| MARKETING|Sunnyvale, CA| jane|
| 60| SOFTWARE| Stanford, CA| jane|
| 70| HARDWARE| BOSTON, MA| sophia|
+-----------+----------+-------------+-------+
>>> spark.sql("select * from dept where dept_number < 50").show()
Tue Oct 30 18:49:39 PDT 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
+-----------+----------+-------------+-------+
|dept_number| dept_name|dept_location|manager|
+-----------+----------+-------------+-------+
| 10|ACCOUNTING| NEW YORK, NY| alex|
| 20| RESEARCH| DALLAS, TX| alex|
| 30| SALES| CHICAGO, IL| jane|
| 40|OPERATIONS| BOSTON, MA| jane|
+-----------+----------+-------------+-------+
>>>