diff --git a/README.md b/README.md index 12f2679d..ecf5cec8 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-bigquery/tr | Query Params Positional Types | [source code](https://github.com/googleapis/nodejs-bigquery/blob/main/samples/queryParamsPositionalTypes.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-bigquery&page=editor&open_in_editor=samples/queryParamsPositionalTypes.js,samples/README.md) | | Query Params Structs | [source code](https://github.com/googleapis/nodejs-bigquery/blob/main/samples/queryParamsStructs.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-bigquery&page=editor&open_in_editor=samples/queryParamsStructs.js,samples/README.md) | | Query Params Timestamps | [source code](https://github.com/googleapis/nodejs-bigquery/blob/main/samples/queryParamsTimestamps.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-bigquery&page=editor&open_in_editor=samples/queryParamsTimestamps.js,samples/README.md) | +| Query Short Mode | [source code](https://github.com/googleapis/nodejs-bigquery/blob/main/samples/queryShortMode.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-bigquery&page=editor&open_in_editor=samples/queryShortMode.js,samples/README.md) | | Query Stack Overflow | [source code](https://github.com/googleapis/nodejs-bigquery/blob/main/samples/queryStackOverflow.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-bigquery&page=editor&open_in_editor=samples/queryStackOverflow.js,samples/README.md) | | Quickstart | [source code](https://github.com/googleapis/nodejs-bigquery/blob/main/samples/quickstart.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-bigquery&page=editor&open_in_editor=samples/quickstart.js,samples/README.md) | | Relax Column | [source code](https://github.com/googleapis/nodejs-bigquery/blob/main/samples/relaxColumn.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-bigquery&page=editor&open_in_editor=samples/relaxColumn.js,samples/README.md) | diff --git a/samples/README.md b/samples/README.md index 71ad7f52..57e38fa7 100644 --- a/samples/README.md +++ b/samples/README.md @@ -96,6 +96,7 @@ * [Query Params Positional Types](#query-params-positional-types) * [Query Params Structs](#query-params-structs) * [Query Params Timestamps](#query-params-timestamps) + * [Query Short Mode](#query-short-mode) * [Query Stack Overflow](#query-stack-overflow) * [Quickstart](#quickstart) * [Relax Column](#relax-column) @@ -1577,6 +1578,23 @@ __Usage:__ +### Query Short Mode + +View the [source code](https://github.com/googleapis/nodejs-bigquery/blob/main/samples/queryShortMode.js). + +[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-bigquery&page=editor&open_in_editor=samples/queryShortMode.js,samples/README.md) + +__Usage:__ + + +`node samples/queryShortMode.js` + + +----- + + + + ### Query Stack Overflow View the [source code](https://github.com/googleapis/nodejs-bigquery/blob/main/samples/queryStackOverflow.js). diff --git a/samples/queryShortMode.js b/samples/queryShortMode.js new file mode 100644 index 00000000..a69d4b66 --- /dev/null +++ b/samples/queryShortMode.js @@ -0,0 +1,56 @@ +// Copyright 2024 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 +// +// http://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. + +'use strict'; + +function main() { + // [START bigquery_query_shortmode] + // Demonstrates issuing a query that may be run in short query mode. + // To enable the short query mode preview feature, the QUERY_PREVIEW_ENABLED + // environmental variable should be set to `TRUE`. + + // Import the Google Cloud client library + const {BigQuery} = require('@google-cloud/bigquery'); + const bigquery = new BigQuery(); + + async function queryShortMode() { + // SQL query to run. + + const sqlQuery = ` + SELECT name, gender, SUM(number) AS total + FROM bigquery-public-data.usa_names.usa_1910_2013 + GROUP BY name, gender + ORDER BY total DESC + LIMIT 10`; + + // Run the query + const [rows, , res] = await bigquery.query(sqlQuery); + + if (!res.jobReference) { + console.log(`Query was run in short mode. Query ID: ${res.queryId}`); + } else { + const jobRef = res.jobReference; + const qualifiedId = `${jobRef.projectId}.${jobRef.location}.${jobRef.jobId}`; + console.log( + `Query was run with job state. Job ID: ${qualifiedId}, Query ID: ${res.queryId}` + ); + } + // Print the results + console.log('Rows:'); + rows.forEach(row => console.log(row)); + } + // [END bigquery_query_shortmode] + queryShortMode(); +} +main(...process.argv.slice(2)); diff --git a/samples/test/queries.test.js b/samples/test/queries.test.js index 00c322a5..16ca109c 100644 --- a/samples/test/queries.test.js +++ b/samples/test/queries.test.js @@ -70,6 +70,12 @@ describe('Queries', () => { assert.match(output, /name/); }); + it('should run a query in short mode', async () => { + const output = execSync('node queryShortMode.js'); + assert.match(output, /Rows:/); + assert.match(output, /name/); + }); + it('should run a query as a dry run', async () => { const output = execSync('node queryDryRun.js'); assert.match(output, /Status:/);