Skip to content

Commit 4368528

Browse files
committed
Merge pull request #45 from GoogleCloudPlatform/export
Added export format option
2 parents d2818ba + 771d2fa commit 4368528

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

bigquery/samples/export_data_to_cloud_storage.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,22 @@
2020
# [START export_table]
2121
def export_table(service, cloud_storage_path,
2222
projectId, datasetId, tableId,
23+
export_format="CSV",
2324
num_retries=5):
2425
"""
2526
Starts an export job
2627
2728
Args:
2829
service: initialized and authorized bigquery
29-
google-api-client object,
30+
google-api-client object.
3031
cloud_storage_path: fully qualified
31-
path to a Google Cloud Storage location,
32-
e.g. gs://mybucket/myfolder/
32+
path to a Google Cloud Storage location.
33+
e.g. gs://mybucket/myfolder/
34+
export_format: format to export in;
35+
"CSV", "NEWLINE_DELIMITED_JSON", or "AVRO".
3336
3437
Returns: an extract job resource representing the
35-
job, see https://cloud.google.com/bigquery/docs/reference/v2/jobs
38+
job, see https://cloud.google.com/bigquery/docs/reference/v2/jobs
3639
"""
3740
# Generate a unique job_id so retries
3841
# don't accidentally duplicate export
@@ -49,6 +52,7 @@ def export_table(service, cloud_storage_path,
4952
'tableId': tableId,
5053
},
5154
'destinationUris': [cloud_storage_path],
55+
'destinationFormat': export_format
5256
}
5357
}
5458
}
@@ -61,11 +65,13 @@ def export_table(service, cloud_storage_path,
6165
# [START run]
6266
def run(cloud_storage_path,
6367
projectId, datasetId, tableId,
64-
num_retries, interval):
68+
num_retries, interval, export_format="CSV"):
6569

6670
bigquery = get_service()
6771
resource = export_table(bigquery, cloud_storage_path,
68-
projectId, datasetId, tableId, num_retries)
72+
projectId, datasetId, tableId,
73+
num_retries=num_retries,
74+
export_format=export_format)
6975
poll_job(bigquery,
7076
resource['jobReference']['projectId'],
7177
resource['jobReference']['jobId'],

bigquery/tests/test_export_data_to_cloud_storage.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,32 @@
2222

2323
class TestExportTableToGCS(CloudBaseTest):
2424

25-
def test_export_table(self):
25+
def test_export_table_csv(self):
2626
run(self.constants['cloudStorageInputURI'],
2727
self.constants['projectId'],
2828
self.constants['datasetId'],
2929
self.constants['newTableId'],
3030
5,
31-
5)
31+
5,
32+
export_format="CSV")
33+
34+
def test_export_table_json(self):
35+
run(self.constants['cloudStorageInputURI'],
36+
self.constants['projectId'],
37+
self.constants['datasetId'],
38+
self.constants['newTableId'],
39+
5,
40+
5,
41+
export_format="NEWLINE_DELIMITED_JSON")
3242

43+
def test_export_table_avro(self):
44+
run(self.constants['cloudStorageInputURI'],
45+
self.constants['projectId'],
46+
self.constants['datasetId'],
47+
self.constants['newTableId'],
48+
5,
49+
5,
50+
export_format="AVRO")
3351

3452
if __name__ == '__main__':
3553
unittest.main()

0 commit comments

Comments
 (0)