2016-03-24 3 views
0

Ich versuche, eine lokale CSV-Upload große Abfrage mit PythonWie eine lokale CSV-Upload große Abfrage Google Python mit

def uploadCsvToGbq(self,table_name): 


    load_config = { 
    'destinationTable': { 
    'projectId': self.project_id, 
    'datasetId': self.dataset_id, 
    'tableId': table_name 
    } 
    } 

    load_config['schema'] = { 
    'fields': [ 
    {'name':'full_name', 'type':'STRING'}, 
    {'name':'age', 'type':'INTEGER'}, 
    ] 
    } 
    load_config['sourceFormat'] = 'CSV' 

    upload = MediaFileUpload('sample.csv', 
        mimetype='application/octet-stream', 
        # This enables resumable uploads. 
        resumable=True) 
    start = time.time() 
    job_id = 'job_%d' % start 
    # Create the job. 
    result = bigquery.jobs.insert(
    projectId=self.project_id, 
    body={ 
    'jobReference': { 
    'jobId': job_id 
    }, 
    'configuration': { 
    'load': load_config 
    } 
    }, 
    media_body=upload).execute() 

    return result 

wenn ich laufe dies wirft Fehler wie

"NameError: global name 'MediaFileUpload' is not defined"

to google

ob ein modul benötigt wird bitte helfen.

+0

Woher haben Sie den Klassennamen 'MediaFileUpload'? – LeeNeverGup

+0

http://stackoverflow.com/questions/25048787/loading-json-file-in-bigquery-using-google-bigquery-client-api Ich versuchte dieses Beispiel – Teejay

Antwort

1
pip install --upgrade google-api-python-client 

Dann auf Ihre Python-Datei schreiben:

from googleapiclient.http import MediaFileUpload 

Aber kümmern Sie einige Klammer verpassen. Besser schreiben:

result = bigquery.jobs().insert(projectId=PROJECT_ID, body={'jobReference': {'jobId': job_id},'configuration': {'load': load_config}}, media_body=upload).execute(num_retries=5) 

Und übrigens, werden Sie alle Ihre CSV-Zeilen laden, darunter die Top-eine, die Spalten definiert.

Verwandte Themen