Rule templates
Data Quality rule templates reduce the effort of writing Specs. A set of commonly used templates is built in, and you can also create custom templates. Templates predefine most of the settings that a Spec requires, so when you create a rule from a template, you only need to configure the additional settings.
Use system rule templates
The following example creates a rule from the Average value, 1-, 7-, and 30-day volatility rate system rule template:
datasets:
- type: Table
tables:
- tb_d_spec_demo
filter: "dt='$[yyyymmdd]' AND hh='$[hh24-1/24]'"
dataSource:
name: odps_first
envType: Dev
rules:
- templateId: "SYSTEM:field:avg:flux:1_7_1m_bizdate"
fields:
- col_income
warn: "when > 1%"
fail: "when > 10%"The preceding code snippet creates a Data Quality monitoring rule that references the Average value, 1-, 7-, and 30-day volatility rate system rule template. The settings are as follows:
templateId: TheIdof the rule template that you want to use. For more rule template IDs, see System rule template list. You can also create a custom rule template and reference Use custom rule templates to create a rule.templateParameters: The parameters that the rule template requires. Different rule templates require different parameters. A common parameter isfields, which specifies the list of fields that the rule monitors.For the parameters that each rule template requires, see System rule template list
warn/fail: The thresholds.
Use custom rule templates
If system rule templates cannot meet your requirements, or if many of your custom SQL rules contain the same SQL statement, you can create custom rule templates.
Define a custom template
The following example shows the Spec configuration of a custom rule template:
name: "id non-null row count check"
id: "1760d075-00bd-43c9-be8f-3c9a4bac35db"
assertion: "id_not_null_row_count = 0"
id_not_null_row_count:
query: "SELECT COUNT(*) FROM ${table} WHERE dt = '$[yyyymmdd-1]';"
catalog: "ods_layer/timeliness_check"The preceding code snippet defines a simple custom rule template that contains the following fields:
name: The name of the template.id: The unique identifier of the template. Make sure that the identifier is globally unique. To reference the template, use this identifier.assertion: The description of the rule. The syntax follows the Data Quality Spec configuration topic'sassertionfield.id_not_null_row_count: In this example, theassertionreferences themetricdefinition. In thequery, you can use${tableName}in place of the table name. When you create a rule from this rule template, the placeholder is replaced with the name of the table that the rule monitors. You can also reference system time parameters directly (for details about how to reference time parameters, see Configure data filtering).catalog: The category of the rule template. You can manage rule templates by category. Separate multiple levels of categories with/.
Threshold settings in custom templates
Fixed and non-fixed thresholds
The following example shows the configuration of a non-fixed threshold:
name: "id non-null row count check"
id: "1760d075-00bd-43c9-be8f-3c9a4bac35db"
assertion: "id_not_null_row_count"
id_not_null_row_count:
query: "SELECT COUNT(*) FROM ${table} WHERE dt = '$[yyyymmdd-1]';"Volatility thresholds
name: "id non-null row count check"
id: "1760d075-00bd-43c9-be8f-3c9a4bac35db"
assertion: "change avg last 7 days percent for id_not_null_row_count"
id_not_null_row_count:
query: "SELECT COUNT(*) FROM ${table} WHERE dt = '$[yyyymmdd-1]';"Create a rule from a custom template
Example 1
datasets: - type: Table tables: - tb_d_spec_demo filter: "dt='$[yyyymmdd]' AND hh='$[hh24-1/24]'" dataSource: name: odps_first envType: Dev rules: - templateId: "1760d075-00bd-43c9-be8f-3c9a4bac35db" # Use fail to define the failure threshold fail: "when != 0"Example 2
datasets: - type: Table tables: - tb_d_spec_demo filter: "dt='$[yyyymmdd]' AND hh='$[hh24-1/24]'" dataSource: name: odps_first envType: Dev rules: - templateId: "1760d075-00bd-43c9-be8f-3c9a4bac35db" warn: "when between 1 and 10)" fail: "when > 10"Example 2
datasets: - type: Table tables: - tb_d_spec_demo filter: "dt='$[yyyymmdd]' AND hh='$[hh24-1/24]'" dataSource: name: odps_first envType: Dev rules: - templateId: "1760d075-00bd-43c9-be8f-3c9a4bac35db" warn: "when not between -1% and 1%" fail: "when not between -10% and 10%"
System rule template list
Id | Template | Required fields | Example |
| Table row count, fixed value |
| |
| Table row count greater than 0 | - | |
| Table row count, 1-day difference |
| |
| Table row count, difference from the previous cycle |
| |
| Table row count, 1-, 7-, and 30-day volatility rate |
| |
| Table row count, 1-, 7-, and 30-day volatility rate and volatility rate from the first day of the current month |
| |
| Table row count, 1-day volatility rate |
| |
| Table row count, 30-day volatility rate |
| |
| Table row count, 7-day volatility rate |
| |
| Table row count, dynamic threshold | - | |
| Table row count, 7-day average volatility rate |
| |
| Table row count, 30-day average volatility rate |
| |
| Table row count, volatility rate from the previous cycle |
| |
| Table size, fixed value |
| |
| Table size, difference from 1 day ago (bytes) |
| |
| Table size, difference from the previous cycle |
| |
| Table size, 1-day volatility rate |
| |
| Table size, 30-day volatility rate |
| |
| Table size, 7-day volatility rate |
| |
| Table size, dynamic threshold | - | |
| Table schema evolution - any change | - | |
| Table schema evolution - column deleted | - | |
| Table schema evolution - column added | - | |
| Table schema evolution - column order changed | - | |
| Table schema evolution - column data type changed | - | |
| Null value count, fixed value |
| |
| Null value record count is 0 |
| |
| Null value count/total row count, fixed value |
| |
| Regular expression check |
| |
| Date format check |
| |
| Email address format check |
| |
| ID card number format check |
| |
| Mobile phone number format check |
| |
| Currency format check |
| |
| Numeric format check |
| |
| Phone number format check |
| |
| Duplicate value count, fixed value |
| |
| Duplicate value count for the field is 0 |
| |
| Duplicate value count for multiple fields is 0 |
| |
| Duplicate value count/total row count, fixed value |
| |
| Unique value count, fixed value |
| |
| Unique value count, 1-, 7-, and 30-day volatility rate |
| |
| Unique value count, dynamic threshold |
| |
| Unique value count/total row count, fixed value |
| |
| Minimum value, 1-, 7-, and 30-day volatility rate |
| |
| Minimum value, dynamic threshold |
| |
| Minimum value, 1-day volatility rate |
| |
| Minimum value, volatility rate from the previous cycle |
| |
| Maximum value, 1-, 7-, and 30-day volatility rate |
| |
| Maximum value, dynamic threshold |
| |
| Maximum value, 1-day volatility rate |
| |
| Maximum value, volatility rate from the previous cycle |
| |
| Average value, 1-, 7-, and 30-day volatility rate |
| |
| Average value, dynamic threshold |
| |
| Average value, 1-day volatility rate |
| |
| Sum, 1-, 7-, and 30-day volatility rate |
| |
| Sum, dynamic threshold |
| |
| Sum, 1-day volatility rate |
| |
| Sum, volatility rate from the previous cycle |
| |
| Count of unique values that do not match the enumeration, fixed value |
| |
| Count of rows that do not match the enumeration, fixed value |
| |
| Count of rows that do not match the enumeration is 0 |
| |
| Enumeration values, custom, fixed value |
| |
| Discrete values (status values), fixed value |
| |
| Discrete values (group count), fixed value |
| |
| Discrete values (group count), dynamic threshold |
| |
| Discrete values (status values), dynamic threshold |
| |
| Discrete values (group count), 1-day volatility rate |
| |
| Discrete values (group count and status values), 1-, 7-, and 30-day volatility rate |
| |
SYSTEM:table:table_count:flux | Table row count, custom-period volatility |
timeOffset is a historical sample offset string, such as 7 days ago. Configure the target datasets and provide comparable historical run records. | |
SYSTEM:table:table_count_delta:fixed | Table row count, custom-period difference |
timeOffset is a historical sample offset string, such as 7 days ago. Configure the target datasets and provide comparable historical run records. | |
SYSTEM:fields:mapping_consistency_count:fixed | Field mapping consistency |
| |
SYSTEM:field:json_key_missing_count:fixed | JSON key presence check |
| |
SYSTEM:field:json_value_invalid_count:fixed | JSON value format check |
| |
SYSTEM:field:delay_count:fixed | Data delay, time difference check between adjacent rows |
| |
SYSTEM:field:group_by_metric_check:fixed | Grouped metric check, fixed value |
| |
SYSTEM:field:group_by_metric_check:flux | Grouped metric check, historical volatility |
| |
Appendix: Configure custom-range system templates
The following custom-range system templates are configured differently from standard system templates and must be configured separately.
Conditional match percentage, custom range
rules: - assertion: "matched_row_percent = 0" filter: "id IS NULL"Field minimum value, custom range
rules: - assertion: "anomal detection for min(income)"Field maximum value, custom range
rules: - assertion: "change avg last 7 days percent for max(income)" warn: "when > 0.1%" fail: "when > 0.5%"Field average value, custom range
rules: - assertion: "change var last 30 days percent for avg(income)" warn: "when < -0.1%" fail: "when < -0.5%"Field sum, custom range
rules: - assertion: "change 1 days ago percent for sum(income)" warn: "when not between -0.1% and 0.1%" fail: "when not between -0.5% and 0.5%"Custom SQL
assertionis configured in the same way as described in Define a custom metric rule.rules: - assertion: "change percent for id_not_null_count" id_not_null_count: query: "SELECT COUNT(*) AS cnt FROM tb_spec_demo WHERE dt = '$[yyyymmdd]'" warn: "when not between -0.1% and 0.1%" fail: "when not between -0.5% and 0.5%"