如何使用目标表在BigQuery中撰写查询?

时间:2018-01-30 13:49:34

标签: python-2.7 google-bigquery

我正在尝试查询BQ表并使用legacy_sql将查询的数据加载到目标表中

代码:

bigquery_client = bigquery.Client.from_service_account_json(config.ZF_FILE)
job_config = bigquery.QueryJobConfig()
job_config.use_legacy_sql = True

# Allow for query results larger than the maximum response size
job_config.allow_large_results = True

# When large results are allowed, a destination table must be set.
dest_dataset_ref = bigquery_client.dataset('datasetId')
dest_table_ref = dest_dataset_ref.table('datasetId:mydestTable')
job_config.destination = dest_table_ref

query =""" SELECT abc FROM [{0}] LIMIT 10 """.format(mySourcetable_name)

# run the Query here now
query_job = bigquery_client.query(query, job_config=job_config)

错误:

google.api_core.exceptions.BadRequest: 400 POST : Invalid dataset ID "datasetId:mydestTable". Dataset IDs must be alphanumeric (plus underscores, dashes, and colons) and must be at most 1024 characters long.

job_config.destination给出:

print job_config.destination

TableReference(u'projectName', 'projectName:dataset', 'projectName:dataset.mydest_table')

数据集ID在我这边仍然是错误的吗?

我可以知道如何获得正确的目的地表吗?

1 个答案:

答案 0 :(得分:1)

这可能对将来的某人有所帮助

它只是命名名称而不是数据集和表格的完整ID,如下所示

dest_dataset_ref = bigquery_client.dataset('dataset_name')
dest_table_ref = dest_dataset_ref.table('mydestTable_name')