Spark SQL

Python 中的 Spark SQL 入门

Mark Plutowski Phd

Data Scientist

创建 SQL 表并查询

Pyspark Shell 光标

Python 中的 Spark SQL 入门

从文件加载 DataFrame

df = spark.read.csv(filename)
df = spark.read.csv(filename, header=True)
Python 中的 Spark SQL 入门

创建 SQL 表并查询

df.createOrReplaceTempView("schedule")

spark.sql("SELECT * FROM schedule WHERE station = 'San Jose'") .show()
+--------+--------+-----+
|train_id| station| time|
+--------+--------+-----+
|     324|San Jose|9:05a|
|     217|San Jose|6:59a|
+--------+--------+-----+
Python 中的 Spark SQL 入门

查看表模式

result = spark.sql("SHOW COLUMNS FROM tablename")
result = spark.sql("SELECT * FROM tablename LIMIT 0")
result = spark.sql("DESCRIBE tablename")
result.show()
print(result.columns)
Python 中的 Spark SQL 入门

框架

Python 中的 Spark SQL 入门

数据框

Python 中的 Spark SQL 入门

表格数据

+--------+-------------+-----+
|train_id|      station| time|
+--------+-------------+-----+
|     324|San Francisco|7:59a| 
|     324|  22nd Street|8:03a|
|     324|     Millbrae|8:16a|
|     324|    Hillsdale|8:24a|
|     324| Redwood City|8:31a|
|     324|    Palo Alto|8:37a|
|     324|     San Jose|9:05a|
|     217|       Gilroy|6:06a|
|     217|   San Martin|6:15a|
|     217|  Morgan Hill|6:21a|
|     217| Blossom Hill|6:36a|
|     217|      Capitol|6:42a|
|     217|       Tamien|6:50a|
|     217|     San Jose|6:59a|
+--------+-------------+-----+
Python 中的 Spark SQL 入门

两个数据框

Python 中的 Spark SQL 入门

两个数据框拼接

Python 中的 Spark SQL 入门

一个数据框

Python 中的 Spark SQL 入门

拆分数据框 1

Python 中的 Spark SQL 入门

拆分数据框 2

Python 中的 Spark SQL 入门

拆分数据框 3

Python 中的 Spark SQL 入门

拆分数据框 4

Python 中的 Spark SQL 入门

拆分数据框 5

Python 中的 Spark SQL 入门

拆分数据框 6

Python 中的 Spark SQL 入门

分布式数据框

Python 中的 Spark SQL 入门

SQL

Python 中的 Spark SQL 入门

结构化查询语言

Python 中的 Spark SQL 入门

查询

Python 中的 Spark SQL 入门

分布式数据

Python 中的 Spark SQL 入门

分布式数据 + 查询

Python 中的 Spark SQL 入门

加载分隔文本

将逗号分隔文件 trainsched.txt 加载为名为 df 的数据框:

df = spark.read.csv("trainsched.txt", header=True)
Python 中的 Spark SQL 入门

加载分隔文本

df = spark.read.csv("trainsched.txt", header=True)
df.show()
+--------+-------------+-----+
|train_id|      station| time|
+--------+-------------+-----+
|     324|San Francisco|7:59a|
|     324|  22nd Street|8:03a|
|     324|     Millbrae|8:16a|
|     324|    Hillsdale|8:24a|
|     324| Redwood City|8:31a|
|     ...|          ...|  ...|
|     217| Blossom Hill|6:36a|
|     217|      Capitol|6:42a|
|     217|       Tamien|6:50a|
|     217|     San Jose|6:59a|
+--------+-------------+-----+
Python 中的 Spark SQL 入门

Pyspark Shell

Python 中的 Spark SQL 入门

Pyspark Shell 光标

Python 中的 Spark SQL 入门

Pyspark Shell

Python 中的 Spark SQL 入门

Pyspark Shell 光标

Python 中的 Spark SQL 入门

Pyspark Shell

Python 中的 Spark SQL 入门

Python 中的 Spark SQL 入门

Pyspark Shell

Python 中的 Spark SQL 入门

Pyspark Shell 光标

Python 中的 Spark SQL 入门

Pyspark Shell

Python 中的 Spark SQL 入门

Pyspark Shell 光标

Python 中的 Spark SQL 入门

Pyspark Shell

Python 中的 Spark SQL 入门

Pyspark Shell 光标

Python 中的 Spark SQL 入门

Ayo berlatih!

Python 中的 Spark SQL 入门

Preparing Video For Download...