-
Notifications
You must be signed in to change notification settings - Fork 0
/
row_by_row2.py
75 lines (70 loc) · 2.25 KB
/
row_by_row2.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
import sys
import os
from datetime import datetime
from airflow.models import Variable
from airflow.models.xcom import XCom
from airflow.providers.snowflake.hooks.snowflake import SnowflakeHook
#AirflowException allows retries
from airflow import AirflowException
def do_snowflake_files(single_file):
print("passing single_file")
print(single_file)
os.chdir('/home/mpapet/airflow/dags/datasets/pickup')
process_file_list = list()
all_files_list = os.listdir("./")
#KISS any file here has not been imported to snowflake
for i in all_files_list:
print(i)
if i.endswith('_move_row.sql') and i not in process_file_list:
print(i)
process_file_list.append(i)
if len(process_file_list) == 0:
print("no files to process")
return('')
else:
print("sending file list")
print(process_file_list)
try:
snow_op = SnowflakeOperator(
task_id='insert-to-snowflake',
snowflake_conn_id='snow_for_me',
warehouse="COMPUTE_WH",
database="DEMO_DATA",
schema="PUBLIC",
autocommit=True,
)
except Exception as e:
print(e)
print('error inserting to snowflake')
raise AirflowException(e)
snowflake_query = ''
fips_list = []
for f in list(process_file_list):
print("checking {}".format(f))
file_stats = os.stat(f)
fips_finder = f.split('_')
if file_stats.st_size > 10 and fips_finder not in fips_list:
fips_list.append(str(fips_finder[0]))
fh = open(f, "r+")
qry = fh.read()
print("!!!!!!!!!!!! DEBUG !!!!!!!!!!!!!!!")
print(qry)
snow_op.run(qry, autocommit=TRUE, handler="first_snowflake_handle")
else:
#should never be here.
print("{} is too small?? {}".format(f,file_stats.st_size))
print("snowflake handle returned")
print(snow_op.first_snowflake_handle)
timestamp = datetime.now()
file_name = 'update_row_' + timestamp.strftime("%Y-%m-%d-%H:%M:%S") + ".sql"
update_query = "UPDATE public.public_statistics set date_exported = CURRENT_TIMESTAMP where county_fips in ({}); \n COMMIT;".format(','.join(fips_list))
try:
f = open(file_name, "w")
f.write(update_query)
f.close()
except Exception as e:
print(e)
print('error writing pgsql update query')
raise AirflowException(e)
print("DEBUG returning {}".format(file_name))
return(file_name)