Skip to content

Commit f6e728e

Browse files
sarafordAce Nassriaverikitsch
authored
feat(functions/v2): add bigquery sample (GoogleCloudPlatform#2648)
* created new bigquery v2 sample * Lint * Add region tags * Specify region tag in test * Update sample * fix pacakge.json * lint Co-authored-by: Ace Nassri <anassri@google.com> Co-authored-by: Averi Kitsch <akitsch@google.com>
1 parent fb3cef2 commit f6e728e

5 files changed

Lines changed: 180 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
name: functions-v2-hellobigquery
2+
on:
3+
push:
4+
branches:
5+
- main
6+
paths:
7+
- 'functions/v2/helloBigQuery/**'
8+
pull_request:
9+
paths:
10+
- 'functions/v2/helloBigQuery/**'
11+
pull_request_target:
12+
types: [labeled]
13+
schedule:
14+
- cron: '0 2 * * *'
15+
jobs:
16+
test:
17+
if: ${{ github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' }}
18+
runs-on: ubuntu-latest
19+
permissions:
20+
contents: 'write'
21+
pull-requests: 'write'
22+
id-token: 'write'
23+
steps:
24+
- uses: actions/checkout@v3
25+
with:
26+
ref: ${{github.event.pull_request.head.ref}}
27+
repository: ${{github.event.pull_request.head.repo.full_name}}
28+
- uses: google-github-actions/auth@v0.7.1
29+
with:
30+
workload_identity_provider: 'projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider'
31+
service_account: 'kokoro-system-test@long-door-651.iam.gserviceaccount.com'
32+
create_credentials_file: 'true'
33+
access_token_lifetime: 600s
34+
- uses: actions/setup-node@v3
35+
with:
36+
node-version: 14
37+
- run: npm install
38+
working-directory: functions/v2/helloBigQuery
39+
- run: npm test
40+
working-directory: functions/v2/helloBigQuery
41+
env:
42+
MOCHA_REPORTER_SUITENAME: functions_v2_hellobigquery
43+
MOCHA_REPORTER_OUTPUT: functions_v2_hellobigquery_sponge_log.xml
44+
MOCHA_REPORTER: xunit
45+
- if: ${{ github.event.action == 'labeled' && github.event.label.name == 'actions:force-run' }}
46+
uses: actions/github-script@v6
47+
with:
48+
github-token: ${{ secrets.GITHUB_TOKEN }}
49+
script: |
50+
try {
51+
await github.rest.issues.removeLabel({
52+
name: 'actions:force-run',
53+
owner: 'GoogleCloudPlatform',
54+
repo: 'nodejs-docs-samples',
55+
issue_number: context.payload.pull_request.number
56+
});
57+
} catch (e) {
58+
if (!e.message.includes('Label does not exist')) {
59+
throw e;
60+
}
61+
}
62+
- if: ${{ github.event_name == 'schedule'}}
63+
run: |
64+
curl https://github.com/googleapis/repo-automation-bots/releases/download/flakybot-1.1.0/flakybot -o flakybot -s -L
65+
chmod +x ./flakybot
66+
./flakybot --repo GoogleCloudPlatform/nodejs-docs-samples --commit_hash ${{github.sha}} --build_url https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/
2+
test/
3+
package-lock.json
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2022 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+
// http://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+
// [START functions_hello_bigquery]
16+
// Import the Google Cloud client library
17+
const {BigQuery} = require('@google-cloud/bigquery');
18+
const bigquery = new BigQuery();
19+
20+
const functions = require('@google-cloud/functions-framework');
21+
22+
/**
23+
* HTTP Cloud Function that returns BigQuery query results
24+
*
25+
* @param {Object} req Cloud Function request context.
26+
* @param {Object} res Cloud Function response context.
27+
*/
28+
functions.http('helloBigQuery', async (req, res) => {
29+
// Define the SQL query
30+
// Queries the public Shakespeare dataset using named query parameter
31+
const sqlQuery = `
32+
SELECT word, word_count
33+
FROM \`bigquery-public-data.samples.shakespeare\`
34+
WHERE corpus = @corpus
35+
AND word_count >= @min_word_count
36+
ORDER BY word_count DESC`;
37+
38+
const options = {
39+
query: sqlQuery,
40+
// Location must match that of the dataset(s) referenced in the query.
41+
location: 'US',
42+
params: {corpus: 'romeoandjuliet', min_word_count: 400},
43+
};
44+
45+
// Execute the query
46+
try {
47+
const [rows] = await bigquery.query(options);
48+
// Send the results
49+
res.status(200).send(rows);
50+
} catch (err) {
51+
console.error(err);
52+
res.status(500).send(`Error querying BigQuery: ${err}`);
53+
}
54+
});
55+
// [END functions_hello_bigquery]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "nodejs-docs-samples-functions-v2-hello-big-query",
3+
"version": "0.0.1",
4+
"private": true,
5+
"license": "Apache-2.0",
6+
"author": "Google LLC",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
10+
},
11+
"engines": {
12+
"node": ">=12.0.0"
13+
},
14+
"scripts": {
15+
"test": "mocha test/*.test.js --timeout=20000 --exit"
16+
},
17+
"dependencies": {
18+
"@google-cloud/bigquery": "^5.12.0",
19+
"@google-cloud/functions-framework": "^3.1.0"
20+
},
21+
"devDependencies": {
22+
"mocha": "^9.1.3",
23+
"supertest": "^6.0.0"
24+
}
25+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2022 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+
// http://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+
const supertest = require('supertest');
16+
const {getTestServer} = require('@google-cloud/functions-framework/testing');
17+
require('../index');
18+
19+
describe('functions_hello_bigquery', () => {
20+
it('helloBigQuery: returns results', async () => {
21+
const results = [
22+
{word: 'the', word_count: 614},
23+
{word: 'I', word_count: 577},
24+
{word: 'and', word_count: 490},
25+
{word: 'to', word_count: 486},
26+
{word: 'a', word_count: 407},
27+
];
28+
const server = getTestServer('helloBigQuery');
29+
await supertest(server).get('/').expect(200).expect(results);
30+
});
31+
});

0 commit comments

Comments
 (0)