使用 PySpark 进行数据清洗
Mike Metzger
Data Engineering Consultant
数据清洗:为数据处理流水线准备原始数据。
数据清洗的可能任务:
典型数据系统的问题:
Spark 的优势:
原始数据:
| name | age (years) | city |
|---|---|---|
| Smith, John | 37 | Dallas |
| Wilson, A. | 59 | Chicago |
| null | 215 |
清洗后:
| last name | first name | age (months) | state |
|---|---|---|---|
| Smith | John | 444 | TX |
| Wilson | A. | 708 | IL |
导入 schema
import pyspark.sql.types
peopleSchema = StructType([
# Define the name field
StructField('name', StringType(), True),
# Add the age field
StructField('age', IntegerType(), True),
# Add the city field
StructField('city', StringType(), True)
])
读取包含数据的 CSV 文件
people_df = spark.read.format('csv').load(name='rawdata.csv', schema=peopleSchema)
使用 PySpark 进行数据清洗