データ接続と統合Python基本的なトランスフォームデバッグ

注: 以下の翻訳の正確性は検証されていません。AIPを利用して英語版の原文から機械的に翻訳されたものです。

Python トランスフォームをデバッグする

このガイドでは、Python トランスフォームで利用可能なデバッグ手法の概要を説明します。エラーと例外について詳しくは、Python ドキュメント ↗を参照してください。

デバッガーを使用する

デバッガーは、Python トランスフォームの問題を特定して解決するための便利なツールです。ブレークポイントを設定してトランスフォームの実行を一時停止し、変数の調査、DataFrame の表示、関数やライブラリの動作の把握を行えます。

使用する IDE のデバッガーについて詳しくは、次を参照してください。

Python のトレースバックを読み取る

Python のトレースバックは、エラーに至るまでの関数呼び出しの順序を含むエラーメッセージで、ほかのプログラミング言語ではスタックトレースとも呼ばれます。Python では、処理されなかった例外があるとトレースバックが出力され、最後に行われた呼び出しが一番下に表示されます。

Python トランスフォームの実行時の失敗は、ほとんどの場合トレースバックとして表示されるため、その読み方を理解することが重要です。

次のコード例を考えてみます。

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 class Stats(object): nums = [] def add(self, n): self.nums.append(n) def sum(self): return sum(self.nums) def avg(self): return self.sum() / len(self.nums) def main(): stats = Statistics() stats.add(1) stats.add(2) stats.add(3) print(stats.avg()) main()

このコードを実行すると、次のトレースバックが出力されます。

Copied!
1 2 3 4 5 6 Traceback (most recent call last): File "test.py", line 23, in <module> main() File "test.py", line 16, in main stats = Statistics() NameError: name 'Statistics' is not defined

ほかのプログラミング言語のスタックトレースとは異なり、Python のトレースバックでは最後に行われた呼び出しが末尾に表示されます。下から上に読むと、トレースバックには次の内容が表示されています。

  • 例外名の NameError ↗。Python の組み込み例外クラス ↗は多数ありますが、コードで独自の例外クラスを定義することもできます。
  • 例外メッセージの name 'Statistics' is not defined。このメッセージには、デバッグに最も役立つ情報が含まれています。
  • 例外が送出されるまでの関数呼び出しの順序。File "test.py", line 23, in <module> に続いて、該当するコード行(16行目)が表示されます。

このトレースバックから、test.py の main メソッド内の16行目で例外が発生していることがわかります。具体的には、エラーの原因となるコード行は stats = Statistics() で、送出された例外は NameError です。このことから、Statistics という名前が存在しないと推測できます。コード例を振り返ると、Statistics の代わりに Stats という名前を使用すべきだったと考えられます。

ロギング

ロギングには、次のオプションを使用できます。

  • 単純な print 文。この方法は、標準(軽量)トランスフォームではサポートされていますが、Spark トランスフォームではサポートされていません。
  • 標準の Python logging モジュール ↗。この方法はすべての種類のトランスフォームでサポートされていますが、保存されるのは INFO レベル以上のログのみです。

ログは、VS Code では出力、Builds アプリケーションではアクション > ログを見るから確認できます。

次のコード例は、デバッグに役立つログを出力する方法を示しています。

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 from transforms.api import transform, Input, Output import polars as pl import logging log = logging.getLogger(__name__) @transform.using( output=Output("/path/output"), input=Input("/path/input"), ) def my_compute_function(output, input): input_df = input.polars() log.info("Number of rows: %d", input_df.height) print("Number of columns: " + str(input_df.width)) output.write_table(input_df)
Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 from transforms.api import transform, Input, Output import polars as pl import logging log = logging.getLogger(__name__) @transform.using( output=Output("/path/output"), input=Input("/path/input"), ) def my_compute_function(ctx, output, input): conn = ctx.duckdb().conn input_df = conn.sql("SELECT * FROM input").fetchdf() log.info("Number of rows: %d", input_df.shape[0]) print("Number of columns: " + str(input_df.shape[1])) output.write_table(input_df)
Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 from transforms.api import transform, Input, Output import pandas as pd import logging log = logging.getLogger(__name__) @transform.using( output=Output("/path/output"), input=Input("/path/input"), ) def my_compute_function(output, input): input_df = input.pandas() log.info("Number of rows: %d", input_df.shape[0]) print("Number of columns:" + str(input_df.shape[1])) output.write_table(input_df)
Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 from transforms.api import transform_df, Input, Output import logging log = logging.getLogger(__name__) @transform_df( Output("/path/output"), input=Input("/path/input"), ) def my_compute_function(input): log.info("Number of rows: %d", input.count()) log.info("Number of columns: %d", len(input.columns)) return input

Spark トランスフォームでのログの扱いについて詳しくは、Spark ロギングを参照してください。

ビルドメトリクス

テレメトリの統計タブを使用すると、メモリー不足 (OOM) エラーやビルドの遅延など、リソース関連の問題を診断できます。このタブにはメモリーと CPU の使用率グラフが表示され、トランスフォームがメモリーに制約されているか、CPU に制約されているか、または割り当てられたリソースを十分に使用していないかを確認できます。

ビルドメトリクスにアクセスするには、次の手順を実行します。

  1. データセットプレビュー、Data Lineage、または Builds アプリケーションからビルドレポートを表示します。
  2. ビルドからジョブを選択し、テレメトリ > 統計に移動します。

メモリーと CPU の使用率メトリクスの読み取り方について詳しくはこちらを参照してください。

テレメトリの統計タブは、軽量コンピュートエンジンを使用するトランスフォームでは使用できますが、Spark トランスフォームでは使用できません。