-
Notifications
You must be signed in to change notification settings - Fork 0
/
On_affiliate_export_gcs_schedule 50 0 ^^^.py
333 lines (272 loc) · 14.6 KB
/
On_affiliate_export_gcs_schedule 50 0 ^^^.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
Python 3.6.7rc1 (v3.6.7rc1:311101f7b6, Sep 26 2018, 16:33:24) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import pandas as pd
import csv
import os
import sys
import logging
from airflow import DAG
from airflow.macros import datetime, ds_add, ds_format
from airflow.operators import (
GCSUpload,
MySQLCsvDump,
S3Upload,
SlackHQOperator,
BigQueryLoad
)
from google.cloud.bigquery.schema import SchemaField
from airflow.operators.latest_only_operator import LatestOnlyOperator
from airflow.operators.python_operator import PythonOperator
from static_feed import trivago_static_feed, skyscanner_static_feed, hotelscombined_static_feed, kayak_static_feed
self_path = os.path.dirname(os.path.abspath("__file__"))
s3_affiliate_paths = {
'hotel_data': 'affiliates/partners/unified/hotel_data.csv',
'hotel_info': 'affiliates/partners/unified/hotel_info.csv',
'hotel_photo': 'affiliates/partners/unified/hotel_photo.csv',
'hotel_room': 'affiliates/partners/unified/hotel_room.csv',
'airpay_hotel_amenities': 'affiliates/partners/airpay/airpay_hotel_amenities.csv',
'airpay_hotel_amenities_info': 'affiliates/partners/airpay/airpay_hotel_amenities_info.csv',
'airpay_hotel_data': 'affiliates/partners/airpay/airpay_hotel_data.csv',
'airpay_hotel_photo': 'affiliates/partners/airpay/airpay_hotel_photo.csv',
'gogobot_hotel_data': 'affiliates/partners/gogobot/gogobot_hotel_data.csv',
'hotellook_hotel_photo': 'affiliates/partners/hotellook/hotellook_hotel_photo.csv',
'hotelscombined_hotel_data': 'hotelscombined/hotel_data.csv',
'kayak_hotel_data': 'affiliates/partners/kayak/kayak_hotel_data.csv',
'kayak_direct_hotel': 'affiliates/partners/kayak/kayak_direct_hotel_data.csv',
'momondo_hotel_amenities': 'affiliates/partners/momondo/momondo_hotel_amenities.csv',
'momondo_hotel_amenities_info': 'affiliates/partners/momondo/momondo_hotel_amenities_info.csv',
'momondo_hotel_data': 'affiliates/partners/momondo/momondo_hotel_data.csv',
'momondo_hotel_image': 'affiliates/partners/momondo/momondo_hotel_image.csv',
'skyscanner_hotel_data': 'affiliates/partners/skyscanner/skyscanner_hotel_data.csv',
'tripadvisor_hotel_data': 'affiliates/partners/tripadvisor/tripadvisor_hotel_data.csv',
'trivago_hotel_data': 'affiliates/partners/trivago/trivago_hotel_data.csv'
}
def _local_cleanup(table_name, **context):
file_path = context['task_instance'].xcom_pull(key='file_path',
task_ids='{}_dump'.format(table_name))
os.remove(file_path)
def dag_folder_cleanup(table_name):
for item in os.listdir(self_path):
if item.endswith(table_name + '.csv'):
os.remove(item)
def python_dump(table_name, file_path=''):
def _cleanup(**context):
dag_folder_cleanup(table_name)
def get_static_feed(**context):
mse = table_name.split('_')[0]
if mse == 'trivago':
static_feed_df = trivago_static_feed.get_trivago_static_feed()
elif mse == 'skyscanner':
static_feed_df = skyscanner_static_feed.get_final_list()
elif mse == 'hotelscombined':
static_feed_df = hotelscombined_static_feed.get_hc_static_feed()
elif mse == 'kayak':
static_feed_df = kayak_static_feed.get_kayak_static_feed()
static_feed_file = self_path + table_name + '.csv'
static_feed_df.to_csv(static_feed_file, sep=',', quoting=csv.QUOTE_MINIMAL, index=False, encoding='utf-8')
return static_feed_file
def bigquery_transform(**context):
logging.info('execution_date: {}'.format(context['execution_date']))
checking_date = context['execution_date'].strftime('%Y-%m-%d')
short_date = ds_add(checking_date, 1)
context['task_instance'].xcom_push(key='short_date', value=short_date)
static_feed_file = context['task_instance'].xcom_pull(task_ids='get_{}_static_feed'.
format(table_name.split('_')[0]))
bq = pd.read_csv(static_feed_file, delimiter=',', quoting=csv.QUOTE_MINIMAL, encoding='utf-8')
bq.drop(labels=['address2', 'country_id', 'longitude', 'latitude', 'phone', 'fax', 'web', 'email', 'city_id',
'currency'], axis=1,
inplace=True)
bq.columns = ['hotel_name', 'hotel_address', 'hotel_city', 'hotel_country', 'hotel_id', 'hotel_state',
'postcode']
bq['postcode'] = bq['postcode'].astype(str)
bq['feed_date'] = short_date
cols = bq.columns.tolist()
cols = [cols[-1]] + cols[:-1] #Make date the first column
cols = [cols[0]] + [cols[5]] + cols[1:5] + cols[6:] #Make hotel_id the second column
bq = bq[cols]
bq.to_csv(static_feed_file, sep=',', quoting=csv.QUOTE_MINIMAL, index=False, encoding='utf-8')
return static_feed_file
def generate_gcs_remote_path(**context):
short_date = context['task_instance'].xcom_pull(task_ids='{}_bigquery_transform'.
format(table_name.split('_')[0]), key='short_date')
return 'trivago/trivago-{}.csv'.format(short_date.replace('-', ''))
def generate_bigquery_table_name(**context):
short_date = context['task_instance'].xcom_pull(task_ids='{}_bigquery_transform'.
format(table_name.split('_')[0]), key='short_date')
return 'trivago_{}'.format(short_date.replace('-', ''))
latest_only_operator = LatestOnlyOperator(
task_id='{}_latest_only'.format(table_name),
dag=dag)
get_static_feed_operator = PythonOperator(
task_id='get_{}_static_feed'.format(table_name.split('_')[0]),
on_failure_callback=SlackHQOperator.on_failure_general_callback,
python_callable=get_static_feed,
provide_context=True,
dag=dag)
if table_name.split('_')[0] == 'trivago':
bigquery_transform_operator = PythonOperator(
task_id='{}_bigquery_transform'.format(table_name.split('_')[0]),
on_failure_callback=SlackHQOperator.on_failure_general_callback,
python_callable=bigquery_transform,
provide_context=True,
dag=dag)
generate_gcs_remote_path_operator = PythonOperator(
task_id='generate_{}_gcs_remote_path'.format(table_name.split('_')[0]),
on_failure_callback=SlackHQOperator.on_failure_general_callback,
python_callable=generate_gcs_remote_path,
provide_context=True,
dag=dag)
trivago_gcs_upload_operator = GCSUpload(
task_id='{}_gcs_upload'.format(table_name.split('_')[0]),
on_failure_callback=SlackHQOperator.on_failure_general_callback,
bucket='affiliate-hotel-data',
local_path_xcom_task_id='{}_bigquery_transform'.format(table_name.split('_')[0]),
remote_dir_xcom_task_id='generate_{}_gcs_remote_path'.format(table_name.split('_')[0]),
content_type='text/csv',
dag=dag)
generate_bigquery_table_name_operator = PythonOperator(
task_id='generate_{}_bigquery_table_name'.format(table_name.split('_')[0]),
on_failure_callback=SlackHQOperator.on_failure_general_callback,
python_callable=generate_bigquery_table_name,
provide_context=True,
dag=dag)
bigquery_load_operator = BigQueryLoad(
task_id='{}_bigquery_load'.format(table_name.split('_')[0]),
on_failure_callback=SlackHQOperator.on_failure_general_callback,
dataset='static_feed',
table_xcom_task_id='generate_{}_bigquery_table_name'.format(table_name.split('_')[0]),
schema=[SchemaField('feed_date', 'DATE'),
SchemaField('hotel_id', 'INTEGER'),
SchemaField('hotel_name', 'STRING'),
SchemaField('hotel_address', 'STRING'),
SchemaField('hotel_city', 'STRING'),
SchemaField('hotel_country', 'STRING'),
SchemaField('hotel_state', 'STRING'),
SchemaField('postcode', 'STRING')],
gs_path_xcom_task_id='{}_gcs_upload'.format(table_name.split('_')[0]),
write_disposition='WRITE_TRUNCATE',
field_delimiter=',',
source_format='CSV',
skip_leading_rows=1,
dag=dag)
gcs_upload_operator = GCSUpload(
task_id='{}_gcs_upload'.format(table_name),
on_failure_callback=SlackHQOperator.on_failure_general_callback,
bucket='affiliate-hotel-data',
local_path_xcom_task_id='get_{}_static_feed'.format(table_name.split('_')[0]),
remote_path=file_path,
content_type='text/csv',
dag=dag)
if table_name in s3_affiliate_paths:
s3_upload_operator = S3Upload(
task_id='{}_s3_upload'.format(table_name),
on_failure_callback=SlackHQOperator.on_failure_general_callback,
bucket='hq-data-extracts',
local_path_xcom_task_id='get_{}_static_feed'.format(table_name.split('_')[0]),
remote_path=s3_affiliate_paths[table_name],
dag=dag)
local_cleanup_operation = PythonOperator(
task_id='{}_cleanup'.format(table_name),
on_failure_callback=SlackHQOperator.on_failure_general_callback,
python_callable=_cleanup,
provide_context=True,
dag=dag)
gcs_upload_operator << get_static_feed_operator << latest_only_operator
if table_name in s3_affiliate_paths:
s3_upload_operator << get_static_feed_operator
if table_name.split('_')[0] == 'trivago':
bigquery_transform_operator << s3_upload_operator
trivago_gcs_upload_operator << generate_gcs_remote_path_operator << bigquery_transform_operator
bigquery_load_operator << generate_bigquery_table_name_operator << trivago_gcs_upload_operator
local_cleanup_operation << bigquery_load_operator
local_cleanup_operation << s3_upload_operator
local_cleanup_operation << gcs_upload_operator
def mysql_dump(database_name, table_name, file_path=''):
def _cleanup(**context):
_local_cleanup(table_name, **context)
latest_only_operator = LatestOnlyOperator(
task_id='{}_latest_only'.format(table_name),
dag=dag)
dump_operator = MySQLCsvDump(
conn_id='alice_database',
table_name=table_name,
database_name=database_name,
header=False,
batch_size=100000,
task_id='{}_dump'.format(table_name),
pool='alice_database',
on_failure_callback=SlackHQOperator.on_failure_general_callback,
dag=dag)
gcs_upload_operator = GCSUpload(
task_id='{}_gcs_upload'.format(table_name),
on_failure_callback=SlackHQOperator.on_failure_general_callback,
bucket='affiliate-hotel-data',
local_path_xcom_key='file_path',
local_path_xcom_task_id='{}_dump'.format(table_name),
remote_path=file_path,
content_type='text/csv',
dag=dag)
if table_name in s3_affiliate_paths:
s3_upload_operator = S3Upload(
task_id='{}_s3_upload'.format(table_name),
on_failure_callback=SlackHQOperator.on_failure_general_callback,
bucket='hq-data-extracts',
local_path_xcom_key='file_path',
local_path_xcom_task_id='{}_dump'.format(table_name),
remote_path=s3_affiliate_paths[table_name],
dag=dag)
local_cleanup_operation = PythonOperator(
task_id='{}_local_cleanup'.format(table_name),
on_failure_callback=SlackHQOperator.on_failure_general_callback,
python_callable=_cleanup,
provide_context=True,
dag=dag)
gcs_upload_operator << dump_operator << latest_only_operator
if table_name in s3_affiliate_paths:
s3_upload_operator << dump_operator
local_cleanup_operation << s3_upload_operator
local_cleanup_operation << gcs_upload_operator
dag = DAG(
dag_id='affiliate_export_gcs',
schedule_interval='50 0 * * *',
start_date=datetime(2017, 10, 16),
catchup=False,
default_args={
'retires': 0
}
)
mysql_dump('affiliate_export', 'mapping_works_data', 'mapping_works/hotel_data.csv')
# unified
mysql_dump('affiliate_export', 'hotel_data', 'unified/hotel_data.csv')
mysql_dump('affiliate_export', 'hotel_info', 'unified/hotel_info.csv')
mysql_dump('affiliate_export', 'hotel_photo', 'unified/hotel_photo.csv')
mysql_dump('affiliate_export', 'hotel_room', 'unified/hotel_room.csv')
# airpay
mysql_dump('affiliate_export', 'airpay_hotel_amenities', 'airpay/hotel_amenities.csv')
mysql_dump('affiliate_export', 'airpay_hotel_amenities_info', 'airpay/hotel_amenities_info.csv')
mysql_dump('affiliate_export', 'airpay_hotel_data', 'airpay/hotel_data.csv')
mysql_dump('affiliate_export', 'airpay_hotel_photo', 'airpay/hotel_photo.csv')
# gogobot
mysql_dump('affiliate_export', 'gogobot_hotel_data', 'gogobot/hotel_data.csv')
# hotellook
mysql_dump('affiliate_export', 'hotellook_hotel_photo', 'hotellook/hotel_photo.csv')
# hotelscombined
# mysql_dump('affiliate_export', 'hotelscombined_hotel_data', 'hotelscombined/hotel_data.csv')
python_dump('hotelscombined_hotel_data', 'hotelscombined/hotel_data.csv')
# kayak
# mysql_dump('affiliate_export', 'kayak_hotel_data', 'kayak/hotel_data.csv')
python_dump('kayak_hotel_data', 'kayak/hotel_data.csv')
mysql_dump('affiliate_export', 'kayak_direct_hotel', 'kayak/direct_hotel_data.csv')
# momondo
mysql_dump('affiliate_export', 'momondo_hotel_amenities', 'momondo/hotel_amenities.csv')
mysql_dump('affiliate_export', 'momondo_hotel_amenities_info', 'momondo/hotel_amenities_info.csv')
mysql_dump('affiliate_export', 'momondo_hotel_data', 'momondo/hotel_data.csv')
mysql_dump('affiliate_export', 'momondo_hotel_image', 'momondo/hotel_image.csv')
# skyscanner
# mysql_dump('affiliate_export', 'skyscanner_hotel_data', 'skyscanner/hotel_data.csv')
python_dump('skyscanner_hotel_data', 'skyscanner/hotel_data.csv')
# tripadvisor
# mysql_dump('affiliate_export', 'tripadvisor_hotel_data', 'tripadvisor/hotel_data.csv')
# trivago
# mysql_dump('affiliate_export', 'trivago_hotel_data', 'trivago/hotel_data.csv')
python_dump('trivago_hotel_data', 'trivago/hotel_data.csv')