export tables from google bigquery to google storage
<p>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 :</p>

<pre><code>#!/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'])
</code></pre>

<p>Thanks</p>

<p>There is a full Python example on the docs page. Two functions you need is there. 
<a href="https://cloud.google.com/bigquery/docs/exporting-data" rel="nofollow">https://cloud.google.com/bigquery/docs/exporting-data</a></p>

<p>including here for future reference:</p>

<pre><code>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))
</code></pre>

<p>and the second</p>

<pre><code>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)
</code></pre>

python google-bigquery