Làm sạch dữ liệu với PySpark
Mike Metzger
Data Engineering Consultant
Làm sạch dữ liệu: Chuẩn bị dữ liệu thô cho các pipeline xử lý dữ liệu.
Các tác vụ có thể có trong làm sạch dữ liệu:
Vấn đề với các hệ thống dữ liệu điển hình:
Ưu điểm của Spark:
Dữ liệu thô:
| name | age (years) | city |
|---|---|---|
| Smith, John | 37 | Dallas |
| Wilson, A. | 59 | Chicago |
| null | 215 |
Dữ liệu đã làm sạch:
| last name | first name | age (months) | state |
|---|---|---|---|
| Smith | John | 444 | TX |
| Wilson | A. | 708 | IL |
Nhập 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)
])
Đọc tệp CSV chứa dữ liệu
people_df = spark.read.format('csv').load(name='rawdata.csv', schema=peopleSchema)
Làm sạch dữ liệu với PySpark