我正在尝试遍历BigQuery数据集中的表,并执行SQL查询并导出表结果。尝试在for循环中包含表名时出现错误。
for i in range(0, 3):
dataset_id = 'test_dataset'
tabelname = "test"+ str(i)
tableOutName = tabelname + "_cleaned"
job_config = bigquery.QueryJobConfig()
# Set the destination table
table_ref = client.dataset(dataset_id).table(tableOutName)
job_config.destination = table_ref
sql = "\"\"\"" + " SELECT * FROM " + "`my-bucket-name.{}.{}` ".format(dataset_id, tabelname) + "WHERE SAFE.ST_GeogFromText(WKT) IS NOT NULL " +"\"\"\""
# Start the query, passing in the extra configuration.
query_job = client.query(
sql,
location='EU',
job_config=job_config) # API request - starts the query
query_job.result() # Waits for the query to finish
print('Query results loaded to table {}'.format(table_ref.path))
答案 0 :(得分:0)
documentation中三引号的用法在Python代码中为delimiters for the string,而不是字符串本身的一部分。
因此,以下代码行必须相应地更改:
sql = "\"\"\"" + " SELECT * FROM " + "`my-bucket-name.{}.{}` ".format(dataset_id, tabelname) + "WHERE SAFE.ST_GeogFromText(WKT) IS NOT NULL " +"\"\"\""
文档以这种方式使用它:
sql = """
SELECT corpus
FROM `bigquery-public-data.samples.shakespeare`
GROUP BY corpus;
"""