【问题标题】:Google BigQuery - Python Query not being parsed correctlyGoogle BigQuery - Python 查询未正确解析
【发布时间】:2016-07-27 15:41:38
【问题描述】:

我正在尝试使用 Python API 设置一个简单的 Google BigQuery 应用程序。我遵循了快速入门指南:

import argparse

from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from oauth2client.client import GoogleCredentials


def main(project_id):
    print "hello"
    # [START build_service]
    # Grab the application's default credentials from the environment.
    credentials = GoogleCredentials.get_application_default()
    # Construct the service object for interacting with the BigQuery API.
    bigquery_service = build('bigquery', 'v2', credentials=credentials)
    # [END build_service]

    query_request = bigquery_service.jobs()
    query_data = {
    'query': (
          'SELECT ticker,close1'
          'FROM Data.data_7 '
          'WHERE ticker = "GTIM"'
          'LIMIT 10')
    }

    query_response = query_request.query(
                                 projectId=project_id,
                                 body=query_data).execute()

   print('Query Results:')
   for row in query_response['rows']:
        print('\t'.join(field['v'] for field in row['f']))


main("sqlserver-1384")

并且能够成功运行上述查询。 但是,每当我将其更改为:

   'query': (
          'SELECT ticker,close1'
          'FROM Data.data_7 '
          'ORDER BY close1 ASC'
          'LIMIT 10')
    }

我收到以下错误:

Traceback (most recent call last):
  File "server1.py", line 57, in <module>
    main("sqlserver-1384")
  File "server1.py", line 50, in main
    body=query_data).execute()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/oauth2client/util.py", line 135, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/googleapiclient/http.py", line 832, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/bigquery/v2/projects/sqlserver-1384/queries?alt=json returned "Encountered " <ID> "ASCLIMIT "" at line 1, column 54.
Was expecting:
    <EOF>">

我的格式有问题吗? 我在 Google BigQuery Web 控制台上运行了相同的查询,它运行良好。

谢谢

【问题讨论】:

    标签: python google-app-engine google-bigquery


    【解决方案1】:

    当查询字符串被 python 解析器连接时,你会得到 ASCLIMIT 这个词,它不是有效的 BQ SQL。在查询中的ASC 后面加一个空格就可以了。

    {
        'query': (
              'SELECT ticker,close1 '  # Space at the end of this line too
              'FROM Data.data_7 '
              'ORDER BY close1 ASC '  # Space at the end of this line
              'LIMIT 10')
    }
    

    或者,使用三引号字符串编写查询:

    '''
    SELECT ticker, close1
    FROM Data.data_7
    ORDER BY close1 ASC
    LIMIT 10
    '''
    

    这样会保留换行符。

    【讨论】:

    • 感谢您的正确回答和编辑。我会尽快标记为正确答案。
    猜你喜欢
    • 2017-05-13
    • 2021-09-26
    • 2020-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 1970-01-01
    • 2017-12-17
    相关资源
    最近更新 更多