Skip to content

Instantly share code, notes, and snippets.

@stephenplusplus
Last active July 9, 2019 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenplusplus/a4e158c3d625f0cd08c0af77937e79c0 to your computer and use it in GitHub Desktop.
Save stephenplusplus/a4e158c3d625f0cd08c0af77937e79c0 to your computer and use it in GitHub Desktop.

Source Issue

How to Run

  • Authorize using Application Default Credentials, or modify the code in test.js to include a configuration object.
  • npm test
{
"name": "gissue-506",
"version": "1.0.0",
"scripts": {
"test": "node ./test.js"
},
"dependencies": {
"@google-cloud/bigtable": "^2.0.1",
"p-queue": "^6.0.2",
"uuid": "^3.0.1"
}
}
'use strict'
const Bigtable = require('@google-cloud/bigtable').Bigtable;
const Q = require('p-queue').default;
const uuid = require('uuid');
const PREFIX = 'bttest-';
const bigtable = new Bigtable();
const INSTANCE = bigtable.instance(generateId('instance'));
const TABLE = INSTANCE.table(generateId('table'));
(async () => {
await createTestResources();
const rows = new Array(100 * 1000).fill({
key: generateId('key'),
data: {
follows: {
jadams: 1,
},
},
});
await TABLE.insert(rows);
console.log(Date.now(), 'Starting stream.');
const stream = TABLE.createReadStream();
stream
.on('error', console.error)
.on('data', () => {
console.log(Date.now(), 'Data event. stream.end()');
stream.end();
})
.on('end', () => {
console.log(Date.now(), 'End event received.');
});
await removeTestResources();
})();
// Utility functions...
function generateId(resourceType) {
return PREFIX + resourceType + '-' + uuid.v1().substr(0, 8);
}
async function createTestResources() {
const [, operation] = await INSTANCE.create({
clusters: [
{
id: generateId('cluster'),
location: 'us-central1-c',
nodes: 1,
},
],
});
await operation.promise();
await TABLE.create({families: ['follows']});
}
async function removeTestResources() {
const [instances] = await bigtable.getInstances();
const testInstances = instances.filter(i => i.id.match(PREFIX));
const q = new Q({concurrency: 5});
await Promise.all(
testInstances.map(instance => {
q.add(() => instance.delete());
})
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment