This repository has been archived by the owner on Jun 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
example_fivetran_bqml.py
130 lines (108 loc) · 3.78 KB
/
example_fivetran_bqml.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
from airflow import DAG
from airflow.providers.ssh.operators.ssh import SSHOperator
from airflow.operators.python_operator import BranchPythonOperator
from fivetran_provider.operators.fivetran import FivetranOperator
from fivetran_provider.sensors.fivetran import FivetranSensor
from airflow.providers.google.cloud.hooks.compute_ssh import ComputeEngineSSHHook
from airflow.providers.google.cloud.operators.bigquery import (
BigQueryExecuteQueryOperator,
BigQueryGetDataOperator,
)
from datetime import datetime, timedelta
# EDIT WITH YOUR PROJECT ID & DATASET NAME
PROJECT_ID = "YOUR PROJECT ID"
DATASET_NAME = "bqml"
DESTINATION_TABLE = "dbt_ads_bqml_preds"
TRAINING_QUERY = (
"CREATE OR REPLACE MODEL bqml.dbt_ads_airflow_model "
"OPTIONS "
"(model_type = 'ARIMA_PLUS', "
"time_series_timestamp_col = 'parsed_date', "
"time_series_data_col = 'daily_impressions', "
"auto_arima = TRUE, "
"data_frequency = 'AUTO_FREQUENCY', "
"decompose_time_series = TRUE "
") AS "
"SELECT "
"timestamp(date_day) as parsed_date, "
"SUM(impressions) as daily_impressions "
"FROM `" + PROJECT_ID + ".bqml.ad_reporting` "
"GROUP BY date_day;"
)
SERVING_QUERY = (
"SELECT string(forecast_timestamp) as forecast_timestamp, "
"forecast_value, "
"standard_error, "
"confidence_level, "
"prediction_interval_lower_bound, "
"prediction_interval_upper_bound, "
"confidence_interval_lower_bound, "
"confidence_interval_upper_bound "
"FROM ML.FORECAST(MODEL `"
+ PROJECT_ID
+ ".bqml.dbt_ads_airflow_model`,STRUCT(30 AS horizon, 0.8 AS confidence_level));"
)
def ml_branch(ds, **kwargs):
if "train" in kwargs["params"] and kwargs["params"]["train"]:
return "train_model"
else:
return "get_predictions"
default_args = {
"owner": "Airflow",
"start_date": datetime(2021, 4, 6),
}
dag = DAG(
dag_id="example_fivetran_bqml",
default_args=default_args,
schedule_interval=timedelta(days=1),
catchup=False,
)
with dag:
linkedin_sync = FivetranOperator(
task_id="linkedin-sync",
fivetran_conn_id="fivetran_default",
connector_id="{{ var.value.linkedin_connector_id }}",
)
linkedin_sensor = FivetranSensor(
task_id="linkedin-sensor",
fivetran_conn_id="fivetran_default",
connector_id="{{ var.value.linkedin_connector_id }}",
poke_interval=5,
)
twitter_sync = FivetranOperator(
task_id="twitter-sync",
fivetran_conn_id="fivetran_default",
connector_id="{{ var.value.twitter_connector_id }}",
)
twitter_sensor = FivetranSensor(
task_id="twitter-sensor",
fivetran_conn_id="fivetran_default",
connector_id="{{ var.value.twitter_connector_id }}",
poke_interval=5,
)
dbt_run = SSHOperator(
task_id="dbt_ad_reporting",
command="cd dbt_ad_reporting ; ~/.local/bin/dbt run -m +ad_reporting",
ssh_conn_id="dbtvm",
)
ml_branch = BranchPythonOperator(
task_id="ml_branch", python_callable=ml_branch, provide_context=True
)
train_model = BigQueryExecuteQueryOperator(
task_id="train_model", sql=TRAINING_QUERY, use_legacy_sql=False
)
get_preds = BigQueryExecuteQueryOperator(
task_id="get_predictions",
sql=SERVING_QUERY,
use_legacy_sql=False,
destination_dataset_table=DATASET_NAME + "." + DESTINATION_TABLE,
write_disposition="WRITE_APPEND",
)
print_preds = BigQueryGetDataOperator(
task_id="print_predictions", dataset_id=DATASET_NAME, table_id=DESTINATION_TABLE
)
linkedin_sync >> linkedin_sensor
twitter_sync >> twitter_sensor
[linkedin_sensor, twitter_sensor] >> dbt_run
dbt_run >> ml_branch >> [train_model, get_preds]
get_preds >> print_preds