【问题标题】:Dag example pet failed to insert and getbirthdag示例宠物插入失败并分娩
【发布时间】:2021-09-14 08:55:41
【问题描述】:

我是airflow 的新手。我根据官方文档设置了所有内容。我使用pet example DAG,但是当我查看DAG 日志时,它会显示以下错误:

第一个错误来自populate_pet_table

psycopg2.errors.InvalidTextRepresentation: invalid input syntax for type integer: "Maxy"
LINE 2:             INSERT INTO pet VALUES ('Maxy', 'Dog', '2018-07-...

第二个错误来自get_birth_date

BETWEEN SYMMETRIC 2020-01-01 AND 2020-12-31;


HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.

那里有什么问题?这真的是官方的例子,所以对我来说应该可以正常工作。这是 dag 的代码:

import datetime

from airflow import DAG
from airflow.providers.postgres.operators.postgres import PostgresOperator

# create_pet_table, populate_pet_table, get_all_pets, and get_birth_date are examples of tasks created by
# instantiating the Postgres Operator

with DAG(
    dag_id="postgres_operator_dag",
    start_date=datetime.datetime(2020, 2, 2),
    schedule_interval="@once",
    catchup=False,
) as dag:
    create_pet_table = PostgresOperator(
        task_id="create_pet_table",
        postgres_conn_id="postgres_default",
        sql="""
            CREATE TABLE IF NOT EXISTS pet (
            pet_id SERIAL PRIMARY KEY,
            name VARCHAR NOT NULL,
            pet_type VARCHAR NOT NULL,
            birth_date DATE NOT NULL,
            OWNER VARCHAR NOT NULL);
          """,
    )
    populate_pet_table = PostgresOperator(
        task_id="populate_pet_table",
        postgres_conn_id="postgres_default",
        sql="""
            INSERT INTO pet VALUES ( 'Max', 'Dog', '2018-07-05', 'Jane');
            INSERT INTO pet VALUES ( 'Susie', 'Cat', '2019-05-01', 'Phil');
            INSERT INTO pet VALUES ( 'Lester', 'Hamster', '2020-06-23', 'Lily');
            INSERT INTO pet VALUES ( 'Quincy', 'Parrot', '2013-08-11', 'Anne');
            """,
    )
    get_all_pets = PostgresOperator(
        task_id="get_all_pets", postgres_conn_id="postgres_default", sql="SELECT * FROM pet;"
    )
    get_birth_date = PostgresOperator(
        task_id="get_birth_date",
        postgres_conn_id="postgres_default",
        sql="""
            SELECT * FROM pet
            WHERE birth_date
            BETWEEN SYMMETRIC {{ params.begin_date }} AND {{ params.end_date }};
            """,
        params={'begin_date': '2020-01-01', 'end_date': '2020-12-31'},
    )

    create_pet_table >> populate_pet_table >> get_all_pets >> get_birth_date

【问题讨论】:

    标签: sql postgresql airflow


    【解决方案1】:

    SQL 本身似乎有错误。

    变化:

    INSERT INTO pet VALUES ( 'Max', 'Dog', '2018-07-05', 'Jane');
    INSERT INTO pet VALUES ( 'Susie', 'Cat', '2019-05-01', 'Phil');
    INSERT INTO pet VALUES ( 'Lester', 'Hamster', '2020-06-23', 'Lily');
    INSERT INTO pet VALUES ( 'Quincy', 'Parrot', '2013-08-11', 'Anne');
    

    收件人:

    INSERT INTO pet (name, pet_type, birth_date, OWNER) VALUES ( 'Max', 'Dog', '2018-07-05', 'Jane');
    INSERT INTO pet (name, pet_type, birth_date, OWNER) VALUES ( 'Susie', 'Cat', '2019-05-01', 'Phil');
    INSERT INTO pet (name, pet_type, birth_date, OWNER) VALUES ( 'Lester', 'Hamster', '2020-06-23', 'Lily');
    INSERT INTO pet (name, pet_type, birth_date, OWNER) VALUES ( 'Quincy', 'Parrot', '2013-08-11', 'Anne');
    

    对于第二个问题你需要改变

    BETWEEN SYMMETRIC {{ params.begin_date }} AND {{ params.end_date }};
    

    到:

    BETWEEN SYMMETRIC DATE '{{ params.begin_date }}' AND DATE '{{ params.end_date }}';
    

    它会正常工作。 来自sqlfiddle的示例

    我提出了PR 来解决这个问题。

    【讨论】:

    • 似乎修复了第一个错误,那么第二个呢?
    • 是的,请让原点知道这里的问题,因为现在一切正常。 P.S 你能告诉我如果我不想运行这些任务之一,我可以这样做吗?我尝试删除(get_birth_date)所以我现在有:create_pet_table >> populate_pet_table >> get_all_pets 但 dag 的日志显示它仍在运行:O
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    • 1970-01-01
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    相关资源
    最近更新 更多