简体   繁体   English

BigQuery Python 客户端中的变量表名

[英]Variable table name in BigQuery Python Client

Having seen the correct format for the setting of variables for sql queries, I'm not finding a similar way to parameterise the table name.看到 sql 查询的变量设置的正确格式后,我没有找到类似的方法来参数化表名。 For example, I have a simple query as follows例如,我有一个简单的查询如下

query = """
    select price, category, title, sm_title from `product.data_set.table_name` where  
    product = @product"""
    
    
    job_config = bigquery.QueryJobConfig(
    query_parameters=[
        bigquery.ScalarQueryParameter("product", "INT64", product),
    ])
    
    
    product_data = client.query(query, job_config=job_config).to_dataframe()

I would like the table_name to be a variable that can be passed in. Is there can option that can be used like is set in the job_config?我希望table_name是一个可以传入的变量。是否有可以像在 job_config 中设置的那样使用的选项?

Setting of python variable for SQL query can be done using python's f string formatting approach as suggested by @EdoAkse in the comments.为 SQL 查询设置 python 变量可以使用python 的 f 字符串格式化方法完成,正如@EdoAkse 在评论中所建议的那样。

You may use below您可以在下面使用

table_name = "tablenamegoeshere"
query = f""" select price, category, title, sm_title from `product.data_set.{table_name}` where product = @product"""

Posting the answer as community wiki for the benefit of the community that might encounter this use case in the future.将答案发布为社区 wiki ,以造福于将来可能会遇到此用例的社区。

Feel free to edit this answer for additional information.请随意编辑此答案以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM