집계 도트 SQL
다음 코드는 SQL을 사용해 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절은 각각Window객체에서 호출하는 도트 표기법 함수로 대응됩니다.- 아래 임포트가 제공됩니다:
- from pyspark.sql import Window
- from pyspark.sql.functions import lead
이 연습은 강의의 일부입니다
Python에서 Spark SQL 입문
연습 안내
- SQL 대신 도트 표기법을 사용해
df와 동일한 결과를 포함하는dot_df라는 데이터프레임을 만드세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Obtain the identical result using dot notation
dot_df = df.withColumn('time_next', ____('time', 1)
.over(____.____('train_id')
.____('time')))