-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdqx_dlt_demo.py
135 lines (105 loc) · 2.78 KB
/
dqx_dlt_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# Databricks notebook source
%pip install databricks-labs-dqx
# COMMAND ----------
dbutils.library.restartPython()
# COMMAND ----------
# MAGIC %md
# MAGIC ## Create DLT Pipeline
# MAGIC
# MAGIC Create new DLT Pipeline to execute this notebook (see [here](https://www.databricks.com/discover/pages/getting-started-with-delta-live-tables#define)).
# MAGIC
# MAGIC Go to `Workflows` tab > `Pipelines` > `Create pipeline`
# COMMAND ----------
# MAGIC %md
# MAGIC ## Define DLT Pipeline
# COMMAND ----------
import dlt
from databricks.labs.dqx.engine import DQEngine
from databricks.sdk import WorkspaceClient
# COMMAND ----------
@dlt.view
def bronze():
df = spark.readStream.format("delta") \
.load("/databricks-datasets/delta-sharing/samples/nyctaxi_2019")
return df
# COMMAND ----------
# Define Data Quality checks
import yaml
checks = yaml.safe_load("""
- check:
function: "is_not_null"
arguments:
col_name: "vendor_id"
name: "vendor_id_is_null"
criticality: "error"
- check:
function: "is_not_null_and_not_empty"
arguments:
col_name: "vendor_id"
trim_strings: true
name: "vendor_id_is_null_or_empty"
criticality: "error"
- check:
function: "is_not_null"
arguments:
col_name: "pickup_datetime"
name: "pickup_datetime_is_null"
criticality: "error"
- check:
function: "not_in_future"
arguments:
col_name: "pickup_datetime"
name: "pickup_datetime_isnt_in_range"
criticality: "warn"
- check:
function: "not_in_future"
arguments:
col_name: "pickup_datetime"
name: "pickup_datetime_not_in_future"
criticality: "warn"
- check:
function: "not_in_future"
arguments:
col_name: "dropoff_datetime"
name: "dropoff_datetime_not_in_future"
criticality: "warn"
- check:
function: "is_not_null"
arguments:
col_name: "passenger_count"
name: "passenger_count_is_null"
criticality: "error"
- check:
function: "is_in_range"
arguments:
col_name: "passenger_count"
min_limit: 0
max_limit: 6
name: "passenger_incorrect_count"
criticality: "warn"
- check:
function: "is_not_null"
arguments:
col_name: "trip_distance"
name: "trip_distance_is_null"
criticality: "error"
""")
# COMMAND ----------
dq_engine = DQEngine(WorkspaceClient())
# Read data from Bronze and apply checks
@dlt.view
def bronze_dq_check():
df = dlt.read_stream("bronze")
return dq_engine.apply_checks_by_metadata(df, checks)
# COMMAND ----------
# # get rows without errors or warnings, and drop auxiliary columns
@dlt.table
def silver():
df = dlt.read_stream("bronze_dq_check")
return dq_engine.get_valid(df)
# COMMAND ----------
# get only rows with errors or warnings
@dlt.table
def quarantine():
df = dlt.read_stream("bronze_dq_check")
return dq_engine.get_invalid(df)