From c7e3c18e01fa22d7afac2ae9fc36e4480587282e Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Tue, 29 Jan 2019 11:13:11 -0800 Subject: [PATCH 1/2] Add sample for fetching total_rows from query results. --- bigquery/docs/snippets.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/bigquery/docs/snippets.py b/bigquery/docs/snippets.py index 4d7e17e5363e..1f6ea2b2b29a 100644 --- a/bigquery/docs/snippets.py +++ b/bigquery/docs/snippets.py @@ -2225,6 +2225,32 @@ def test_client_query_legacy_sql(client): # [END bigquery_query_legacy] +def test_client_query_total_rows(client, capsys): + """Run a query an just check for how many rows.""" + # [START bigquery_query_total_rows] + # from google.cloud import bigquery + # client = bigquery.Client() + + query = ( + "SELECT name FROM `bigquery-public-data.usa_names.usa_1910_2013` " + 'WHERE state = "TX" ' + "LIMIT 100" + ) + query_job = client.query( + query, + # Location must match that of the dataset(s) referenced in the query. + location="US", + ) # API request - starts the query + + results = query_job.result() # Waits for query to complete. + next(iter(results)) # Fetch the first page of results, which contains total_rows. + print("Got {} rows.".format(results.total_rows)) + # [START bigquery_query_total_rows] + + out, _ = capsys.readouterr() + assert "Got 100 rows." in out + + def test_manage_job(client): sql = """ SELECT corpus From 0f2e10ac54e701d7a56e4ffa8d18b57464cf1b8c Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Tue, 29 Jan 2019 16:37:49 -0800 Subject: [PATCH 2/2] Copypasta --- bigquery/docs/snippets.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bigquery/docs/snippets.py b/bigquery/docs/snippets.py index 1f6ea2b2b29a..5356700a495a 100644 --- a/bigquery/docs/snippets.py +++ b/bigquery/docs/snippets.py @@ -2226,7 +2226,7 @@ def test_client_query_legacy_sql(client): def test_client_query_total_rows(client, capsys): - """Run a query an just check for how many rows.""" + """Run a query and just check for how many rows.""" # [START bigquery_query_total_rows] # from google.cloud import bigquery # client = bigquery.Client() @@ -2245,7 +2245,7 @@ def test_client_query_total_rows(client, capsys): results = query_job.result() # Waits for query to complete. next(iter(results)) # Fetch the first page of results, which contains total_rows. print("Got {} rows.".format(results.total_rows)) - # [START bigquery_query_total_rows] + # [END bigquery_query_total_rows] out, _ = capsys.readouterr() assert "Got 100 rows." in out