Skip to content

Commit 68c2cc8

Browse files
authored
docs(bigquery): add sample to read from query destination table (#9964)
This sample addresses internal issue 134774673, in which we recieved feedback that it's not clear how to paginate through the results of a query. This sample shows that all completed queries have a destination table, which can be read from with tabledata.list. Note: The client library avoids the extra call to `get_table` if query results are read directly from the QueryJob, but it's confusing to show that we're able to get the schema from a private API call to getQueryResults.
1 parent 8f67bf8 commit 68c2cc8

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def query_pagination(client):
17+
18+
# [START bigquery_query_pagination]
19+
# TODO(developer): Import the client library.
20+
# from google.cloud import bigquery
21+
22+
# TODO(developer): Construct a BigQuery client object.
23+
# client = bigquery.Client()
24+
25+
query = """
26+
SELECT name, SUM(number) as total_people
27+
FROM `bigquery-public-data.usa_names.usa_1910_2013`
28+
GROUP BY name
29+
ORDER BY total_people DESC
30+
"""
31+
query_job = client.query(query) # Make an API request.
32+
query_job.result() # Wait for the query to complete.
33+
34+
# Get the destination table for the query results.
35+
#
36+
# All queries write to a destination table. If a destination table is not
37+
# specified, the BigQuery populates it with a reference to a temporary
38+
# anonymous table after the query completes.
39+
destination = query_job.destination
40+
41+
# Get the schema (and other properties) for the destination table.
42+
#
43+
# A schema is useful for converting from BigQuery types to Python types.
44+
destination = client.get_table(destination)
45+
46+
# Download rows.
47+
#
48+
# The client library automatically handles pagination.
49+
print("The query data:")
50+
rows = client.list_rows(destination, max_results=20)
51+
for row in rows:
52+
print("name={}, count={}".format(row["name"], row["total_people"]))
53+
# [END bigquery_query_pagination]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from .. import query_pagination
16+
17+
18+
def test_query_pagination(capsys, client):
19+
20+
query_pagination.query_pagination(client)
21+
out, _ = capsys.readouterr()
22+
assert "The query data:" in out
23+
assert "name=James, count=4942431" in out

0 commit comments

Comments
 (0)