定义 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(), ____)
____
])