Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added export format option #45

Merged
merged 4 commits into from
Jul 1, 2015
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions bigquery/samples/export_data_to_cloud_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# [START export_table]
def export_table(service, cloud_storage_path,
projectId, datasetId, tableId,
export_format="CSV",
num_retries=5):
"""
Starts an export job
Expand All @@ -28,8 +29,10 @@ def export_table(service, cloud_storage_path,
service: initialized and authorized bigquery
google-api-client object,
cloud_storage_path: fully qualified
path to a Google Cloud Storage location,
e.g. gs://mybucket/myfolder/
path to a Google Cloud Storage location,
e.g. gs://mybucket/myfolder/
export_format: format to export in;
"CSV", "NEWLINE_DELIMITED_JSON", or "AVRO".

Returns: an extract job resource representing the
job, see https://cloud.google.com/bigquery/docs/reference/v2/jobs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring still has wrong indentations.

It should be as follows:

def export_table(service, cloud_storage_path,
                 projectId, datasetId, tableId,
                 export_format="CSV",
                 num_retries=5):
    """
    Starts an export job.

    Args:
        service: Initialized and authorized bigquery
            google-api-client object.
        cloud_storage_path: Fully qualified
            path to a Google Cloud Storage location.
            e.g. gs://mybucket/myfolder/
        export_format: Format to export in;
            "CSV", "NEWLINE_DELIMITED_JSON", or "AVRO".

    Returns: An extract job resource representing the
        job, see https://cloud.google.com/bigquery/docs/reference/v2/jobs
    """
  • Use period at the end of the clause
  • Use capitalized first letter
  • Correct indentation (continuation line should have deeper indent, for example, the second line for the "service" argument should have deeper indentation)

Expand All @@ -49,6 +52,7 @@ def export_table(service, cloud_storage_path,
'tableId': tableId,
},
'destinationUris': [cloud_storage_path],
'destinationFormat': export_format
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add test for this option?

}
}
}
Expand All @@ -61,11 +65,13 @@ def export_table(service, cloud_storage_path,
# [START run]
def run(cloud_storage_path,
projectId, datasetId, tableId,
num_retries, interval):
num_retries, interval, export_format="CSV"):

bigquery = get_service()
resource = export_table(bigquery, cloud_storage_path,
projectId, datasetId, tableId, num_retries)
projectId, datasetId, tableId,
num_retries=num_retries,
export_format=export_format)
poll_job(bigquery,
resource['jobReference']['projectId'],
resource['jobReference']['jobId'],
Expand Down
22 changes: 20 additions & 2 deletions bigquery/tests/test_export_data_to_cloud_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,32 @@

class TestExportTableToGCS(CloudBaseTest):

def test_export_table(self):
def test_export_table_csv(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests only check if the run() succeeds.
Is it possible to mock the client and check if the right RPCs are made?

run(self.constants['cloudStorageInputURI'],
self.constants['projectId'],
self.constants['datasetId'],
self.constants['newTableId'],
5,
5)
5,
export_format="CSV")

def test_export_table_json(self):
run(self.constants['cloudStorageInputURI'],
self.constants['projectId'],
self.constants['datasetId'],
self.constants['newTableId'],
5,
5,
export_format="NEWLINE_DELIMITED_JSON")

def test_export_table_avro(self):
run(self.constants['cloudStorageInputURI'],
self.constants['projectId'],
self.constants['datasetId'],
self.constants['newTableId'],
5,
5,
export_format="AVRO")

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