2

文档说明该参数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
    )

我收到错误消息: Exception: BigQuery job failed. Final error was: {u'reason': u'notFound', u'message': u'Not found: URI gs://gdbm-public/entity/{ ds_nodash }.0.GeoLocation.json'}.

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

4

2 回答 2

4

问题是调用.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'
于 2018-09-10T16:57:49.600 回答
3

这个问题与 Dustin 所描述的完全一样,调用.format字符串会导致一组双括号被删除。但是,不是将作为 1 解决方案的括号加倍:

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

我发现以这种方式格式化字符串以避免混淆更容易:

"entity/{0}.0.{1}.json".format("{{ ds_nodash }}", filename)
于 2018-09-26T18:37:55.730 回答