注: 以下の翻訳の正確性は検証されていません。AIPを利用して英語版の原文から機械的に翻訳されたものです。
インクリメンタル計算は、入力データセットに対してトランスフォームを実行し、出力データセットを生成する効率的な方法です。トランスフォームのビルド履歴を活用することで、トランスフォームを実行するたびに出力データセット全体を再計算する必要がなくなります。入力データセットと出力データセットを同じにすることはできません。循環依存関係が生じないように、入力データセットと出力データセットは異なる必要があります。
インクリメンタルパイプラインの作成と管理に関する一連のガイダンスについては、パイプラインの構築セクションを参照してください。
このセクションでは、まずインクリメンタルトランスフォームを使用しないコード例を取り上げ、インクリメンタルトランスフォームの利点を説明します。
Copied!1 2 3 4 5 6 7 8 9 10 11 12 13import polars as pl from transforms.api import transform, Input, Output @transform.using( students=Input('/examples/students_hair_eye_color'), processed=Output('/examples/hair_eye_color_processed') ) def filter_hair_color(students, processed): students_df = students.polars() # 新しいデータだけでなく入力全体に対して filter 関数が実行されるため、非効率です processed.write_table(students_df.filter(pl.col('hair') == 'Brown'))
Copied!1 2 3 4 5 6 7 8 9 10 11 12from transforms.api import transform, Input, Output @transform.using( students=Input('/examples/students_hair_eye_color'), processed=Output('/examples/hair_eye_color_processed') ) def filter_hair_color(students, processed): students_df = students.polars() # 新しいデータだけでなく入力全体に対して filter 関数が実行されるため、非効率です processed.write_table(students_df[students_df['hair'] == 'Brown'])
Copied!1 2 3 4 5 6 7 8 9 10from transforms.api import transform, Input, Output @transform.using( students=Input('/examples/students_hair_eye_color'), processed=Output('/examples/hair_eye_color_processed') ) def filter_hair_color(ctx, students, processed): conn = ctx.duckdb().conn query = conn.sql("SELECT * FROM students WHERE hair = 'Brown'") processed.write_table(query)
Copied!1 2 3 4 5 6 7 8 9 10 11 12from transforms.api import transform, Input, Output @transform.spark.using( students=Input('/examples/students_hair_eye_color'), processed=Output('/examples/hair_eye_color_processed') ) def filter_hair_color(students, processed): students_df = students.dataframe() # 新しいデータだけでなく入力全体に対して filter 関数が実行されるため、非効率です processed.write_dataframe(students_df.filter(students_df.hair == 'Brown'))
入力データセット /examples/students_hair_eye_color に新しいデータが追加されると、入力に追加された新しいデータだけでなく、入力全体に対して filter() が実行されます。これは計算リソースと時間の両方を浪費します。
トランスフォームが自身のビルド履歴を認識できれば、出力をより効率的に計算できます。具体的には、入力に加えられた変更を使用して、出力データセットを変更できます。このように、テーブルを再実体化するときに、すでに実体化されているデータを使用する処理をインクリメンタル計算と呼びます。インクリメンタル計算を使用しない場合、出力データセットは常に最新のトランスフォーム出力で置き換えられます。
前述のトランスフォームの例に戻ります。このトランスフォームは、students データセットに対して filter() を実行し、髪が茶色の学生を書き出します。インクリメンタル計算を使用すると、新たに2人の学生のデータが students に追加された場合、トランスフォームはビルド履歴の情報を使用して、新しく追加された学生のうち髪が茶色の学生だけを出力に追加できます。
+---+-----+-----+------+ +---+-----+-----+------+
| id| hair| eye| sex| | id| hair| eye| sex|
+---+-----+-----+------+ Build 1 +---+-----+-----+------+
| 17|Black|Green|Female| ---------> | 18|Brown|Green|Female|
| 18|Brown|Green|Female| +---+-----+-----+------+
| 19| Red|Black|Female|
+---+-----+-----+------+
... ...
+---+-----+-----+------+ Build 2 +---+-----+-----+------+
| 20|Brown|Amber|Female| ---------> | 20|Brown|Amber|Female|
| 21|Black|Blue |Male | +---+-----+-----+------+
+---+-----+-----+------+
したがって、前述のトランスフォームの例は、次の構文でインクリメンタルロジックを使用するように書き換えられます。
Copied!1 2 3 4 5 6 7 8 9 10 11import polars as pl from transforms.api import transform, incremental, Input, Output @incremental() @transform.using( students=Input('/examples/students_hair_eye_color'), processed=Output('/examples/hair_eye_color_processed') ) def filter_hair_color(students, processed): students_df = students.polars('added') processed.write_table(students_df.filter(pl.col('hair') == 'Brown'))
Copied!1 2 3 4 5 6 7 8 9 10from transforms.api import transform, incremental, Input, Output @incremental() @transform.using( students=Input('/examples/students_hair_eye_color'), processed=Output('/examples/hair_eye_color_processed') ) def filter_hair_color(students, processed): students_df = students.pandas('added') processed.write_table(students_df[students_df['hair'] == 'Brown'])
Copied!1 2 3 4 5 6 7 8 9 10 11from transforms.api import transform, incremental, Input, Output @incremental() @transform.using( students=Input('/examples/students_hair_eye_color'), processed=Output('/examples/hair_eye_color_processed') ) def filter_hair_color(ctx, students, processed): conn = ctx.duckdb(read_modes={students: "added"}).conn query = conn.sql("SELECT * FROM students WHERE hair = 'Brown'") processed.write_table(query)
Copied!1 2 3 4 5 6 7 8 9 10from transforms.api import transform, incremental, Input, Output @incremental() @transform.spark.using( students=Input('/examples/students_hair_eye_color'), processed=Output('/examples/hair_eye_color_processed') ) def filter_hair_color(students, processed): students_df = students.dataframe('added') processed.write_dataframe(students_df.filter(students_df.hair == 'Brown'))
インクリメンタルトランスフォームと @incremental デコレーターの詳細については、インクリメンタルトランスフォームのリファレンスを参照してください。
Code Repositories のプレビュー機能では、incremental() デコレーターに require_incremental=True を渡しても、常に非インクリメンタルモードでトランスフォームが実行されます。VS Code のプレビューでは、インクリメンタルな動作が反映されます。
一方のデータセットが全体を読み込む参照テーブルで、もう一方がインクリメンタルに読み込むインクリメンタルデータセットである場合、結合を安全に計算できます。ただし、結合対象の両方のデータセットをインクリメンタルに読み込む場合は、特別な処理が必要です。詳細については、インクリメンタルトランスフォームを活用して大規模なデータセットを結合する例を参照してください。
ストリーミングデータセットに対してインクリメンタルな軽量トランスフォームを実行すると、インクリメンタルウィンドウに処理対象の新しいファイルが含まれない場合があります。基盤となる統合では空でないファイルリストが想定されているため、空の入力に対して .polars(lazy=True) を呼び出すと、BinderException が発生する可能性があります。このエラーを回避するには、コンテキストの is_incremental プロパティを使用し、.polars(lazy=True) を呼び出す前に入力にデータがあるか確認します。
Copied!1 2 3 4 5 6 7 8 9 10 11 12 13from transforms.api import transform, incremental, Input, Output @incremental() @transform.using( my_output=Output('/path/to/output'), my_input=Input('/path/to/input'), ) def compute(ctx, my_output, my_input): if ctx.is_incremental and my_input.polars().is_empty(): return df = my_input.polars(lazy=True) my_output.write_table(df)
または、トランスフォームを実行する前に、インクリメンタルウィンドウに常に1つ以上のファイルが含まれるようにすることもできます。遅延 API の詳細については、Polars 遅延 API のドキュメントを参照してください。
メディアセットでは、インクリメンタル計算がサポートされています。詳細については、インクリメンタルメディアセットのドキュメントを参照してください。
メディアセット入力を含むインクリメンタルトランスフォームに対して、トランザクション型データセットのバッチ処理を設定できます。詳細については、インクリメンタル入力のバッチサイズの制限に関するドキュメントを参照してください。メディアセットについては、インクリメンタルメディアセット入力のバッチサイズの制限を参照してください。