Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: Cloud functions example for Bigtable #728

Merged
merged 9 commits into from
May 11, 2020
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions samples/functions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2020 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';

// [START bigtable_functions_quickstart]
// Imports the Google Cloud client library
const {Bigtable} = require('@google-cloud/bigtable');

// Instantiates a client
const bigtable = new Bigtable();

exports.readRows = async (req, res) => {
// Gets a reference to a Cloud Bigtable instance and database
const instance = bigtable.instance(req.body.instanceId);
const table = instance.table(req.body.tableId);

// Execute the query
try {
const prefix = 'phone#';
const rows = [];
await table
.createReadStream({
prefix,
})
.on('error', err => {
res.send(`Error querying Bigtable: ${err}`);
res.status(500).end();
})
.on('data', row => {
rows.push(
`rowkey: ${row.id}, ` +
`os_build: ${row.data['stats_summary']['os_build'][0].value}\n`
);
})
.on('end', () => {
rows.forEach(r => res.write(r));
res.status(200).end();
});
} catch (err) {
res.send(`Error querying Bigtable: ${err}`);
res.status(500).end();
}
};

// [END bigtable_functions_quickstart]
18 changes: 18 additions & 0 deletions samples/functions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@google-cloud/bigtable-samples",
"private": true,
"license": "Apache-2.0",
"author": "Google Inc.",
"repository": "googleapis/nodejs-bigtable",
"engines": {
"node": ">=8.0.0"
},
"scripts": {
"test": "mocha test/*.test.js --timeout=20000",
"start": "functions-framework --target=get"
},
"dependencies": {
"@google-cloud/bigtable": "^2.3.2",
"@google-cloud/functions-framework": "^1.5.1"
}
}
6 changes: 5 additions & 1 deletion samples/package.json
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
"repository": "googleapis/nodejs-bigtable",
"files": [
"*.js",
"functions/*.js",
"hello-world/*.js",
"document-snippets/*.js"
],
@@ -18,9 +19,12 @@
"yargs": "^15.0.0"
},
"devDependencies": {
"@google-cloud/functions-framework": "^1.5.1",
"chai": "^4.2.0",
"mocha": "^7.0.0",
"snap-shot-it": "^7.9.1"
"snap-shot-it": "^7.9.1",
"request": "^2.88.0",
"requestretry": "^4.1.0"
},
"scripts": {
"test": "mocha --timeout=60000",
17 changes: 9 additions & 8 deletions samples/readSnippets.js
Original file line number Diff line number Diff line change
@@ -30,8 +30,9 @@ async function main(
const bigtable = new Bigtable();

/**
* TODO(developer): Uncomment these variables before running the sample.
* TODO(developer): Uncomment these variables before running the sample
*/

// const instanceId = 'YOUR_INSTANCE_ID';
// const tableId = 'YOUR_TABLE_ID';
const instance = bigtable.instance(instanceId);
@@ -44,8 +45,8 @@ async function main(
// [END bigtable_reads_prefix]
// [END bigtable_reads_filter]
switch (readType) {
// [START bigtable_reads_row]
case 'readRow': {
// [START bigtable_reads_row]
const rowkey = 'phone#4c410523#20190501';

const [row] = await table.row(rowkey).get();
@@ -54,8 +55,8 @@ async function main(
break;
}

// [START bigtable_reads_row_partial]
case 'readRowPartial': {
// [START bigtable_reads_row_partial]
const COLUMN_FAMILY = 'stats_summary';
const COLUMN_QUALIFIER = 'os_build';
const rowkey = 'phone#4c410523#20190501';
@@ -69,17 +70,17 @@ async function main(
break;
}

// [START bigtable_reads_rows]
case 'readRows': {
// [START bigtable_reads_rows]
const rowKeys = ['phone#4c410523#20190501', 'phone#4c410523#20190502'];
const [rows] = await table.getRows({keys: rowKeys});
rows.forEach(row => printRow(row.id, row.data));
// [END bigtable_reads_rows]
break;
}

// [START bigtable_reads_row_range]
case 'readRowRange': {
// [START bigtable_reads_row_range]
const start = 'phone#4c410523#20190501';
const end = 'phone#4c410523#201906201';

@@ -100,8 +101,8 @@ async function main(
break;
}

// [START bigtable_reads_row_ranges]
case 'readRowRanges': {
// [START bigtable_reads_row_ranges]
await table
.createReadStream({
ranges: [
@@ -127,8 +128,8 @@ async function main(
break;
}

// [START bigtable_reads_prefix]
case 'readPrefix': {
// [START bigtable_reads_prefix]
const prefix = 'phone#';

await table
@@ -147,8 +148,8 @@ async function main(
break;
}

// [START bigtable_reads_filter]
case 'readFilter': {
// [START bigtable_reads_filter]
const filter = {
value: /PQ2A.*$/,
};
185 changes: 185 additions & 0 deletions samples/test/functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
// Copyright 2020 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';

const assert = require('assert');
const {execSync} = require('child_process');
const path = require('path');
const requestRetry = require('requestretry');
const uuid = require('uuid');
const {Bigtable} = require('@google-cloud/bigtable');
const {describe, it, before, after} = require('mocha');

const PORT = 9010;
const BASE_URL = `http://localhost:${PORT}`;
const cwd = path.join(__dirname, '..');

const INSTANCE_ID = `nodejs-bigtable-samples-keepme`;
const TABLE_ID = `mobile-time-series-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules

describe('functions', async () => {
const bigtable = Bigtable();
const instance = bigtable.instance(INSTANCE_ID);
let table;
const TIMESTAMP = new Date(2019, 5, 1);
TIMESTAMP.setUTCHours(0);

let ffProc;

before(async () => {
table = instance.table(TABLE_ID);

await table.create().catch(console.error);
await table.createFamily('stats_summary').catch(console.error);

const rowsToInsert = [
{
key: 'phone#4c410523#20190501',
data: {
stats_summary: {
connected_cell: {
value: 1,
timestamp: TIMESTAMP,
},
connected_wifi: {
value: 1,
timestamp: TIMESTAMP,
},
os_build: {
value: 'PQ2A.190405.003',
timestamp: TIMESTAMP,
},
},
},
},
{
key: 'phone#4c410523#20190502',
data: {
stats_summary: {
connected_cell: {
value: 1,
timestamp: TIMESTAMP,
},
connected_wifi: {
value: 1,
timestamp: TIMESTAMP,
},
os_build: {
value: 'PQ2A.190405.004',
timestamp: TIMESTAMP,
},
},
},
},
{
key: 'phone#4c410523#20190505',
data: {
stats_summary: {
connected_cell: {
value: 0,
timestamp: TIMESTAMP,
},
connected_wifi: {
value: 1,
timestamp: TIMESTAMP,
},
os_build: {
value: 'PQ2A.190406.000',
timestamp: TIMESTAMP,
},
},
},
},
{
key: 'phone#5c10102#20190501',
data: {
stats_summary: {
connected_cell: {
value: 1,
timestamp: TIMESTAMP,
},
connected_wifi: {
value: 1,
timestamp: TIMESTAMP,
},
os_build: {
value: 'PQ2A.190401.002',
timestamp: TIMESTAMP,
},
},
},
},
{
key: 'phone#5c10102#20190502',
data: {
stats_summary: {
connected_cell: {
value: 1,
timestamp: TIMESTAMP,
},
connected_wifi: {
value: 0,
timestamp: TIMESTAMP,
},
os_build: {
value: 'PQ2A.190406.000',
timestamp: TIMESTAMP,
},
},
},
},
];

await table.insert(rowsToInsert).catch(console.error);

// Run the functions-framework instance to host functions locally
// exec's 'timeout' param won't kill children of "shim" /bin/sh process
// Workaround: include "& sleep <TIMEOUT>; kill $!" in executed command
ffProc = execSync(
`functions-framework --target=readRows --signature-type=http --port ${PORT} & sleep 2; kill $!`,
{shell: true, cwd}
);
});

after(async () => {
// Wait for the functions framework to stop
await ffProc;
await table.delete().catch(console.error);
});

it('should read one row', async () => {
const response = await requestRetry({
url: `${BASE_URL}/readRows`,
method: 'GET',
body: {
instanceId: INSTANCE_ID,
tableId: TABLE_ID,
},
retryDelay: 200,
json: true,
});

assert.strictEqual(response.statusCode, 200);
assert.strictEqual(
response.body,
`rowkey: phone#4c410523#20190501, os_build: PQ2A.190405.003
rowkey: phone#4c410523#20190502, os_build: PQ2A.190405.004
rowkey: phone#4c410523#20190505, os_build: PQ2A.190406.000
rowkey: phone#5c10102#20190501, os_build: PQ2A.190401.002
rowkey: phone#5c10102#20190502, os_build: PQ2A.190406.000
`
);
});
});