データパイプラインの監視

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

練習しましょう!

Python で学ぶ ETL と ELT

Preparing Video For Download...