Skip to content

Commit

Permalink
docs: add short mode query sample (#1395)
Browse files Browse the repository at this point in the history
* docs: add short mode query sample

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
alvarowolfx and gcf-owl-bot[bot] authored Jul 31, 2024
1 parent 3fd28b8 commit 8cd9628
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
18 changes: 18 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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).
Expand Down
56 changes: 56 additions & 0 deletions samples/queryShortMode.js
Original file line number Diff line number Diff line change
@@ -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));
6 changes: 6 additions & 0 deletions samples/test/queries.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:/);
Expand Down

0 comments on commit 8cd9628

Please sign in to comment.