0

I'm trying to write a simple query using the Python client library named "parameter", but kept encountering errors.

I keep getting "Undeclared query parameters" when I try to run the code. Did I miss out anything?

My Code:

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 Answer 1

1

It looks like you are missing to enter your job_config into the arguments of your client.query() method. You should have:

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

Official docs here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.