Anas Al-Masri
Anas Al-Masri

Reputation: 21

BigQuery API returns "No query found"

I am trying to query my BigQuery database using Python but every time I run this code I get an error saying "No query found" even though the query works fine on Google Cloud Console.

Note: myfilepath.json and my-project-id are valid values in the code I have.

def explicit():
    import os
    os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "myfilepath.json"
    credentials = GoogleCredentials.get_application_default()
    bigquery_service = build('bigquery', 'v2', credentials=credentials)
    query_request = bigquery_service.jobs()

    query_data = {
        'query': ('#standardSQL SELECT * FROM `SentimentAnalysis.testdataset`')
    }

    query_response = query_request.query(
        projectId='my-project-id',
        body=query_data).execute()
    print(query_response)

explicit()

The error I get every time is:

googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/bigquery/v2/projects/my-project-id/queries?alt=json returned "1.58 - 1.58: No query found.">

Upvotes: 1

Views: 859

Answers (1)

Guillem Xercavins
Guillem Xercavins

Reputation: 7058

Working snippet based on Mikhail's advice using 'useLegacySql': False:

from apiclient.discovery import build

def explicit():
    bigquery_service = build('bigquery', 'v2')

    query_request = bigquery_service.jobs()

    query_data = {
        'query': ('SELECT * FROM `dataset.table`'),
        'useLegacySql': False
    }

    query_response = query_request.query(
        projectId='PROJECT_ID',
        body=query_data).execute()
    print(query_response)

explicit()

Alternatively, you can use the idiomatic client:

from google.cloud import bigquery

client = bigquery.Client()

QUERY = ("""
    SELECT * FROM `project.dataset.table`
    LIMIT 10""")

query_job = client.query(QUERY)

results = query_job.result()
rows = list(results)

for row in rows:
    print row

Upvotes: 2

Related Questions