เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

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.functions
  • PARTITION 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')))
แก้ไขและรันโค้ด