BigQuery Python 客户端中的变量 table 名称

Variable table name in BigQuery Python Client

看到 sql 查询的变量设置的正确格式后,我没有找到类似的方法来参数化 table 名称。比如我有一个简单的查询如下

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()

我希望 table_name 是一个可以传入的变量。是否可以像 job_config 中设置的那样使用选项?

可以使用 python 的 f 字符串 格式化方法为 SQL 查询设置 python 变量,如@EdoAkse 在评论。

您可以在下面使用

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

将答案发布为 community wiki 以造福于将来可能会遇到此用例的社区。

随时编辑此答案以获取更多信息。