-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcountry3.py
63 lines (47 loc) · 2.05 KB
/
country3.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
import datetime
from airflow import models
from airflow.operators.bash_operator import BashOperator
from airflow.operators.dummy_operator import DummyOperator
default_dag_args = {
# https://airflow.apache.org/faq.html#what-s-the-deal-with-start-date
'start_date': datetime.datetime(2020, 4, 27)
}
dataset = 'airflow'
table = 'Country'
query_cmd = 'bq query --use_legacy_sql=false '
table_cmd = 'create or replace table ' + dataset + '.' + table + '(id int64, name string)'
insert_cmd = 'insert into ' + dataset + '.' + table + '(id, name)'
update_cmd = 'update ' + dataset + '.' + table
with models.DAG(
'country3',
schedule_interval=None,
default_args=default_dag_args) as dag:
create_dataset = BashOperator(
task_id='create_dataset',
bash_command='bq --location=US mk --dataset ' + dataset)
create_table = BashOperator(
task_id='create_table',
bash_command=query_cmd + "'" + table_cmd + "'",
trigger_rule='all_done')
branch = DummyOperator(
task_id='branch',
trigger_rule='all_done')
insert_usa = BashOperator(
task_id='insert_usa',
bash_command=query_cmd + "'" + insert_cmd + ' values(1, "\'"USA"\'")' + "'",
trigger_rule='one_success')
insert_canada = BashOperator(
task_id='insert_canada',
bash_command=query_cmd + "'" + insert_cmd + ' values(2, "\'"Canada"\'")' + "'",
trigger_rule='one_success')
update_usa = BashOperator(
task_id='update_usa',
bash_command=query_cmd + "'" + update_cmd + ' set id = 3 where id = 1' + "'",
trigger_rule='one_success')
update_canada = BashOperator(
task_id='update_canada',
bash_command=query_cmd + "'" + update_cmd + ' set id = 4 where id = 2' + "'",
trigger_rule='one_success')
create_dataset >> create_table >> branch
branch >> insert_usa >> update_usa
branch >> insert_canada >> update_canada