从google bigquery导出表格到谷歌存储

时间:2016-10-17 14:37:12

标签: python google-bigquery

我是bigQuery的新手。我需要将表格从BigQuery导出到谷歌存储。 目前,我可以按数据集列出所有表格。 有些boody可以帮助我如何导出表格? 我的python代码如下:

#!/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 个答案:

答案 0 :(得分:3)

docs page

上有一个完整的Python示例

包括此处以供将来参考:

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 = bigquery_client.extract_table(
        table, destination)

    job.result()

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