-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrapy-repos-staging.py
234 lines (195 loc) · 7.02 KB
/
scrapy-repos-staging.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
from datetime import datetime
from airflow.decorators import dag, task, task_group
from airflow.providers.postgres.hooks.postgres import PostgresHook
from airflow.providers.postgres.operators.postgres import PostgresOperator
from airflow.operators.trigger_dagrun import TriggerDagRunOperator
pg_hook = PostgresHook(postgres_conn_id="raw_user")
@dag(schedule=None, start_date=datetime(2021, 12, 1), catchup=False)
def scrapy_repos_staging():
@task(task_id="check_for_duplicates", retries=0)
def check_for_duplicates(relation_name, dupe_col):
dupe_sql = f"""
WITH cte AS (
SELECT
{dupe_col}
FROM {relation_name}
)
SELECT
COUNT({dupe_col}) as count,
{dupe_col}
FROM cte
GROUP BY {dupe_col}
HAVING COUNT({dupe_col}) > 1
"""
conn = pg_hook.get_conn()
cur = conn.cursor()
cur.execute(dupe_sql)
dupes = cur.fetchall()
cur.close()
conn.close()
if len(dupes) > 0:
drop_temp_table(relation_name)
raise Exception(f"Found {len(dupes)} duplicates")
@task(task_id="drop_temp_table", retries=0)
def drop_temp_table(relation_name):
sql = f"""
DROP TABLE IF EXISTS {relation_name};
"""
conn = pg_hook.get_conn()
cur = conn.cursor()
cur.execute(sql)
cur.close()
conn.close()
#### Project Spider ####
create_staging_projectspider_temp = PostgresOperator(
task_id="create_staging_projectspider_temp",
postgres_conn_id="raw_user",
sql="sql/scrapy_repos/staging/create_projectspider.sql",
# add params
params={
"table_suffix": "_temp",
}
)
create_staging_projectspider_prod = PostgresOperator(
task_id="create_staging_projectspider_prod",
postgres_conn_id="raw_user",
sql="sql/scrapy_repos/staging/create_projectspider.sql",
# add params
params={
"table_suffix": "",
}
)
@task_group(group_id="staging_projectspider")
def staging_projectspider():
temp_relation_name = "staging.projectspider_temp"
create_staging_projectspider_temp >> check_for_duplicates(
relation_name=temp_relation_name,
dupe_col="dim_id",
) >> drop_temp_table(
relation_name=temp_relation_name
) >> create_staging_projectspider_prod
#### Repo Spider ####
create_staging_repospider_temp = PostgresOperator(
task_id="create_staging_repospider_temp",
postgres_conn_id="raw_user",
sql="sql/scrapy_repos/staging/create_repospider.sql",
# add params
params={
"table_suffix": "_temp",
}
)
create_staging_repospider_prod = PostgresOperator(
task_id="create_staging_repospider_prod",
postgres_conn_id="raw_user",
sql="sql/scrapy_repos/staging/create_repospider.sql",
# add params
params={
"table_suffix": "",
}
)
@task_group(group_id="staging_repospider")
def staging_repospider():
temp_relation_name = "staging.repospider_temp"
create_staging_repospider_temp >> check_for_duplicates(
relation_name=temp_relation_name,
dupe_col="dim_id",
) >> drop_temp_table(
relation_name=temp_relation_name
) >> create_staging_repospider_prod
#### Contributors Spider ####
create_staging_contributorsspider_temp = PostgresOperator(
task_id="create_staging_contributorsspider_temp",
postgres_conn_id="raw_user",
sql="sql/scrapy_repos/staging/create_contributorsspider.sql",
# add params
params={
"table_suffix": "_temp",
}
)
create_staging_contributorsspider_prod = PostgresOperator(
task_id="create_staging_contributorsspider_prod",
postgres_conn_id="raw_user",
sql="sql/scrapy_repos/staging/create_contributorsspider.sql",
# add params
params={
"table_suffix": "",
}
)
@task_group(group_id="staging_contributorsspiderr")
def staging_contributorsspider():
temp_relation_name = "staging.contributorsspider_temp"
create_staging_contributorsspider_temp >> check_for_duplicates(
relation_name=temp_relation_name,
dupe_col="dim_id",
) >> drop_temp_table(
relation_name=temp_relation_name
) >> create_staging_contributorsspider_prod
#### Profile Spider ####
create_staging_profilespider_temp = PostgresOperator(
task_id="create_staging_profilespider_temp",
postgres_conn_id="raw_user",
sql="sql/scrapy_repos/staging/create_profilespider.sql",
# add params
params={
"table_suffix": "_temp",
}
)
create_staging_profilespider_prod = PostgresOperator(
task_id="create_staging_profilespider_prod",
postgres_conn_id="raw_user",
sql="sql/scrapy_repos/staging/create_profilespider.sql",
# add params
params={
"table_suffix": "",
}
)
@task_group(group_id="staging_profilespiderr")
def staging_profilespider():
temp_relation_name = "staging.profilespider_temp"
create_staging_profilespider_temp >> check_for_duplicates(
relation_name=temp_relation_name,
dupe_col="dim_id",
) >> drop_temp_table(
relation_name=temp_relation_name
) >> create_staging_profilespider_prod
#### Company Spider ####
create_staging_companyspider_temp = PostgresOperator(
task_id="create_staging_companyspider_temp",
postgres_conn_id="raw_user",
sql="sql/scrapy_repos/staging/create_companyspider.sql",
# add params
params={
"table_suffix": "_temp",
}
)
create_staging_companyspider_prod = PostgresOperator(
task_id="create_staging_companyspider_prod",
postgres_conn_id="raw_user",
sql="sql/scrapy_repos/staging/create_companyspider.sql",
# add params
params={
"table_suffix": "",
}
)
@task_group(group_id="staging_companyspiderr")
def staging_companyspider():
temp_relation_name = "staging.companyspider_temp"
create_staging_companyspider_temp >> check_for_duplicates(
relation_name=temp_relation_name,
dupe_col="dim_id",
) >> drop_temp_table(
relation_name=temp_relation_name
) >> create_staging_companyspider_prod
@task_group(group_id="staging")
def staging():
staging_projectspider()
staging_repospider()
staging_contributorsspider()
staging_profilespider()
staging_companyspider()
trigger_maps_dag = TriggerDagRunOperator(
task_id="trigger_maps_dag",
trigger_dag_id="scrapy_repos_maps"
)
staging() >> trigger_maps_dag
scrapy_repos_staging()