定義 schema
建立明確的 schema 有助於提升資料品質與匯入效能。就像課程中提到的,我們會建立一個簡單的 schema,讀入以下欄位:
- Name
- Age
- City
Name 與 City 欄位為 StringType(),Age 欄位為 IntegerType()。
本練習屬於課程
使用 PySpark 清理資料
練習說明
- 從
pyspark.sql.types函式庫匯入*。 - 使用
StructType方法定義一個新的 schema。 - 為
name、age、city定義StructField。每個欄位都要對應正確的資料型別,且不可為nullable。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import the pyspark.sql.types library
____
# Define a new schema using the StructType method
people_schema = ____([
# Define a StructField for each field
StructField('name', ____, False),
____('____', IntegerType(), ____)
____
])