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

Update samples to support latest Google Cloud Python #656

Merged
merged 13 commits into from
Nov 15, 2016
Prev Previous commit
Next Next commit
Update bigquery samples to use latest google-cloud
Change-Id: I6cbcca11be84baa7c40eed14b342cc916b9116d9
Jon Wayne Parrott committed Nov 15, 2016

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 1374be3d33d9ceab68165c79e2220ef39606fc57
55 changes: 7 additions & 48 deletions bigquery/cloud-client/snippets.py
Original file line number Diff line number Diff line change
@@ -35,18 +35,7 @@
def list_projects():
bigquery_client = bigquery.Client()

projects = []
page_token = None

while True:
results, page_token = bigquery_client.list_projects(
page_token=page_token)
projects.extend(results)

if not page_token:
break

for project in projects:
for project in bigquery_client.list_projects():
print(project.project_id)


@@ -57,18 +46,7 @@ def list_datasets(project=None):
"""
bigquery_client = bigquery.Client(project=project)

datasets = []
page_token = None

while True:
results, page_token = bigquery_client.list_datasets(
page_token=page_token)
datasets.extend(results)

if not page_token:
break

for dataset in datasets:
for dataset in bigquery_client.list_datasets():
print(dataset.name)


@@ -98,17 +76,7 @@ def list_tables(dataset_name, project=None):
print('Dataset {} does not exist.'.format(dataset_name))
return

tables = []
page_token = None

while True:
results, page_token = dataset.list_tables(page_token=page_token)
tables.extend(results)

if not page_token:
break

for table in tables:
for table in dataset.list_tables():
print(table.name)


@@ -157,19 +125,10 @@ def list_rows(dataset_name, table_name, project=None):
# Reload the table so that the schema is available.
table.reload()

rows = []
page_token = None

# Load at most 25 results. You can change this to `while True` and change
# the max_results argument to load more rows from BigQuery, but note
# that this can take some time. It's preferred to use a query.
while len(rows) < 25:
results, total_rows, page_token = table.fetch_data(
max_results=25, page_token=page_token)
rows.extend(results)

if not page_token:
break
# Load at most 25 results. You can change the max_results argument to load
# more rows from BigQuery, but note that this can take some time. It's
# preferred to use a query.
rows = table.fetch_data(max_results=25)
Copy link
Contributor

Choose a reason for hiding this comment

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

You may prefer to just consume the iterator right here:

rows = list(table.fetch_data(max_results=25))

There is too much suspense between this and the for row in rows (when the request(s) actually get made)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair enough, done.


# Use format to create a simple table.
format_string = '{!s:<16} ' * len(table.schema)