Reputation: 343
I'm new on bigQuery. I need to export tables from BigQuery to google storage. For the moment, I can list all tables by dataset. Does some boody can help me with how can I export then tables? My python code is below :
#!/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'])
Thanks
Upvotes: 2
Views: 963
Reputation: 207912
There is a full Python example on the docs page
including here for future reference:
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))
Upvotes: 3