BigQuery Python客户端库-命名参数错误

时间:2018-07-02 12:06:27

标签: google-bigquery

我正在尝试使用名为“ parameter”的Python客户端库编写一个简单的查询,但是一直遇到错误。

当我尝试运行代码时,我不断收到“未声明的查询参数”。我错过了什么吗?

我的代码:

import datetime
import os
from google.cloud import bigquery

os.environ["GOOGLE_APPLICATION_CREDENTIALS"]=<path>
client = bigquery.Client(project='project_id')

query = """
    SELECT * from `<project_id>.<dataset_id>.*` 
    WHERE CAST(REGEXP_EXTRACT(_TABLE_SUFFIX, r"^(\d{8})$") AS INT64) = @date
    limit 10;
    """

query_params = [
    bigquery.ScalarQueryParameter(
        'date', 
        'INT64', 
        int((datetime.date.today().strftime('%Y%m%d'))
        )
    ]

job_config = bigquery.QueryJobConfig()
job_config.query_parameters = query_params

query_job = client.query(
    query,
    location = 'US') 

for row in query_job:
    print(row)

assert query_job.state == 'DONE'

1 个答案:

答案 0 :(得分:1)

您似乎缺少将job_config输入到client.query()方法的参数中的想法。您应该具有:

query_job = client.query(
    query,
    location = 'US',
    job_config=job_config) 

Official docs here