Skip to main content

Basic Transform Example

Transform Engines

The transform decorator supports two computation engines: PySpark and Pandas. You can specify the engine using the engine parameter.
When using transforms with dependencies (via input_mapping or depends), all connected transforms must use the same engine. For example, if a transform uses the Pandas engine, all transforms it depends on or that depend on it must also use the Pandas engine.

Engine Characteristics

  • PySpark (default):
    • Distributed processing capabilities
    • Better for large-scale data processing
    • Supports all materialization features
  • Pandas:
    • Better for smaller datasets
    • More intuitive Python-native syntax
    • Ideal for local development and testing

Materialization

Materialization is the process of storing the output of a transform function for reuse. This can be useful when a transform function is computationally expensive and its output is used multiple times in the pipeline.
Materialization is allowed for PySpark DataFrames only. So the output of the transform function should be a PySpark DataFrame.
In above code, we used context.resources["pyspark"].spark to access the PySpark session. For more information, check the Context section.
After running the pipeline, the output of the fetch_data function will be stored in Datazone as a dataset. You can check the dataset alias in the Datazone UI or use the datazone dataset list command to list all datasets.

Input Mapping

You can define input mappings to specify the data sources and dependencies for your transform functions. Input mappings enable you to:
  • Chain multiple transform functions
  • Create directed acyclic graphs (DAGs)
  • Connect to different data sources
  • Apply data transformations sequentially
Here are the common input mapping patterns:
  • Input mappings should be defined as a dictionary where the key is the input parameter name and the value is an instance of the Input class.
  • The Input class accepts a Dataset or another transform function as an argument.

Output Mapping

Output mapping defines how the output of a transform function should be stored or mapped. You can specify the output mapping using the output_mapping parameter.
  • Output mapping should be defined as a dictionary where the key is the output parameter name and the value is an instance of the Output class.

Multiple Outputs

Transform Hooks

Transform hooks allow you to execute custom logic on success or failure of a transform function. You can define hooks using the on_success and on_failure parameters in the transform decorator.

Partitioning

Partitioning helps organize and optimize your datasets in our data platform. When you create a transform function, you can specify partition columns using the partition_by parameter.

Why Partition?

  • Improve query performance when filtering by partition columns
  • Efficiently manage large datasets
  • Enable data retention policies by date partitions

Common Partition Strategies

All transformed datasets are stored in Delta Lake format. Choose partition columns based on your most common filtering needs, typically date-based or categorical columns with reasonable cardinality. If you partition by a high-cardinality column, it may lead to a large number of small files, which can impact query performance.

Best Practices

  • Use date partitioning for time-series data
  • Avoid partitioning by columns with high cardinality
  • Consider your query patterns when choosing partition columns

Generator Transforms

Generator transforms allow you to yield data multiple times from a single transform function. This is useful for processing data in chunks, creating multiple batches from a single input, or streaming incremental results.

Usage

Use Python’s yield statement to return data in chunks. Each yielded value creates a separate batch that gets written to the output dataset.
Multiple Transactions: Each yield creates a separate transaction. If your transform fails after some yields have succeeded, the already-written batches will remain in the output dataset. Consider idempotency in your pipeline design.
Mode Must Be Append: Generator transforms require mode="append" in the output mapping. Using mode="overwrite" will cause each yielded batch to overwrite the previous one, leaving only the last batch in the output dataset.