WeData DQ Expectations Demo 4 — wedata.dq 包
概述
本 Demo 演示如何通过 pip install wedata-dq 安装包后,使用 @expect 装饰器语法进行数据质量验证。
对标 Databricks DLT 的 @dlt.expect() 装饰器,WeData 提供自己的 wedata.dq 包,用户只需:
from wedata.dq import table, expect, expect_or_drop, expect_or_fail
@table
@expect("valid_age", "age BETWEEN 18 AND 100", severity="warn")
@expect("positive_amount", "amount > 0", severity="error")
def transform_orders():
return spark.sql("SELECT * FROM raw_orders")
目录结构
wedata_demo_4/
├── wedata/ # ⭐ wedata.dq Python 包源码
│ ├── __init__.py # 包入口,版本信息
│ └── dq/
│ ├── __init__.py # 导出 table / expect / expect_or_drop / expect_or_fail
│ ├── decorators.py # 装饰器定义(规则注册)
│ └── runtime.py # 运行时引擎(规则执行 + 指标写入)
├── pyproject.toml # 构建配置
├── setup.cfg # 包元数据(name=wedata-dq, version=1.0.0)
├── demo_pipeline.py # 演示脚本(使用 wedata.dq 包)
├── requirements.txt # 依赖: pyspark
├── run_demo.sh # 一键运行脚本
└── README.md # 本文件
快速开始
方式 1: 一键运行
cd wedata_demo_4/
bash run_demo.sh
方式 2: 手动运行
cd wedata_demo_4/
pip install pyspark
python demo_pipeline.py
生产环境安装方式(DLC 引擎)
构建 wheel 包
cd wedata_demo_4/
pip install build
python -m build
# 输出: dist/wedata_dq-1.0.0-py3-none-any.whl
上传到外网
方式A: COS 直接上传(最快)
将 dist/wedata_dq-1.0.0-py3-none-any.whl 上传到 COS 桶(如 wedata-dq-1258344699.cos.ap-guangzhou.myqcloud.com/release/),用户脚本中 pip install 即可。
方式B: 腾讯内网 PyPI 源
上传到 https://mirrors.tencent.com/repository/pypi/tencent_pypi/(需联系基础架构同学开通权限)。
用户 DLC 脚本中使用
# DLC 用户脚本开头,pip install 安装 wedata-dq 包
import subprocess
# 方式A: 从 COS 直接安装
subprocess.check_call([
"pip", "install",
"https://wedata-dq-1258344699.cos.ap-guangzhou.myqcloud.com/release/wedata_dq-1.0.0-py3-none-any.whl"
])
# 方式B: 从腾讯内网 PyPI 源安装
subprocess.check_call([
"pip", "install", "-i", "https://mirrors.tencent.com/repository/pypi/tencent_pypi/simple/",
"--trusted-host", "mirrors.tencent.com",
"wedata-dq"
])
# 安装后直接使用
from wedata.dq import table, expect, expect_or_drop, expect_or_fail, init_runtime
@table
@expect("valid_age", "age BETWEEN 18 AND 100", severity="warn")
def transform_customers():
return spark.sql("SELECT * FROM raw_customers")
核心设计
装饰器对标 Databricks DLT
| Databricks DLT | WeData wedata.dq |
|---|---|
@dlt.table |
@table |
@dlt.expect("rule", "expr") |
@expect("rule", "expr") |
@dlt.expect_or_drop("rule", "expr") |
@expect_or_drop("rule", "expr") |
@dlt.expect_or_fail("rule", "expr") |
@expect_or_fail("rule", "expr") |
三种违规策略
| 策略 | 装饰器 | 行为 |
|---|---|---|
| warn | @expect |
记录违规行,不阻断数据流 |
| drop | @expect_or_drop |
自动丢弃违规行 |
| fail | @expect_or_fail |
发现违规则抛异常,终止作业 |
执行流程
用户脚本 import wedata.dq
→ @expect 装饰器在编译期注册规则
→ @table 装饰器在运行期拦截函数调用
→ apply_expectations() 应用规则
→ F.expr() 编译 SQL 约束
→ 统计通过/失败行数
→ 按策略处理 (warn/drop/fail)
→ 写入 wedata_dq_expectations 监控表
与 Demo 2/3 的区别
| 对比维度 | Demo 2 (方案2) | Demo 3 (方案3) | Demo 4 (新方案) |
|---|---|---|---|
| 包形式 | 独立脚本内嵌类 | Java 注入源码 | 标准 Python 包 |
| 安装方式 | 无(脚本自带) | Java 拼接注入 | pip install wedata-dq |
| 分发渠道 | ❌ 复制代码 | ❌ 依赖 Java 注入器 | ✅ COS / PyPI / --py-files |
| 用户导入 | wedata_dlt = WedataExpectations() |
不需要 import | from wedata.dq import expect |
| 对标 DLT | 需手动创建实例 | 源码内联 | import wedata.dq 即可 |
| 版本管理 | ❌ 无 | ❌ 改模板文件 | ✅ pip 版本号管理 |
测试数据说明
| 表 | 行数 | 质量问题 |
|---|---|---|
| raw_customers | 5 | Charlie 邮箱为空(drop), David 年龄/国家码不合法(warn) |
| raw_orders | 6 | #103 customer_id 为空(fail), #104 金额为负(error), #105 状态不合法(warn) |
预期结果:
- customers 表: 4 行(Charlie 被 drop)
- orders 表: 不存在(#103 触发 fail,整表失败)
- 监控表: 包含所有规则的通过/失败统计
Metadata
Release files for wedata-dq 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| wedata_dq-1.0.0.tar.gz | 9.8 kB | Without provenance | Details for wedata_dq-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| wedata_dq-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for wedata_dq-1.0.0-py3-none-any.whl |
Total release size: 19.3 kB
Release files / wedata_dq-1.0.0.tar.gz
| Download URL | wedata_dq-1.0.0.tar.gz |
|---|---|
| Size | 9.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4a5185c31606194231462794c17d9370f246e20ac9df68658901f84e2e1b8dc1
|
|
BLAKE2b-256 checksum How to use checksums |
0a8a68d4669a0383a03e0b641e291b3759e41af906c97c2f155379d9a18b37dd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.7
|
Release files / wedata_dq-1.0.0-py3-none-any.whl
| Download URL | wedata_dq-1.0.0-py3-none-any.whl |
|---|---|
| Size | 9.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
50cfc95c983c05791553c8f3d3b761082b888e311923f22dfd8d9d0ce3eb24c7
|
|
BLAKE2b-256 checksum How to use checksums |
12278ba363639eb933cbbf7114816403dd353b601f15d7fae14a7eb755643e2c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.7
|
Security for wedata-dq 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.