This repository was archived by the owner on Sep 30, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathexample_snowflake_partial_table_with_append.py
More file actions
140 lines (114 loc) · 4.03 KB
/
Copy pathexample_snowflake_partial_table_with_append.py
File metadata and controls
140 lines (114 loc) · 4.03 KB
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
"""
Example ETL DAG highlighting Astro functionality
DAG requires 2 "Homes" csv's (found in this repo), and a supported database
General flow of the DAG is to extract the data from csv's and combine using SQL,
then switch to Python for a melt transformation, then back to SQL for final
filtering. The data is then loaded by appending to an existing reporting table.
This example DAG creates the reporting table & truncates it by the end of the execution.
"""
import os
from datetime import datetime, timedelta
import pandas as pd
from airflow.decorators import dag
from astro.files import File
from astro.sql import append, cleanup, dataframe, load_file, run_raw_sql, transform
from astro.table import Metadata, Table
SNOWFLAKE_CONN_ID = "snowflake_conn"
dir_path = os.path.dirname(os.path.realpath(__file__))
FILE_PATH = dir_path + "/data/"
# The first transformation combines data from the two source csv's
@transform
def extract_data(homes1: Table, homes2: Table):
return """
SELECT *
FROM {{homes1}}
UNION
SELECT *
FROM {{homes2}}
"""
# Switch to Python (Pandas) for melting transformation to get data into long format
@dataframe
def transform_data(df: pd.DataFrame):
df.columns = df.columns.str.lower()
melted_df = df.melt(id_vars=["sell", "list"], value_vars=["living", "rooms", "beds", "baths", "age"])
return melted_df
# Back to SQL to filter data
@transform
def filter_data(homes_long: Table):
return """
SELECT *
FROM {{homes_long}}
WHERE SELL > 200
"""
# [START howto_run_raw_sql_snowflake_1]
@run_raw_sql
def create_table(table: Table):
"""Create the reporting data which will be the target of the append method"""
return """
CREATE OR REPLACE TABLE {{table}} (
sell number,
list number,
variable varchar,
value number
);
"""
@dag(
start_date=datetime(2021, 12, 1),
schedule_interval=None,
catchup=False,
default_args={
"email_on_failure": False,
"retries": 1,
"retry_delay": timedelta(seconds=5),
},
)
def example_snowflake_partial_table_with_append():
homes_reporting = Table(name="homes_reporting", temp=True, conn_id=SNOWFLAKE_CONN_ID)
create_results_table = create_table(table=homes_reporting, conn_id=SNOWFLAKE_CONN_ID)
# [END howto_run_raw_sql_snowflake_1]
# Initial load of homes data csv's into Snowflake
homes_data1 = load_file(
input_file=File(path=FILE_PATH + "homes.csv"),
output_table=Table(
conn_id=SNOWFLAKE_CONN_ID,
metadata=Metadata(
database=os.getenv("SNOWFLAKE_DATABASE"),
schema=os.getenv("SNOWFLAKE_SCHEMA"),
),
),
assume_schema_exists=True, # Skip queries that check if the table schema exist
)
homes_data2 = load_file(
input_file=File(path=FILE_PATH + "homes2.csv"),
output_table=Table(
conn_id=SNOWFLAKE_CONN_ID,
metadata=Metadata(
database=os.getenv("SNOWFLAKE_DATABASE"),
schema=os.getenv("SNOWFLAKE_SCHEMA"),
),
),
assume_schema_exists=True,
)
# Define task dependencies
extracted_data_table = extract_data(
homes1=homes_data1,
homes2=homes_data2,
output_table=Table(),
)
transformed_data_table = transform_data(df=extracted_data_table, output_table=Table())
filtered_data = filter_data(
homes_long=transformed_data_table,
output_table=Table(),
)
# Append transformed & filtered data to reporting table
# Dependency is inferred by passing the previous `filtered_data` task to `append_table` param
# [START append_example_with_columns_list]
record_results = append(
source_table=filtered_data,
target_table=homes_reporting,
columns=["sell", "list", "variable", "value"],
)
# [END append_example_with_columns_list]
record_results.set_upstream(create_results_table)
cleanup()
example_snowflake_partial_table_dag = example_snowflake_partial_table_with_append()