是的,确实如此。不过,我建议您切换到也支持参数的官方 Google BigQuery 客户端库。
BigQuery 客户端库:
https://cloud.google.com/bigquery/docs/reference/libraries#client-libraries-install-python
使用 Pandas GBQ 设置参数
您可以使用configuration 参数在 Pandas GBQ 查询中设置参数,以引用 Pandas GBQ docs:
configuration : dict, 作业的可选查询配置参数
加工。例如:
configuration = {‘query’: {‘useQueryCache’: False}}
这是来自该链接的完整代码示例,描述了如何在 Pandas GBQ 中参数化查询:
import pandas
sql = """
SELECT name
FROM `bigquery-public-data.usa_names.usa_1910_current`
WHERE state = @state
"""
query_config = {
'query': {
'parameterMode': 'NAMED',
'queryParameters': [
{
'name': 'state',
'parameterType': {'type': 'STRING'}
},
]
}
}
df = pandas.read_gbq(sql, configuration=query_config)
使用 BigQuery 客户端库设置参数
这是一篇关于从 Pandas-GBQ 迁移到 BigQuery 客户端库的优秀文章:
https://cloud.google.com/bigquery/docs/pandas-gbq-migration
这里是一些示例 Python 代码,我在使用官方 BQ 客户端库的查询中使用参数:
table_name = "my_table"
job_config = bigquery.QueryJobConfig()
# Set the destination table
table_ref = client.dataset(dataset_id).table(table_name)
job_config.destination = table_ref
job_config.write_disposition = 'WRITE_APPEND'
sql = """
SELECT * FROM dataset.table WHERE visit_date = date
"""
query_params = [bigquery.ScalarQueryParameter('date', 'DATE', date)]
job_config.query_parameters = query_params
# 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