我尝试使用 Python 查询 BigQuery 数据库,但每次运行此代码时,我都会收到一条错误消息,提示“未找到查询”,即使该查询在 Google Cloud Console 上运行良好。
注意:myfilepath.json 和 my-project-id 是我代码中的有效值。
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()
我每次遇到的错误是:
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.">
最佳答案
基于 Mikhail 建议的工作片段,使用 '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()
或者,您可以使用惯用的客户端:
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
关于python - BigQuery API 返回 "No query found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48586861/