Làm sạch dữ liệu và kiểm tra chất lượng

Chuyển đổi dữ liệu với Spark SQL trong Databricks

Disha Mukherjee

Lead Data Engineer

Ba vấn đề trong dữ liệu thực tế

recraft: half: Một đống tài liệu và tệp rời rạc với lỗi và thiếu sót, tách nền trong suốt

 

  • Thiếu giá trị ở các cột chính
  • Hàng trùng do nạp dữ liệu lặp lại
  • Bản ghi không hợp lệ về logic
Chuyển đổi dữ liệu với Spark SQL trong Databricks

Vì sao cần định nghĩa schema tường minh?

recraft: half: Một nhà khoa học bối rối với dấu hỏi lơ lửng trên các hàng bảng tính, tách nền trong suốt

 

  • Spark có thể đọc sai kiểu cột
  • Cột số có thể bị suy luận thành string
  • StructType định nghĩa kiểu rõ ràng, cho mọi lần chạy
Chuyển đổi dữ liệu với Spark SQL trong Databricks

Nhập khẩu thư viện

from pyspark.sql.types import (
    StructType, StructField,
    StringType, DoubleType, IntegerType, TimestampType
)

from pyspark.sql import functions as F
Chuyển đổi dữ liệu với Spark SQL trong Databricks

Định nghĩa schema

schema = StructType([
    StructField("ID",                 IntegerType(),   True),
    StructField("Date",               TimestampType(), True),
    StructField("Transaction_Amount", DoubleType(),    True),
    StructField("Transaction_Status", StringType(),    True),
    # ... 6 more fields
])

df = (spark.read.format("csv") .option("header", "true") .schema(schema) .load("/Volumes/.../transactions.csv"))
df.printSchema()
 |-- ID: integer (nullable = true)
 |-- Date: timestamp (nullable = true)
 |-- Transaction_Amount: double (nullable = true)
 |-- Transaction_Status: string (nullable = true)
 ...
Chuyển đổi dữ liệu với Spark SQL trong Databricks

Tìm giá trị null

null_counts = df.select([
    F.sum(F.col(c).isNull().cast("int")).alias(c)
    for c in df.columns
])

null_counts.show()
+-----------+------------------+--------+--------+
|Customer_ID|Transaction_Amount|Category|Location|
+-----------+------------------+--------+--------+
|30         |50                |40      |30      |
+-----------+------------------+--------+--------+
Chuyển đổi dữ liệu với Spark SQL trong Databricks

Loại bỏ và điền null

df_no_nulls = df.na.drop(subset=["Customer_ID"])

df_filled = df_no_nulls.na.fill({ "Transaction_Amount": 0.0, "Category": "Unknown", "Location": "Unknown" })
print(f"Before: {df.count()}") print(f"After: {df_filled.count()}")
Before: 100,150
After:  100,120
Chuyển đổi dữ liệu với Spark SQL trong Databricks

Xử lý dữ liệu trùng lặp

total = df_filled.count()
distinct = df_filled.distinct().count()

print(f"Duplicates: {total - distinct}")
Duplicates: 149
Chuyển đổi dữ liệu với Spark SQL trong Databricks

Xử lý dữ liệu trùng lặp

df_deduped = df_filled.dropDuplicates(
    subset=["Customer_ID", "Date", "Transaction_Amount"]
)

print(f"Rows after dedup: {df_deduped.count()}")
Rows after dedup: 99,971
Chuyển đổi dữ liệu với Spark SQL trong Databricks

Lọc bản ghi không hợp lệ

df_valid = df_deduped.filter(F.col("Transaction_Amount") > 0)

df_valid = df_valid.filter( F.col("Transaction_Status") == "Completed" )
print(f"Valid transactions: {df_valid.count()}")
Valid transactions: 33,223
Chuyển đổi dữ liệu với Spark SQL trong Databricks

Tạo cột dẫn xuất

df_enriched = df_valid.withColumn("Revenue_Band",
    F.when(F.col("Transaction_Amount") >= 50000, "High")
     .when(F.col("Transaction_Amount") >= 10000, "Medium")

.otherwise("Low"))
df_enriched.select("Customer_ID", "Transaction_Amount", "Revenue_Band").show(3)
+-----------+------------------+------------+
|Customer_ID|Transaction_Amount|Revenue_Band|
+-----------+------------------+------------+
|CUST003    |5752.36           |Low         |
|CUST009    |28959.12          |Medium      |
|CUST010    |72098.18          |High        |
+-----------+------------------+------------+
Chuyển đổi dữ liệu với Spark SQL trong Databricks

Kiểm tra chất lượng dữ liệu

total_rows = df_enriched.count()
distinct_rows = df_enriched.distinct().count()

null_rate = ( df_enriched.filter(F.col("Customer_ID").isNull()) .count() / total_rows * 100 )
print(f"Total rows: {total_rows}") print(f"Duplicates: {total_rows - distinct_rows}") print(f"Null rate: {null_rate:.2f}%")
Total rows:   33,223
Duplicates:   0
Null rate:    0.00%
Chuyển đổi dữ liệu với Spark SQL trong Databricks

Cùng luyện tập nào!

Chuyển đổi dữ liệu với Spark SQL trong Databricks

Preparing Video For Download...