2016-10-17 59 views
1

我是新的bigQuery。我需要將表格從BigQuery導出到Google存儲。 目前,我可以通過數據集列出所有表格。 是否有一些boody可以幫助我如何導出表格? 我的Python代碼如下:從谷歌bigquery出口表谷歌存儲

#!/usr/bin/env python 

from googleapiclient import discovery 
from oauth2client.client import GoogleCredentials 
from bigquery_client import client 

credentials = GoogleCredentials.get_application_default() 
service = discovery.build('bigquery', 'v2', credentials=credentials) 

# * Project ID of the datasets to be listed 
projectId = 'xxxxxxxxx' 

datasets = service.datasets() 
request = datasets.list(projectId=projectId) 

response = request.execute() 

for dataset in response['datasets']: 
    datasetId = dataset['datasetReference']['datasetId'] 
    tables = service.tables() 
request = tables.list(projectId=projectId, datasetId=datasetId) 
response = request.execute() 
if 'tables' in response : 
    for table in response['tables']: 
     print ('Dataset name treated is :' + '%s' % dataset['datasetReference']['datasetId']) 
     print ('%s' % dataset['datasetReference']['datasetId']) 
     print ('Is table number:' + '%s' % table['tableReference']['tableId']) 

感謝

回答

1

有文檔頁面上一個完整的Python的例子。你需要兩個功能。 https://cloud.google.com/bigquery/docs/exporting-data

這裏包括以供將來參考:

def export_data_to_gcs(dataset_name, table_name, destination): 
    bigquery_client = bigquery.Client() 
    dataset = bigquery_client.dataset(dataset_name) 
    table = dataset.table(table_name) 
    job_name = str(uuid.uuid4()) 

    job = bigquery_client.extract_table_to_storage(
     job_name, table, destination) 

    job.begin() 

    wait_for_job(job) 

    print('Exported {}:{} to {}'.format(
     dataset_name, table_name, destination)) 

和第二

def wait_for_job(job): 
    while True: 
     job.reload() 
     if job.state == 'DONE': 
      if job.error_result: 
       raise RuntimeError(job.error_result) 
      return 
     time.sleep(1)