開始使用免費開始

彙總 dot SQL

下列程式碼使用 SQL 來設定名為 df 的 DataFrame:

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 BYORDER BY 子句各自都有對應的點記法函式,並呼叫在 Window 物件上。
  • 可用的匯入如下:
    • from pyspark.sql import Window
    • from pyspark.sql.functions import lead

本練習屬於課程

Python Spark SQL 入門

檢視課程

練習說明

  • 建立名為 dot_df 的 DataFrame,使用點記法而非 SQL,產生與 df 完全相同的結果。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Obtain the identical result using dot notation 
dot_df = df.withColumn('time_next', ____('time', 1)
        .over(____.____('train_id')
        .____('time')))
編輯並執行程式碼