python - Airflow - GoogleCloudStorageToBigQueryOperator 不呈现模板化的 source_objects

标签 python google-cloud-platform airflow google-cloud-composer

documentation声明 source_objects 参数采用模板化值。但是,当我尝试以下操作时:

gcs_to_bq_op = GoogleCloudStorageToBigQueryOperator(
    task_id=name,
    bucket='gdbm-public',
    source_objects=['entity/{{ ds_nodash }}.0.{}.json'.format(filename)],
    destination_project_dataset_table='dbm_public_entity.{}'.format(name),
    schema_fields=schema,
    source_format='NEWLINE_DELIMITED_JSON',
    create_disposition='CREATE_IF_NEEDED',
    write_disposition='WRITE_TRUNCATE',
    max_bad_records=0,
    allow_jagged_rows=True,
    google_cloud_storage_conn_id='my_gcp_conn',
    bigquery_conn_id='my_gcp_conn',
    delegate_to=SERVICE_ACCOUNT,
    dag=dag
    )

我收到错误信息: 异常:BigQuery 作业失败。最终错误是:{u'reason': u'notFound', u'message': u'Not found: URI gs://gdbm-public/entity/{ ds_nodash }.0.GeoLocation.json'}。

我找到了一个 example其中 {{ ds_nodash }} 变量以相同的方式使用。所以我不确定为什么这对我不起作用。

最佳答案

问题是在字符串上调用 .format 会导致一组双括号被删除:

>>> 'entity/{{ ds_nodash }}.0.{}.json'.format(filename)
'entity/{ ds_nodash }.0.foobar.json'

您需要通过加倍来转义您希望在字符串中的大括号:

>>> 'entity/{{{{ ds_nodash }}}}.0.{}.json'.format(filename)
'entity/{{ ds_nodash }}.0.foobar.json'

关于python - Airflow - GoogleCloudStorageToBigQueryOperator 不呈现模板化的 source_objects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52261904/

相关文章:

python - 尽管单元测试正常运行,Authtoken 迁移仍无法正常工作

python - Openpyxl:似乎无法获得适合我想要做的事情的语法(向下阅读下一个单元格)

machine-learning - Google 云平台设置错误 : (gcloud. beta.ml) 无效选择: 'init-project'

python - 环境错误 : HTTPSConnectionPool while pip install on docker container

python - Airflow 用户创建

google-cloud-platform - 通过airflow检索SMTP密码的命令

python - 对于很长的数据库,有没有办法知道 panda `to_csv` 的状态?

Python:循环读取所有文本文件行

google-cloud-platform - Spanner - 如何查找 table 尺寸

java - Apache Beam 组合分组值