Spark Expectations is a Python library by Nike-Inc that facilitates in-flight data quality (DQ) checks within Apache Spark jobs. It enables validation of data against defined rules (row-level, aggregate, and query-based) as data is processed, ensuring only quality data reaches its destination. Erroneous records are quarantined into a separate error table, and aggregated metrics are provided. The library is actively maintained with regular updates; the current version is 2.9.1.
pip install spark-expectationsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up `SparkExpectations`, define data quality rules, and apply them to a Spark DataFrame using the `@se.with_expectations` decorator. It includes steps for initializing Spark, creating a sample DataFrame, defining mock rules, configuring the `SparkExpectations` instance, and running the decorated function to process and validate data.
Review Databricks Serverless documentation and `spark-expectations` specific guidance for serverless deployments. Consider using alternative deployment models if conflicts persist. If possible, ensure the `pyspark` version required by `spark-expectations` is compatible with the Databricks runtime.
Always include `option("checkpointLocation", "path/to/checkpoint")` in your `WrappedDataFrameStreamWriter` configuration for streaming target and error tables.Ensure `rules_df` is provided during `SparkExpectations` instantiation and `stats_table`, `error_records_table_name`, and `target_table` are correctly configured in `user_conf` and passed to the `@se.with_expectations` decorator.
Carefully review the `spark-expectations` documentation on `action_if_failed` for each rule type to align with desired data handling and job failure policies.
Thoroughly test `spark-expectations` jobs when migrating to new major Apache Spark versions. Consult Spark migration guides for details on compatibility issues and necessary code adjustments, especially concerning SQL syntax and NULL handling.
For notification in Databricks Serverless, prefer webhook-based notification methods like Slack or Microsoft Teams, which are generally more reliable in such environments.
When dealing with evolving schemas in source data, plan for how the `_error` table schema will be managed to accommodate changes and prevent write failures. This may involve custom schema handling or specific table format configurations.