-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
4 changed files
with
110 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Copyright 2019 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
def query_script(client): | ||
# [START bigquery_query_script] | ||
# TODO(developer): Import the client library. | ||
# from google.cloud import bigquery | ||
|
||
# TODO(developer): Construct a BigQuery client object. | ||
# client = bigquery.Client() | ||
|
||
# Run a SQL script. | ||
sql_script = """ | ||
-- Declare a variable to hold names as an array. | ||
DECLARE top_names ARRAY<STRING>; | ||
-- Build an array of the top 100 names from the year 2017. | ||
SET top_names = ( | ||
SELECT ARRAY_AGG(name ORDER BY number DESC LIMIT 100) | ||
FROM `bigquery-public-data.usa_names.usa_1910_2013` | ||
WHERE year = 2000 | ||
); | ||
-- Which names appear as words in Shakespeare's plays? | ||
SELECT | ||
name AS shakespeare_name | ||
FROM UNNEST(top_names) AS name | ||
WHERE name IN ( | ||
SELECT word | ||
FROM `bigquery-public-data.samples.shakespeare` | ||
); | ||
""" | ||
parent_job = client.query(sql_script) | ||
|
||
# Wait for the whole script to finish. | ||
rows_iterable = parent_job.result() | ||
print("Script created {} child jobs.".format(parent_job.num_child_jobs)) | ||
|
||
# Fetch result rows for the final sub-job in the script. | ||
rows = list(rows_iterable) | ||
print( | ||
"{} of the top 100 names from year 2000 also appear in Shakespeare's works.".format( | ||
len(rows) | ||
) | ||
) | ||
|
||
# Fetch jobs created by the SQL script. | ||
child_jobs_iterable = client.list_jobs(parent_job=parent_job) | ||
for child_job in child_jobs_iterable: | ||
child_rows = list(child_job.result()) | ||
print( | ||
"Child job with ID {} produced {} row(s).".format( | ||
child_job.job_id, len(child_rows) | ||
) | ||
) | ||
|
||
# [END bigquery_query_script] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Copyright 2019 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from .. import query_script | ||
|
||
|
||
def test_query_script(capsys, client): | ||
|
||
query_script.query_script(client) | ||
out, _ = capsys.readouterr() | ||
assert "Script created 2 child jobs." in out | ||
assert ( | ||
"53 of the top 100 names from year 2000 also appear in Shakespeare's works." | ||
in out | ||
) | ||
assert "produced 53 row(s)" in out | ||
assert "produced 1 row(s)" in out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters