Skip to content

Commit 8e7e89c

Browse files
authored
doc(bigquery): add code sample for scripting (#9537)
* doc(bigquery): add code sample for scripting This code sample initiates a scripting job and then demonstrates how to fetch the results and the child jobs. Removes system test for scripting, as it's redundant with the code sample. * blacken * add snippet to query how-to
1 parent 73a07d4 commit 8e7e89c

File tree

4 files changed

+110
-31
lines changed

4 files changed

+110
-31
lines changed

bigquery/docs/usage/queries.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,16 @@ See BigQuery documentation for more information on
4848
:dedent: 4
4949
:start-after: [START bigquery_query_params_named]
5050
:end-before: [END bigquery_query_params_named]
51+
52+
Run a script
53+
^^^^^^^^^^^^
54+
55+
See BigQuery documentation for more information on `scripting in BigQuery
56+
standard SQL
57+
<https://cloud.google.com/bigquery/docs/reference/standard-sql/scripting>`_.
58+
59+
.. literalinclude:: ../samples/query_script.py
60+
:language: python
61+
:dedent: 4
62+
:start-after: [START bigquery_query_script]
63+
:end-before: [END bigquery_query_script]

bigquery/samples/query_script.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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_script(client):
17+
# [START bigquery_query_script]
18+
# TODO(developer): Import the client library.
19+
# from google.cloud import bigquery
20+
21+
# TODO(developer): Construct a BigQuery client object.
22+
# client = bigquery.Client()
23+
24+
# Run a SQL script.
25+
sql_script = """
26+
-- Declare a variable to hold names as an array.
27+
DECLARE top_names ARRAY<STRING>;
28+
29+
-- Build an array of the top 100 names from the year 2017.
30+
SET top_names = (
31+
SELECT ARRAY_AGG(name ORDER BY number DESC LIMIT 100)
32+
FROM `bigquery-public-data.usa_names.usa_1910_2013`
33+
WHERE year = 2000
34+
);
35+
36+
-- Which names appear as words in Shakespeare's plays?
37+
SELECT
38+
name AS shakespeare_name
39+
FROM UNNEST(top_names) AS name
40+
WHERE name IN (
41+
SELECT word
42+
FROM `bigquery-public-data.samples.shakespeare`
43+
);
44+
"""
45+
parent_job = client.query(sql_script)
46+
47+
# Wait for the whole script to finish.
48+
rows_iterable = parent_job.result()
49+
print("Script created {} child jobs.".format(parent_job.num_child_jobs))
50+
51+
# Fetch result rows for the final sub-job in the script.
52+
rows = list(rows_iterable)
53+
print(
54+
"{} of the top 100 names from year 2000 also appear in Shakespeare's works.".format(
55+
len(rows)
56+
)
57+
)
58+
59+
# Fetch jobs created by the SQL script.
60+
child_jobs_iterable = client.list_jobs(parent_job=parent_job)
61+
for child_job in child_jobs_iterable:
62+
child_rows = list(child_job.result())
63+
print(
64+
"Child job with ID {} produced {} row(s).".format(
65+
child_job.job_id, len(child_rows)
66+
)
67+
)
68+
69+
# [END bigquery_query_script]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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_script
16+
17+
18+
def test_query_script(capsys, client):
19+
20+
query_script.query_script(client)
21+
out, _ = capsys.readouterr()
22+
assert "Script created 2 child jobs." in out
23+
assert (
24+
"53 of the top 100 names from year 2000 also appear in Shakespeare's works."
25+
in out
26+
)
27+
assert "produced 53 row(s)" in out
28+
assert "produced 1 row(s)" in out

bigquery/tests/system.py

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -431,37 +431,6 @@ def test_list_tables(self):
431431
)
432432
self.assertGreater(len(list(iterator)), 0)
433433

434-
def test_listing_scripting_jobs(self):
435-
# Run a SQL script.
436-
sql_script = """
437-
-- Declare a variable to hold names as an array.
438-
DECLARE top_names ARRAY<STRING>;
439-
440-
-- Build an array of the top 100 names from the year 2017.
441-
SET top_names = (
442-
SELECT ARRAY_AGG(name ORDER BY number DESC LIMIT 100)
443-
FROM `bigquery-public-data.usa_names.usa_1910_current`
444-
WHERE year = 2017
445-
);
446-
447-
-- Which names appear as words in Shakespeare's plays?
448-
SELECT
449-
name AS shakespeare_name
450-
FROM UNNEST(top_names) AS name
451-
WHERE name IN (
452-
SELECT word
453-
FROM `bigquery-public-data.samples.shakespeare`
454-
);
455-
"""
456-
parent_job = Config.CLIENT.query(sql_script, project=Config.CLIENT.project)
457-
parent_job.result()
458-
459-
# Fetch jobs created by the SQL script.
460-
child_jobs = list(Config.CLIENT.list_jobs(parent_job=parent_job))
461-
462-
assert parent_job.num_child_jobs > 0
463-
assert len(child_jobs) == parent_job.num_child_jobs
464-
465434
def test_update_table(self):
466435
dataset = self.temp_dataset(_make_dataset_id("update_table"))
467436

0 commit comments

Comments
 (0)