-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathETL_RUN.py
48 lines (35 loc) · 1.16 KB
/
ETL_RUN.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
from airflow import DAG
from airflow.operators.python import PythonOperator, PythonVirtualenvOperator
from airflow.utils.dates import days_ago
def run_etl():
import psycopg2
import requests
import sys
conn = psycopg2.connect(host='host.docker.internal', database='airflow', user='airflow', password='airflow')
cur = conn.cursor()
sess = requests.session()
resp = sess.get('https://statsapi.web.nhl.com/api/v1/teams/21/stats')
cur.execute('INSERT INTO aerode.stage_json VALUES (%s)', (resp.text,))
cur.execute('SELECT aerode.f_parse_statistics()')
result = cur.fetchone()[0]
if result == 1:
cur.execute('TRUNCATE aerode.stage_json')
else:
sys.exit('Unexpected error has occured')
cur.connection.commit()
cur.close()
cur.connection.close()
with DAG(
dag_id='Run_ETL',
schedule_interval='0 */12 * * *',
start_date=days_ago(1),
catchup=False,
max_active_runs=1
) as dag:
etl_task = PythonVirtualenvOperator(
task_id="etl_task",
python_callable=run_etl,
requirements=["psycopg2-binary", "requests"],
system_site_packages=True,
)
etl_task