监控数据管道

使用 Python 的 ETL 和 ELT

Jake Roach

Data Engineer

监控数据管道

应监控数据管道中的数据变化和执行失败

  • 缺失数据
  • 数据类型漂移
  • 包弃用或功能变更

$$

数据管道的日志记录工作流。

使用 Python 的 ETL 和 ELT

记录数据管道性能

  • 记录执行时的性能
  • 失败时提供排查起点
import logging
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)

# Create different types of logs
logging.debug(f"Variable has value {path}")
logging.info("Data has been transformed and will now be loaded.")
DEBUG: Variable has value raw_file.csv
INFO: Data has been transformed and will now be loaded.
使用 Python 的 ETL 和 ELT

记录警告与错误

import logging
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)

# Create different types of logs
logging.warning("Unexpected number of rows detected.")
logging.error("{ke} arose in execution.")
WARNING: Unexpected number of rows detected.
ERROR: KeyError arose in execution.
使用 Python 的 ETL 和 ELT

使用 try-except 处理异常

try:
    # Execute some code here
    ...

except:
    # Logging about failures that occured
    # Logic to execute upon exception
    ...

  • 出错时可执行应对代码
使用 Python 的 ETL 和 ELT

用 try-except 处理特定异常

except 子句中传入具体异常

try:
    # Try to filter by price_change
    clean_stock_data = transform(raw_stock_data)
    logging.info("Successfully filtered DataFrame by 'price_change'")

except KeyError as ke:
    # Handle the error, create new column, transform
    logging.warning(f"{ke}: Cannot filter DataFrame by 'price_change'")
    raw_stock_data["price_change"] = raw_stock_data["close"] - raw_stock_data["open"]
    clean_stock_data = transform(raw_stock_data)

使用 Python 的 ETL 和 ELT

Passons à la pratique !

使用 Python 的 ETL 和 ELT

Preparing Video For Download...