Aggregate dot SQL
โค้ดต่อไปนี้ใช้ SQL เพื่อกำหนดค่าให้กับ dataframe ชื่อ df
df = spark.sql("""
SELECT *,
LEAD(time,1) OVER(PARTITION BY train_id ORDER BY time) AS time_next
FROM schedule
""")
LEADมีฟังก์ชันที่เทียบเท่าอยู่ในpyspark.sql.functionsPARTITION BYและORDER BYต่างก็มีฟังก์ชัน dot notation ที่เทียบเท่า ซึ่งเรียกใช้บนออบเจกต์Window- มีการ import ต่อไปนี้ให้แล้ว:
- from pyspark.sql import Window
- from pyspark.sql.functions import lead
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Introduction to Spark SQL in Python
คำแนะนำการฝึกหัด
- สร้าง dataframe ชื่อ
dot_dfที่มีผลลัพธ์เหมือนกับdfทุกประการ โดยใช้ dot notation แทน SQL
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Obtain the identical result using dot notation
dot_df = df.withColumn('time_next', ____('time', 1)
.over(____.____('train_id')
.____('time')))