Airflow 无法使用GoogleCloud操作系统迭代xcom_拉列表

Airflow 无法使用GoogleCloud操作系统迭代xcom_拉列表,airflow,Airflow,我想动态获取gcs bucket上的csv文件列表,然后将每个文件转储到相应的BQ表中 我正在使用GoogleCloudStorageListOperator和GoogleCloudStorageToBigQueryOperator操作符 GCS_Files = GoogleCloudStorageListOperator( task_id='GCS_Files', bucket=cf.storage.import_bucket_n

我想动态获取gcs bucket上的csv文件列表,然后将每个文件转储到相应的BQ表中

我正在使用GoogleCloudStorageListOperatorGoogleCloudStorageToBigQueryOperator操作符

GCS_Files = GoogleCloudStorageListOperator(
                task_id='GCS_Files',
                bucket=cf.storage.import_bucket_name,
                prefix='20190701/',
                delimiter='.csv',
                dag=dag
            )

for idx, elem in enumerate(["{{ task_instance.xcom_pull(task_ids='GCS_Files') }}"]):
    storage_to_bigquery = GoogleCloudStorageToBigQueryOperator(
            task_id='storage_to_bigquery',
            bucket=cf.storage.import_bucket_name,
            create_disposition='CREATE_IF_NEEDED',
            autodetect=True,
            destination_project_dataset_table=f"{cf.project}.{cf.bigquery.core_dataset_name}.{idx}",
            skip_leading_rows=1,
            source_format='CSV', 
            source_objects=[f'{elem}'],
            write_disposition='WRITE_TRUNCATE',
            dag=dag
            )

    storage_to_bigquery.set_upstream(GCS_Files)

但是,列表无法一次迭代一个,并抛出以下错误

googleapiclient.errors.HttpError: <HttpError 400 when requesting https://bigquery.googleapis.com/bigquery/v2/projects/my-project/jobs?alt=json returned "Source URI must not contain the ',' character: gs://mybucket/['20190701/file0.csv', '20190701/file1.csv', '20190701/file2.csv']">
googleapiclient.errors.HttpError:

有什么建议吗?提前感谢。

您不能从代码中的任何地方调用宏。 这在代码中被视为一个字符串:“{{task_instance.xcom_pull(task_id='GCS_Files')}” 然后在传入gcp运算符时由Jinja2进行计算,因为您使用的是模板字段:

为了能够调用task_instance.xcom_pull,您需要有一个上下文,它只能存在于DAG运行中。当气流惰性地计算DAG时,此选项不可用


在您的情况下,最好使用一个子DAG在运算符上循环,使用您的宏生成要循环的文件列表:

您不能从代码中的任何地方调用宏。 这在代码中被视为一个字符串:“{{task_instance.xcom_pull(task_id='GCS_Files')}” 然后在传入gcp运算符时由Jinja2进行计算,因为您使用的是模板字段:

为了能够调用task_instance.xcom_pull,您需要有一个上下文,它只能存在于DAG运行中。当气流惰性地计算DAG时,此选项不可用

在您的情况下,最好使用一个子DAG循环您的操作符,使用your宏生成要循环的文件列表:

同意@Breathe,
上下文
(因此,
xcom
s)只能在任务(操作符)运行时访问,而不能在DAG构造期间访问。有关更多详细信息,请参阅带有@Breathe的页面,
context
(因此
xcom
s)只能在任务(操作员)运行时访问,而不能在DAG构造期间访问。有关更多详细信息,请参阅