Introduction
With data ecosystems constantly growing in size and complexity, a need for resillient data pipelines with fast time to market is becoming more and more important. This encourages companies to reach for the new methodologies for their ETL processes such as declarative pipelines, which allow developers to focus on defining what the desired state of data should look like without worrying about the task execution order.
Apache Spark™ Declarative Pipelines (SDP), the open-source declarative framework in Apache Spark for building batch and streaming data pipelines, applies this approach and provides a data processing solution that handles transformation logic for you along with some other useful capabilities such as automated data quality checks, monitoring and observability. Readers may recognize its predecessor on Databricks as Delta Live Tables (DLT).
Declarative approach to data pipelines
The usage of declarative approach isn’t unique to data engineering. In DevOps, tools like Kubernetes and Terraform have revolutionized deployment practices by introducing the concept of Infrastructure as Code, which allows engineers to define the desired state of infrastructure and environments rather than specifying each operational step. Similarly, declarative pipelines let engineers define the final state of the database, leaving the operational activities to the underlying system.
Traditionally, ETL pipelines are created by explicitly specifying the order of task execution focusing on the relationships between the tasks to make sure that the results are consistent, without missing required data and avoiding duplicates.
Typical process can be defined in following steps:
- Download the files from the storage.
- Load the data to the staging tables.
- Perform transformations on the data, combining data from different sources.
- Load to the final destination and perform data quality checks.
Although this imperative approach is flexible and well known, it might become difficult to scale and maintain as the pipeline grows in complexity, because when the steps are explicitly defined the developer has to reflect the data lineage in the pipeline manually therefore it can be challenging to ensure the best performance with parallelisation, scaling and with minimal costs.
Declarative pipelines on the other hand don’t require explicit specification of the transformation steps order. Pipeline author specifies how the final data state should look like, leaving the pipeline engine to automatically determine the actual steps to achieve it, i.e. the order of transformations, scheduling tasks, handling dependencies, resource allocation and optimization. In theory, this approach seems to be a great option, because it brings scalability and simplicity to increasingly complex data solutions.
This method may be a great option in many use cases, but when deciding whether we should use the declarative framework we need to consider the relative lack of flexibility, as those solutions abstract away operational details, which can make it harder to debug and troubleshoot issues. Also vendor lock-in might be an issue because of proprietary syntax or platform-specific integrations.
Introducing Spark Declarative Pipelines
Spark Declarative Pipelines (SDP) is a declarative ETL framework for building scalable and reliable data processing pipelines. It lets users focus on the transformations and desired data structures, while automatically managing orchestration, compute infrastructure, data quality and error handling.
What is the difference between Spark Declarative Pipelines and Delta tables? Delta table is a default data table format in Databricks, which basically extends parquet file format allowing to perform ACID transactions. Spark Declarative Pipelines however let you describe how the data flows between those tables by creating them and managing updates.
SDP uses the following datasets to maintain results of declarative queries:
- Streaming table - Delta table with support of streaming and incremental data processing. It is always defined against a data source that is continuously or incrementally growing. Those tables are designed for append-only data sources and natively support Databricks Autoloader which enables efficient ingestion of cloud files.
- Temporary view - logical dataset that is computed every time it’s queried. It’s useful for dividing large statements into easier-to-manage queries. Spark Declarative Pipelines don’t publish temporary views outside the pipeline so they can be useful as intermediate queries that shouldn’t be exposed to the users.
- Materialized view - view with precomputed results. Those datasets are refreshed each time the pipeline updates to reflect changes in the upstream datasets that might have been changed. Materialized views are useful when multiple downstream queries use them as they don’t need to be computed every time when queried, unlike regular views.
Spark Declarative Pipelines use those datasets to define a pipeline, which is a main unit to run data processing workflows. A pipeline is generally defined in a source file (notebook or python file) that contains all definitions of streaming tables and materialized views that are declared in SQL or in python. After creating the pipeline code with its logic we need to create the pipeline itself by providing its configuration. Although most settings are optional and the platform can proceed with the defaults, we need to consider the default catalog and schema, because this is where the pipeline publishes its tables in Unity Catalog and makes them available outside the pipeline.
Some key features of the Spark Declarative Pipelines framework are listed below:
- Automated dependency management and pipeline visualization - SDP determines dependencies across the pipeline and renders a data flow graph to visualize data lineage while checking for syntax errors.
- Data quality enforcement - we can specify expectations to control data accuracy while providing flexibility with the method of processing the invalid records. SDP can fail the whole pipeline, omit the erroneous rows or write the invalid records while reporting the failure in the dataset metrics.
- Incremental and streaming data processing - On Databricks, SDP supports Autoloader, which allows to stream the cloud files in real-time without specifying additional cloud services and reducing the amount of data being processed in comparison with batch data reloads.
- Built-in monitoring and observability - provides monitoring and logging capabilities. We can track the runtime statistics such as records processed, used runtime, observe pipeline health and monitor data quality trends.
SDP pipeline example
Pipelines description
In this chapter a sample SDP pipeline will be created to demonstrate the capabilities of the framework. This example shows the pipeline that uses data provided by Databricks available in the /databricks-datasets on DBFS, specifically retail-org dataset.
The process will read sales orders data, join with the table containing product information and retrieve the list of top selling product categories. We’ll demonstrate how to read CSV and JSON data to a bronze layer table, read raw data and use an SDP expectation to write cleansed data to the silver layer and finally write aggregated data to the gold layer table.
Requirements to run the workflow on your Databricks workspace:
- cluster creation permission (not needed with serverless compute) - SDP runtime creates cluster before running pipeline, so this pipeline will fail if you don’t have such permissions in your workspace
- access to read/write data in a Unity Catalog schema - pipeline will output data as tables in Unity Catalog
- make sure your workspace has Spark Declarative Pipelines enabled as some older workspaces without premium pricing tier might not have this feature available
Steps for pipeline creation
First, go to Jobs & Pipelines in the sidebar and click New → ETL pipeline. This opens the Lakeflow Pipelines Editor, where the pipeline source code is kept in SQL or Python files. Add the following SQL snippets to a SQL file in the pipeline.
Here we specify the bronze layer corresponding to the data ingestion, which uses Autoloader capabilities to incrementally load new data as it arrives. This creates streaming tables for sales orders and products. The data is read straight from csv/json files and is made available as tables for other pipeline steps.
CREATE OR REFRESH STREAMING TABLE sales_ordersCOMMENT "List of sales, ingested from /databricks-datasets."TBLPROPERTIES ("dataLayer" = "bronze")ASSELECT * FROM STREAM read_files("/databricks-datasets/retail-org/sales_orders/", format => "json", inferColumnTypes => true); CREATE OR REFRESH STREAMING TABLE productsCOMMENT "List of products, ingested from /databricks-datasets."TBLPROPERTIES ("dataLayer" = "bronze")AS SELECT * FROM STREAM read_files("/databricks-datasets/retail-org/products/", format => "csv", sep => ";", header => true)
In this step, we combine sales data with products to create a sales table with an added product category column. Notice that other datasets defined in the pipeline are referenced simply by name, without any prefix, and STREAM() reads sales_orders incrementally as a stream. We also applied a data quality check which will stop pipeline execution if there is a product_id that exists in sales_orders table but not in products.
CREATE OR REFRESH STREAMING TABLE product_sales_categorised( CONSTRAINT valid_products EXPECT (mapping_product_id IS NOT NULL) ON VIOLATION FAIL UPDATE)PARTITIONED BY (product_category)COMMENT "Product sales with product category"TBLPROPERTIES ("dataLayer" = "silver")ASSELECT product_details.id AS product_id, product_details.curr AS currency, product_details.name AS product_name, product_details.price AS product_price, product_details.qty AS quantity, product_details.unit AS product_unit, product_category, product_id AS mapping_product_idFROM( SELECT explode(ordered_products) AS product_details FROM STREAM(sales_orders)) sLEFT JOIN products p ON s.product_details.id = p.product_id
Finally, we create a gold table which aggregates product data by category and filters only USD currency.
CREATE OR REFRESH MATERIALIZED VIEW top_productsCOMMENT "List of top selling products in USD"TBLPROPERTIES ("dataLayer" = "gold")ASSELECT product_category, format_number(SUM(product_sales), 'USD ,###') AS total_salesFROM( SELECT product_category, product_price * quantity AS product_sales FROM product_sales_categorised WHERE currency = 'USD')GROUP BY product_categoryORDER BY SUM(product_sales) DESC
Note: pipeline source code is not designed to be run cell by cell like a regular notebook. The queries are executed as part of the pipeline: in the Lakeflow Pipelines Editor you can use Dry run to validate the code without writing any data, Run file to update only the datasets defined in the current file, or Run pipeline to update all of them.
Now when we have our code ready we can go ahead and configure the pipeline:
- Open the pipeline settings.
- Provide pipeline details:
- Name
- Product edition: Advanced, needed for Data Quality
- Default catalog and schema - put any Unity Catalog schema you have access to
- (optional) compute details - serverless compute is the recommended option.
- Save the settings.
- Run the pipeline by clicking Run pipeline in the top panel.
Pipeline execution and results
Once we run the pipeline we can see the execution graph with all defined datasets. Here we can see the advantage of the declarative approach, because we didn’t explicitly say that the product_sales_category should run before products and sales_orders, but the execution engine figured it out for us.

When we click on one of the pipeline steps we can see details of the dataset. From here we can go straight to the data catalog to see how the data looks like, which wouldn’t be an option if we didn’t specify the target schema when defining the pipeline.


We can also check our DQ expectation defined on the product_sales_categorised table. There are no failures because all product ids from sales_orders were available in the product table.

Because the source code and the pipeline live together in the Lakeflow Pipelines Editor, we can modify, validate and run the pipeline from one place. It allows easier pipeline modifications and debugging because we don’t have to go back and forth between the code and the pipeline UI.

Conclusion
Declarative data pipelines simplify the creation and maintenance of complex workflows, allowing data teams to focus on insights over infrastructure. While there are trade-offs in flexibility and possible vendor lock-in, the benefits of faster deployment, easier maintenance and a focus on the final state of the database make declarative pipelines a powerful tool for modern data solutions.
Spark Declarative Pipelines is an example of a declarative approach. It lets users create reliable data processing pipelines using python or SQL. Apart from the automatic task orchestration it provides some other useful features, such as data quality controls, incremental data processing and observability and allows you to leverage the advantages of Databricks platform
Gcp google cloud platform logging monitoring alerting quick introduction
Introduction to data quality terms definitions examples of use

