開始使用免費開始

定義 schema

建立明確的 schema 有助於提升資料品質與匯入效能。就像課程中提到的,我們會建立一個簡單的 schema,讀入以下欄位:

  • Name
  • Age
  • City

NameCity 欄位為 StringType()Age 欄位為 IntegerType()

本練習屬於課程

使用 PySpark 清理資料

檢視課程

練習說明

  • pyspark.sql.types 函式庫匯入 *
  • 使用 StructType 方法定義一個新的 schema。
  • nameagecity 定義 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(), ____)
  ____
])
編輯並執行程式碼