2016-07-27 11 views
1

Ich versuche, eine einfache Google BigQuery-Anwendung mithilfe der Python-API einzurichten. Ich folgte der Quick-Start-Anleitung:Google BigQuery - Python-Abfrage wird nicht korrekt analysiert

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") 

Und konnte die obige Abfrage erfolgreich ausführen. Allerdings, wenn ich ändere es:

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

ich die folgende Fehlermeldung erhalten:

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>"> 

Ist etwas nicht in Ordnung mit meinem Format? Ich habe die gleiche Abfrage in der Google BigQuery Web Console ausgeführt und es hat gut funktioniert.

Danke

Antwort

3

Wenn der Query-String vom Python-Parser verkettet wird, sind Sie mit dem Wort ASCLIMIT links, die nicht gültig BQ SQL ist. Fügen Sie in der Abfrage ein Leerzeichen nach ASC hinzu, und Sie sollten OK sein.

{ 
    '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') 
} 

Alternativ schreiben Sie Ihre Abfrage triple Strings in Anführungszeichen verwendet:

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

Als dann Zeilenumbrüche bleiben erhalten.

+0

Vielen Dank für die richtige Antwort und Änderungen. Wird als richtige Antwort markieren, sobald ich es darf. –