dbt-expectations包通过提供一组全面的数据质量测试功能,扩展了dbt-core内置测试能力。受Great Expectations框架的启发,这些测试可以执行数据完整性测试、并验证有关数据的假设。
dbt Expectations介绍
dbt-expectations是dbt测试工具包,旨在为dbt模型提供类似于Great expectations的数据质量测试功能。它包括各种测试,支持验证数据的一致性、准确性和有效性。
- 安装dbt Expectations
要在dbt项目中使用dbt-expectations,需要将其作为依赖项添加到包中。Yml文件,然后使用DBT deps命令安装它。
- packages.yml中增加依赖dbt Expectations
packages:
- package: calogica/dbt_expectations
version: 0.10.3
- 安装包
dbt deps
典型测试及示例
下面是dbt-expectations包中的一些常用测试,以及如何在dbt模型中使用它们的示例。
1. expect_table_row_count_to_be_between
下面示例检查表中行数是否在指定范围内。
models: # or seeds:
- name: orders
tests:
- dbt_expectations.expect_table_row_count_to_be_between:
min_value: 100 # (Optional)
max_value: 1000 # (Optional)
# group_by: [group_id, other_group_id, ...] # (Optional)
# row_condition: "id is not null" # (Optional)
# strictly: false # (Optional. Adds an 'or equal to' to the comparison operator for min/max)
何时使用:非常适合确保表大小保持在预期范围内,有助于检测丢失数据或意外数据增长等问题。
2. expect_table_row_count_to_equal_other_table
下面示例验证表中的行数是否与另一个表的行数相匹配。
models: # or seeds:
- name: orders
tests:
- dbt_expectations.expect_table_row_count_to_equal_other_table:
compare_model: ref('expected_orders')
# group_by: [col1, col2] # (Optional)
# compare_group_by: [col1, col2] # (Optional)
# factor: 1 # (Optional) 乘数因子,用于乘以因子后结果比较
# row_condition: "id is not null" # (Optional)
# compare_row_condition: "id is not null" # (Optional)
何时使用:用于确保相关表之间的一致