Incremental Python Transform
In this guide, we'll build an Incremental Python Transform that processes only new or changed data to improve pipeline performance.
Check out our concept guides to learn about incremental processing strategies and supported input formats.
Prerequisites
- Ascend Flow
Create a Transform
You can create a Transform in two ways: through the form UI or directly in the Files panel.
- Form
- Files panel
- Double-click the Flow where you want to add your Transform
- Right-click on an existing component (typically a Read component or another Transform) that will provide input data
- Select Create Downstream → Transform
- Complete the form with these details:
- Select your Flow
- Enter a descriptive name for your Transform (e.g.,
sales_aggregation
) - Choose the appropriate file type for your Transform logic
- Open the files panel in the top left corner
- Navigate to and select your desired Flow
- Right-click on the components directory and choose New file
- Name your file with a descriptive name that reflects its purpose (e.g.,
sales_aggregation
) - Choose the appropriate file extension based on your Transform type:
.py
for Python Transforms.sql
for SQL Transforms
Create your Python Transform
Structure your Python Transform following this pattern:
-
Import necessary packages: Import Ascend resources like
transform
andref
from theascend.resources
module -
Apply the
@transform()
decorator: Configure it with:inputs
: List of input datasets usingref()
materialized
: Set to"incremental"
to enable incremental processingincremental_strategy
: Use"merge"
for updating existing recordsunique_key
: Specify a column that uniquely identifies recordsmerge_update_columns
: List columns that should be updated during merges
-
Define your transform function: Create a function that takes input data and
context
parameters -
Use incremental context: Check
context.is_incremental
and usecontext.current_data()
to access existing data -
Filter for new data: Use incremental state to process only new or changed records
- Compare current data with previous state using timestamps or IDs
# Example of filtering for new data
if context.is_incremental:
current_data = context.current_data()
# Only process records with newer timestamps
output = output[output["timestamp"] > current_data["timestamp"].max()] -
Return the processed data: Send your transformed data back to Ascend
- Return a properly structured dataframe or table object
Example
This example demonstrates how to create an incremental transform that only processes new or updated records:
"""
Example of an Incremental Python Transform Component.
This file demonstrates how to create a transform that processes only new or
changed data to improve pipeline performance using the incremental strategy.
"""
from ascend.resources import ref, transform
@transform(
inputs=[ref("incrementing_data")],
materialized="incremental",
incremental_strategy="merge",
unique_key="key",
🎉 Congratulations! You've successfully created an Incremental Python Transform in Ascend.