RDD에서 집계하기
이제 PySpark에서 DataFrame으로 분석을 수행해 보았으니, RDD로도 비슷한 작업을 간단히 해 보겠습니다. 제공된 코드를 사용해 PySpark에서 RDD 값의 합계를 구하세요.
spark라는 Spark 세션은 미리 만들어져 있습니다.
이 연습은 강의의 일부입니다
PySpark 입문
연습 안내
- 제공된 DataFrame에서 RDD를 생성하세요.
- 제공된 Lambda 함수를 RDD의 키에 적용하세요.
- 집계 결과를 수집(collect)하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# DataFrame Creation
data = [("HR", "3000"), ("IT", "4000"), ("Finance", "3500")]
columns = ["Department", "Salary"]
df = spark.createDataFrame(data, schema=columns)
# Map the DataFrame to an RDD
rdd = df.rdd.____(lambda row: (row["Department"], row["Salary"]))
# Apply a lambda function to get the sum of the DataFrame
rdd_aggregated = rdd.____(lambda x, y: x + y)
# Show the collected Results
print(rdd_aggregated.____())