-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgres_to_snowflake_demo.py
169 lines (141 loc) · 5.1 KB
/
postgres_to_snowflake_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import pandas as pd
import psycopg2
from sqlalchemy import create_engine
from sqlalchemy.dialects import registry
from dotenv import load_dotenv
import os
from dagster import (
asset,
asset_check,
Definitions,
AssetCheckResult,
MetadataValue,
AssetCheckSeverity,
)
from data_diff import connect_to_table, diff_tables
from random import randint
load_dotenv()
SOURCE_DATABASE_HOST = os.getenv("SOURCE_DATABASE_HOST")
SOURCE_DATABASE_PORT = os.getenv("SOURCE_DATABASE_PORT")
SOURCE_DATABASE_NAME = os.getenv("SOURCE_DATABASE_NAME")
SOURCE_DATABASE_USER = os.getenv("SOURCE_DATABASE_USER")
SOURCE_DATABASE_PASSWORD = os.getenv("SOURCE_DATABASE_PASSWORD")
DESTINATION_SNOWFLAKE_ACCOUNT = os.getenv("DESTINATION_SNOWFLAKE_ACCOUNT")
DESTINATION_SNOWFLAKE_USER = os.getenv("DESTINATION_SNOWFLAKE_USER")
DESTINATION_SNOWFLAKE_PASSWORD = os.getenv("DESTINATION_SNOWFLAKE_PASSWORD")
DESTINATION_SNOWFLAKE_WAREHOUSE = os.getenv("DESTINATION_SNOWFLAKE_WAREHOUSE")
DESTINATION_SNOWFLAKE_DATABASE = os.getenv("DESTINATION_SNOWFLAKE_DATABASE")
DESTINATION_SNOWFLAKE_SCHEMA = os.getenv("DESTINATION_SNOWFLAKE_SCHEMA")
DESTINATION_SNOWFLAKE_ROLE = os.getenv("DESTINATION_SNOWFLAKE_ROLE")
EVENT_COUNT = 100
@asset
def source_postgres_events():
conn = psycopg2.connect(
host=SOURCE_DATABASE_HOST,
port=SOURCE_DATABASE_PORT,
dbname=SOURCE_DATABASE_NAME,
user=SOURCE_DATABASE_USER,
password=SOURCE_DATABASE_PASSWORD,
)
cursor = conn.cursor()
cursor.execute("DROP TABLE IF EXISTS events;")
create_table_query = """
CREATE TABLE events (
"ID" INTEGER,
"DATE" DATE
);
"""
cursor.execute(create_table_query)
for i in range(EVENT_COUNT):
cursor.execute(f"INSERT INTO events VALUES ({i}, current_date + {i});")
noise_size = randint(1, 5)
for i in range(noise_size):
cursor.execute(f"INSERT INTO events VALUES (-{i}, current_date - {i});")
conn.commit()
cursor.close()
conn.close()
@asset(deps=[source_postgres_events])
def replicated_snowflake_events():
src_conn = psycopg2.connect(
host=SOURCE_DATABASE_HOST,
port=SOURCE_DATABASE_PORT,
dbname=SOURCE_DATABASE_NAME,
user=SOURCE_DATABASE_USER,
password=SOURCE_DATABASE_PASSWORD,
)
src_df = pd.read_sql('SELECT * FROM events WHERE "ID" > 0', src_conn)
src_conn.close()
registry.register("snowflake", "snowflake.sqlalchemy", "dialect")
sf_engine = create_engine(
f"snowflake://{DESTINATION_SNOWFLAKE_USER}:{DESTINATION_SNOWFLAKE_PASSWORD}@{DESTINATION_SNOWFLAKE_ACCOUNT}/{DESTINATION_SNOWFLAKE_DATABASE}/{DESTINATION_SNOWFLAKE_SCHEMA}?warehouse={DESTINATION_SNOWFLAKE_WAREHOUSE}&role={DESTINATION_SNOWFLAKE_ROLE}"
)
src_df.to_sql("events", sf_engine, if_exists="replace", index=False)
with sf_engine.connect() as sf_conn:
noise_size = randint(5, 15)
for i in range(noise_size):
sf_conn.execute(
f"UPDATE events SET DATE = CURRENT_DATE() - 2 WHERE ID = {i};"
)
@asset_check(
name="postgres_to_snowflake_data_diff_check",
asset=replicated_snowflake_events,
)
def postgres_to_snowflake_data_diff_check() -> AssetCheckResult:
template_postgres = {
"driver": "postgresql",
}
template_snowflake = {
"driver": "snowflake",
}
source_events_table = connect_to_table(
{
**template_postgres,
"host": SOURCE_DATABASE_HOST,
"port": SOURCE_DATABASE_PORT,
"dbname": SOURCE_DATABASE_NAME,
"user": SOURCE_DATABASE_USER,
"password": SOURCE_DATABASE_PASSWORD,
},
"events",
)
replicated_events_table = connect_to_table(
{
**template_snowflake,
"account": DESTINATION_SNOWFLAKE_ACCOUNT,
"user": DESTINATION_SNOWFLAKE_USER,
"password": DESTINATION_SNOWFLAKE_PASSWORD,
"warehouse": DESTINATION_SNOWFLAKE_WAREHOUSE,
"database": DESTINATION_SNOWFLAKE_DATABASE,
"schema": DESTINATION_SNOWFLAKE_SCHEMA,
"role": DESTINATION_SNOWFLAKE_ROLE,
},
"EVENTS",
)
results = pd.DataFrame(
diff_tables(
source_events_table,
replicated_events_table,
key_columns=["ID"],
extra_columns=("DATE",),
),
columns=["diff_type", "row_diffs"],
)
total_diffs_count = len(results)
yield AssetCheckResult(
passed=total_diffs_count == 0,
severity=AssetCheckSeverity.ERROR,
metadata={
"total_diffs": MetadataValue.int(total_diffs_count),
"source_row_diffs": MetadataValue.int(
len(results[results["diff_type"] == "-"])
),
"target_row_diffs": MetadataValue.int(
len(results[results["diff_type"] == "+"])
),
"preview": MetadataValue.md(results.head(100).to_markdown()),
},
)
defs = Definitions(
assets=[source_postgres_events, replicated_snowflake_events],
asset_checks=[postgres_to_snowflake_data_diff_check],
)