Skip to main content

A PySpark-based Data Quality Framework using YAML-configurable checks.

Project description

๐Ÿ” PySpark Data Quality Framework

A flexible, extensible PySpark-based Data Quality (DQ) framework designed to apply configurable data quality checks on Spark DataFrames using a YAML-based rule system.


๐Ÿ“ฆ Installation

  • Install the package from PyPI:
pip install pyspark-dq-framework

Ensure pyspark is installed:

pip install pyspark


๐Ÿ“ Project Structure

pyspark-dq-framework/
โ”œโ”€โ”€ pyspark_dq/                   # your Python package
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ dq_functions.py           # Core data quality functions
โ”‚   โ”œโ”€โ”€ dq_config.py              # YAML parser to config DataFrame
โ”‚   โ””โ”€โ”€ run_dq_check.py           # Engine to run checks with logging
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ LICENSE
โ”œโ”€โ”€ MANIFEST.in
โ”œโ”€โ”€ setup.py

- Import packages

from pyspark_dq.dq_functions import * from pyspark_dq.dq_config import * from pyspark_dq.run_dq_check import *


๐Ÿ“˜ Module Descriptions
1. config.yml โ€“ Define DQ Rules
Stores DQ rules for tables and columns

YAML format, human-readable and configurable

Sample:
model:
  - name: df
    columns:
      - name: ID
        checks:
          - null_check
          - unique_check
      - name: Age
        checks:
          - null_check

2. dq_functions.py โ€“ Built-in DQ Checks
Contains all reusable data quality functions

Supported Checks:

null_check
unique_check
set_membership_check
range_check
non_negative_check
regex_check
not_empty_check
data_type_check

Each check returns:

valid_df: rows that passed
invalid_df: rows that failed
log_df: log entry of the check

from pyspark_dq.dq_functions import null_check
valid_df, invalid_df, log_df = null_check(df, {"table": "df", "column": "ID"})

3. dq_config.py โ€“ Parse YAML Config
Reads config.yml and flattens it into a Spark DataFrame

Function:
from pyspark_dq.dq_config import dq_config
dq_config_df = dq_config(spark)

Returns a DataFrame like:
+-------+--------+-------------+
| table | column | check       |
+-------+--------+-------------+
| df    | ID     | null_check  |
| df    | ID     | unique_check|
| df    | Age    | null_check  |
+-------+--------+-------------+

4. run_dq_check.py โ€“ Run and Log DQ Checks
Executes each check defined in config

Logs check result and error records

Function:
from pyspark_dq.run_dq_check import run_dq_checks_with_error_handling

clean_df, all_errors_df, enriched_logs_df = run_dq_checks_with_error_handling(
    df, dq_config_df, check_function_map
)

Returns:
clean_df: all valid rows
all_errors_df: union of invalid rows from all checks
enriched_logs_df: metadata logs + JSON error list

๐Ÿš€ How to Use (main.py)
Steps:
Start Spark Session

from pyspark.sql import SparkSession
spark = SparkSession.builder.appName("DQExample").getOrCreate()

Create Sample DataFrame
data = [(1, "Alice", 34), (None, "Bob", 45)]
df = spark.createDataFrame(data, ["ID", "Name", "Age"])

Create YAML Config (config.yml)
model:
  - name: df
    columns:
      - name: ID
        checks:
          - null_check
      - name: Age
        checks:
          - null_check

Load Config
from dq_config import dq_config
dq_config_df = dq_config(spark)

Map Check Functions
from pyspark_dq.dq_functions import *

check_function_map = {
    "null_check": null_check,
    "unique_check": unique_check,
    "set_membership_check": lambda df, row: set_membership_check(df, row, allowed_values=["Alice", "Bob"]),
    "range_check": lambda df, row: range_check(df, row, min_value=0, max_value=100),
    "non_negative_check": non_negative_check,
    "regex_check": lambda df, row: regex_check(df, row, pattern=r"^[a-zA-Z]+$"),
    "not_empty_check": not_empty_check,
    "data_type_check": data_type_check
}

Run DQ Checks
from pyspark_dq.run_dq_check import run_dq_checks_with_error_handling

clean_df, error_df, logs_df = run_dq_checks_with_error_handling(
    df, dq_config_df, check_function_map
)

Inspect Results
clean_df.show()
error_df.show()
logs_df.show(truncate=False)


๐Ÿงช Supported Environments
Python 3.7+
Apache Spark 3.x+
Local or Cluster Spark Deployments

๐Ÿ“– Example Output
Logs Output:

+-----+--------+-------------+-------+--------------+-------------------------+
|table|column  |check_type   |passed|invalid_count |error_data_json          |
+-----+--------+-------------+------+--------------+-------------------------+
|df   |ID      |null_check   |false |1             |["{\"Name\":\"Bob\",..."]
+-----+--------+-------------+------+--------------+-------------------------+


๐Ÿ”ง Extending the Framework
To add a custom check:

Define a new function in dq_functions.py
Make sure it returns: valid_df, invalid_df, log_df
Add it to your check_function_map in main.py

๐Ÿง‘โ€๐Ÿ’ป Contributing
Fork the repository
Add your improvements or new checks

Submit a pull request ๐Ÿš€

๐Ÿ“„ License
This project is licensed under the MIT License.

๐Ÿ’ฌ Questions or Feedback?
Open an issue

Start a discussion in the GitHub repository

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyspark_dq_framework-0.1.2.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyspark_dq_framework-0.1.2-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file pyspark_dq_framework-0.1.2.tar.gz.

File metadata

  • Download URL: pyspark_dq_framework-0.1.2.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for pyspark_dq_framework-0.1.2.tar.gz
Algorithm Hash digest
SHA256 700955dd66dc68bbd67930daee421f8f315e8e021c4638edd350b2cf21a58913
MD5 cfb1766fb6962e0009c7d48376201334
BLAKE2b-256 8d2a788ac7a93f1f087198caa2e3a9e1a85192804c1407842219ba1905e19760

See more details on using hashes here.

File details

Details for the file pyspark_dq_framework-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for pyspark_dq_framework-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 18999851cafb6daedbb611f493ca2b162ece613e6c4b4979d52fb6e13966f49b
MD5 c184239bfe0510c2400af1cf11c7ead5
BLAKE2b-256 713f7493bcb4861dcca948a28531e1590d7f538c89d8e6d5156a2e1b5e5506f3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page