|
| 1 | +# Copyright 2024 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 client_query_shortmode() -> None: |
| 17 | + # [START bigquery_query_shortquery] |
| 18 | + # This example demonstrates issuing a query that may be run in short query mode. |
| 19 | + # |
| 20 | + # To enable the short query mode preview feature, the QUERY_PREVIEW_ENABLED |
| 21 | + # environmental variable should be set to `TRUE`. |
| 22 | + from google.cloud import bigquery |
| 23 | + |
| 24 | + # Construct a BigQuery client object. |
| 25 | + client = bigquery.Client() |
| 26 | + |
| 27 | + query = """ |
| 28 | + SELECT |
| 29 | + name, |
| 30 | + gender, |
| 31 | + SUM(number) AS total |
| 32 | + FROM |
| 33 | + bigquery-public-data.usa_names.usa_1910_2013 |
| 34 | + GROUP BY |
| 35 | + name, gender |
| 36 | + ORDER BY |
| 37 | + total DESC |
| 38 | + LIMIT 10 |
| 39 | + """ |
| 40 | + # Run the query. The returned `rows` iterator can return information about |
| 41 | + # how the query was executed as well as the result data. |
| 42 | + rows = client.query_and_wait(query) |
| 43 | + |
| 44 | + if rows.job_id is not None: |
| 45 | + print("Query was run with job state. Job ID: {}".format(rows.job_id)) |
| 46 | + else: |
| 47 | + print("Query was run in short mode. Query ID: {}".format(rows.query_id)) |
| 48 | + |
| 49 | + print("The query data:") |
| 50 | + for row in rows: |
| 51 | + # Row values can be accessed by field name or index. |
| 52 | + print("name={}, gender={}, total={}".format(row[0], row[1], row["total"])) |
| 53 | + # [END bigquery_query_shortquery] |
0 commit comments