Spark SQL

Introduction to Spark SQL in Python

Mark Plutowski Phd

Data Scientist

สร้างตาราง SQL และคิวรีข้อมูล

เคอร์เซอร์ PySpark Shell

Introduction to Spark SQL in Python

โหลด dataframe จากไฟล์

df = spark.read.csv(filename)
df = spark.read.csv(filename, header=True)
Introduction to Spark SQL in Python

สร้างตาราง 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|
+--------+--------+-----+
Introduction to Spark SQL in Python

ตรวจสอบ schema ของตาราง

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)
Introduction to Spark SQL in Python

เฟรม

Introduction to Spark SQL in Python

Data Frame

Introduction to Spark SQL in Python

ข้อมูลแบบตาราง

+--------+-------------+-----+
|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|
+--------+-------------+-----+
Introduction to Spark SQL in Python

dataframe สองชุด

Introduction to Spark SQL in Python

dataframe สองชุดที่เชื่อมต่อกัน

Introduction to Spark SQL in Python

dataframe ชุดเดียว

Introduction to Spark SQL in Python

การแบ่ง dataframe 1

Introduction to Spark SQL in Python

การแบ่ง dataframe 2

Introduction to Spark SQL in Python

การแบ่ง dataframe 3

Introduction to Spark SQL in Python

การแบ่ง dataframe 4

Introduction to Spark SQL in Python

การแบ่ง dataframe 5

Introduction to Spark SQL in Python

การแบ่ง dataframe 6

Introduction to Spark SQL in Python

การแบ่ง dataframe แบบกระจาย

Introduction to Spark SQL in Python

SQL

Introduction to Spark SQL in Python

Structured Query Language

Introduction to Spark SQL in Python

การคิวรี

Introduction to Spark SQL in Python

ข้อมูลแบบกระจาย

Introduction to Spark SQL in Python

ข้อมูลแบบกระจาย + ข้อมูล

Introduction to Spark SQL in Python

การโหลดข้อความที่คั่นด้วยตัวคั่น

โหลดไฟล์ trainsched.txt ที่คั่นด้วยคอมมาลงใน dataframe ชื่อ df:

df = spark.read.csv("trainsched.txt", header=True)
Introduction to Spark SQL in Python

การโหลดข้อความที่คั่นด้วยตัวคั่น

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|
+--------+-------------+-----+
Introduction to Spark SQL in Python

PySpark Shell

Introduction to Spark SQL in Python

เคอร์เซอร์ PySpark Shell

Introduction to Spark SQL in Python

PySpark Shell

Introduction to Spark SQL in Python

เคอร์เซอร์ PySpark Shell

Introduction to Spark SQL in Python

PySpark Shell

Introduction to Spark SQL in Python

Introduction to Spark SQL in Python

PySpark Shell

Introduction to Spark SQL in Python

เคอร์เซอร์ PySpark Shell

Introduction to Spark SQL in Python

PySpark Shell

Introduction to Spark SQL in Python

เคอร์เซอร์ PySpark Shell

Introduction to Spark SQL in Python

PySpark Shell

Introduction to Spark SQL in Python

เคอร์เซอร์ PySpark Shell

Introduction to Spark SQL in Python

มาฝึกกันเถอะ!

Introduction to Spark SQL in Python

Preparing Video For Download...