简体   繁体   English

使用 python 中的 Airflow 触发 SQL 时出现模板错误?

[英]Getting Template Error while triggering SQL using Airflow from python?

I am trying to run a SQL script from Airflow.我正在尝试从 Airflow 运行 SQL 脚本。 It is failing due to a Template error.由于模板错误而失败。

The script is basically trying a run a sql from Athena and load into Redshift table.该脚本基本上是尝试从 Athena 运行 sql 并加载到 Redshift 表中。

SQl is placed at: redshift/sql/public/flow/KN_AWS_RE_ShipmentData_dup_update.sql SQl 位于:redshift redshift/sql/public/flow/KN_AWS_RE_ShipmentData_dup_update.sql

My Airflow Code我的 Airflow 代码

from datetime import datetime, timedelta

from airflow import DAG
from airflow.operators.postgres_operator import PostgresOperator

from utils import FAILURE_EMAILS

yesterday = datetime.combine(datetime.today() - timedelta(1), datetime.min.time())

default_args = {
    'owner': 'airflow',
    'depends_on_past': False,
    'start_date': yesterday,
    'email': FAILURE_EMAILS,
    'email_on_failure': False,
    'email_on_retry': False,
    'retries': 1,
    'retry_delay': timedelta(minutes=5)
}

dag = DAG('sample_sql', default_args=default_args, schedule_interval='0 4 * * *')

execute_notebook = PostgresOperator(
    task_id='sample_sql',
    postgres_conn_id='REDSHIFT_CONN',
    sql="redshift/sql/public/flow/KN_AWS_RE_ShipmentData_dup_update.sql",
    params={'limit': '50'},
    dag=dag
)

Error错误

[2020-04-14 02:19:24,412] {{standard_task_runner.py:52}} INFO - Started process 23012 to run task
[2020-04-14 02:19:24,482] {{logging_mixin.py:112}} INFO - [2020-04-14 02:19:24,481] {{dagbag.py:403}} INFO - Filling up the DagBag from /usr/local/airflow/dags/Scripts/Sample.py
[2020-04-14 02:19:24,495] {{baseoperator.py:807}} ERROR - KN_AWS_RE_ShipmentData_dup_update.sql
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/airflow/models/baseoperator.py", line 805, in resolve_template_files
    setattr(self, field, env.loader.get_source(env, content)[0])
  File "/usr/local/lib/python3.7/site-packages/jinja2/loaders.py", line 187, in get_source
    raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: KN_AWS_RE_ShipmentData_dup_update.sql
[2020-04-14 02:19:24,545] {{logging_mixin.py:112}} INFO - Running %s on host %s <TaskInstance: sample_sql.sample_sql 2020-04-14T02:14:08.020072+00:00 [running]> 0ca54c719ff7
[2020-04-14 02:19:24,583] {{taskinstance.py:1088}} ERROR - KN_AWS_RE_ShipmentData_dup_update.sql
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/airflow/models/taskinstance.py", line 940, in _run_raw_task
    self.render_templates(context=context)

How do I fix this issue?我该如何解决这个问题?

You have to specify the path to the .sql template file at the instantiation of the DAG, with the variable template_searchpath .您必须在 DAG 实例化时使用变量template_searchpath指定.sql模板文件的路径。 By default Jinja will look into your DAG folder.默认情况下,Jinja 会查看您的 DAG 文件夹。

Note that your DAG contains one bad practice, that is having a start_date that is dynamic.请注意,您的 DAG 包含一种不好的做法,即具有动态的start_date The start_date should be fixed (ie datetime(2020,4,13) ) instead of dynamic (ie datetime.now() ). start_date应该是固定的(即datetime(2020,4,13) )而不是动态的(即datetime.now() )。 You can read more about it here .你可以在这里阅读更多关于它的信息。

This said I would try to change the DAG definition to this:这表示我会尝试将 DAG 定义更改为:

from datetime import datetime, timedelta

from airflow import DAG
from airflow.operators.postgres_operator import PostgresOperator

from utils import FAILURE_EMAILS

# Remove this
# yesterday = datetime.combine(datetime.today() - timedelta(1), datetime.min.time())

default_args = {
    'owner': 'airflow',
    'depends_on_past': False,
    'start_date': datetime(2020,4,13),  # Change this
    'email': FAILURE_EMAILS,
    'email_on_failure': False,
    'email_on_retry': False,
    'retries': 1,
    'retry_delay': timedelta(minutes=5)
}

dag = DAG(
    'sample_sql', 
    default_args=default_args, 
    schedule_interval='0 4 * * *',
    template_searchpath='/redshift/sql/public/flow/')

execute_notebook = PostgresOperator(
    task_id='sample_sql',
    postgres_conn_id='REDSHIFT_CONN',
    sql='KN_AWS_RE_ShipmentData_dup_update.sql',
    params={'limit': '50'},
    dag=dag
)

execute_notebook  # Tell airflow the tasks dependencies, in this case no dependency

Of course you should choose the correct absolute base path to assign to template_searchpath , so something like /home/redshift/sql/public/flow .当然,您应该选择正确的绝对基本路径来分配给template_searchpath ,例如/home/redshift/sql/public/flow

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM