python - BigQuery API 返回 "No query found"

标签 python google-bigquery google-cloud-platform

我尝试使用 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/

相关文章:

python - 在 PyCharm 中使用模块运行 App Engine 开发服务器

python - 为什么 SymPy 1.1.1 在计算这个基本积分时返回错误?

sql - BigQuery 在标准模式下使用 regex_extract_all 选择别名

google-analytics - 如何在 Bigquery 中获取唯一页面浏览量的 Google Analytics 定义

用于谷歌云 SQL 的 GoLang SDK

python - 如何删除在上一个日期时间的结束时间之前开始的日期时间

python - 为什么 bool 表达式 "1 in (1, 2, 3) == True"为 False?

google-bigquery - 奇怪的问题: Bigquery dataset not found

kubernetes - 从服务帐户对受 Google IAP 保护的资源的编程访问被拒绝,并出现无效签名错误

python-2.7 - 如何在谷歌云函数中用python做出http响应