2016-10-17 3 views
1

bigquery에 새로 왔습니다. BigQuery에서 Google 스토리지로 테이블을 내보내야합니다. 현재로서는 모든 테이블을 데이터 집합으로 나열 할 수 있습니다. 일부 boody는 테이블을 어떻게 내보낼 수 있습니까? 내 파이썬 코드는 다음과 같습니다 :google bigquery에서 Google 저장 용량으로 테이블 내보내기

#!/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

워드 프로세서 페이지에서 전체 파이썬 예제가 있습니다. 필요한 두 가지 기능이 있습니다.

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) 
: 향후 참조를 위해 여기에 포함 https://cloud.google.com/bigquery/docs/exporting-data

관련 문제