Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

samples: create BigQuery subscription #1594

Merged
merged 5 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-pubsub/tree
| Sample | Source Code | Try it |
| --------------------------- | --------------------------------- | ------ |
| Create an Avro based Schema | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createAvroSchema.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/createAvroSchema.js,samples/README.md) |
| Create BigQuery Subscription | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createBigQuerySubscription.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/createBigQuerySubscription.js,samples/README.md) |
| Create a Proto based Schema | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createProtoSchema.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/createProtoSchema.js,samples/README.md) |
| Create Push Subscription | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createPushSubscription.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/createPushSubscription.js,samples/README.md) |
| Create Subscription | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createSubscription.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/createSubscription.js,samples/README.md) |
Expand Down
20 changes: 20 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ guides.
* [Before you begin](#before-you-begin)
* [Samples](#samples)
* [Create an Avro based Schema](#create-an-avro-based-schema)
* [Create BigQuery Subscription](#create-bigquery-subscription)
* [Create a Proto based Schema](#create-a-proto-based-schema)
* [Create Push Subscription](#create-push-subscription)
* [Create Subscription](#create-subscription)
Expand Down Expand Up @@ -103,6 +104,25 @@ __Usage:__



### Create BigQuery Subscription

Creates a new BigQuery subscription.

View the [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createBigQuerySubscription.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/createBigQuerySubscription.js,samples/README.md)

__Usage:__


`node createBigQuerySubscription.js <topic-name-or-id> <subscription-name-or-id> <bigquery-table-id>`


-----




### Create a Proto based Schema

Creates a new schema definition on a project, using Protos
Expand Down
67 changes: 67 additions & 0 deletions samples/createBigQuerySubscription.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2019-2022 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.

/**
* This application demonstrates how to perform basic operations on
* subscriptions with the Google Cloud Pub/Sub API.
*
* For more information, see the README.md under /pubsub and the documentation
* at https://cloud.google.com/pubsub/docs.
*/

'use strict';

// sample-metadata:
// title: Create BigQuery Subscription
// description: Creates a new BigQuery subscription.
// usage: node createBigQuerySubscription.js <topic-name-or-id> <subscription-name-or-id> <bigquery-table-id>

function main(
topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID',
subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID',
bigqueryTableId = 'YOUR_TABLE_ID'
) {
// [START pubsub_create_bigquery_subscription]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID';
// const subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID';
// const bigqueryTableId = 'YOUR_TABLE_ID';

// Imports the Google Cloud client library
const {PubSub} = require('@google-cloud/pubsub');

// Creates a client; cache this for further use
const pubSubClient = new PubSub();

async function createBigQuerySubscription() {
const options = {
bigqueryConfig: {
table: bigqueryTableId,
writeMetadata: true,
},
};

await pubSubClient
.topic(topicNameOrId)
.createSubscription(subscriptionNameOrId, options);
console.log(`Subscription ${subscriptionNameOrId} created.`);
}

createBigQuerySubscription().catch(console.error);
// [END pubsub_create_bigquery_subscription]
}

main(...process.argv.slice(2));
1 change: 1 addition & 0 deletions samples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"protobufjs": "^6.11.2"
},
"devDependencies": {
"@google-cloud/bigquery": "^6.0.0",
"@types/chai": "^4.2.16",
"@types/rimraf": "^3.0.0",
"chai": "^4.2.0",
Expand Down
67 changes: 67 additions & 0 deletions samples/system-test/subscriptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import {BigQuery} from '@google-cloud/bigquery';
import {
CreateSubscriptionOptions,
PubSub,
Expand Down Expand Up @@ -63,6 +64,50 @@ describe('subscriptions', () => {
}
}

function reserveBigQueryName(testName: string): string {
return resources.generateBigQueryName(testName);
}

function fullBigQueryTableName(datasetId: string, tableId: string): string {
return `${projectId}.${datasetId}.${tableId}`;
}

async function createBigQueryTable(datasetId: string, tableId: string) {
const bigquery = new BigQuery({
projectId: projectId,
});

const datasetOptions = {
location: 'US',
};
await bigquery.createDataset(datasetId, datasetOptions);

const schema = [
{name: 'data', type: 'STRING'},
{name: 'message_id', type: 'STRING'},
{name: 'attributes', type: 'STRING'},
{name: 'subscription_name', type: 'STRING'},
{name: 'publish_time', type: 'TIMESTAMP'},
];
const tableOptions = {
location: 'US',
schema: schema,
};
await bigquery.dataset(datasetId).createTable(tableId, tableOptions);
}

async function cleanBigQueryDataset(datasetId: string) {
const bigquery = new BigQuery({
projectId: projectId,
});

const deleteOptions = {
force: true,
};

await bigquery.dataset(datasetId).delete(deleteOptions);
}

async function cleanSubs() {
const [subscriptions] = await pubsub.getSubscriptions();
await Promise.all(
Expand Down Expand Up @@ -126,6 +171,28 @@ describe('subscriptions', () => {
assert(subscriptions.some(s => s.name === fullSubName(subName)));
});

it('should create a BigQuery subscription', async () => {
const testId = 'bigquery_sub';
const topic = await createTopic(testId);
const subName = reserveSub(testId);
const datasetId = reserveBigQueryName(testId);
const tableId = reserveBigQueryName(testId);
const fullTableName = fullBigQueryTableName(datasetId, tableId);

await createBigQueryTable(datasetId, tableId);

const output = execSync(
`${commandFor('createBigQuerySubscription')} ${
topic.name
} ${subName} ${fullTableName}`
);
assert.include(output, `Subscription ${subName} created.`);
const [subscriptions] = await pubsub.topic(topic.name).getSubscriptions();
assert(subscriptions.some(s => s.name === fullSubName(subName)));

await cleanBigQueryDataset(datasetId);
});

it('should modify the config of an existing push subscription', async () => {
const testId = 'mod_push';
const topic = await createTopic(testId);
Expand Down
10 changes: 10 additions & 0 deletions samples/system-test/testResources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ export class TestResources {
return [this.getPrefix(testId), this.tokenMaker.uuid()].join('-');
}

/**
* Generates a unique resource name for one run of a test within
* a test suite for BigQuery resources.
*/
generateBigQueryName(testId: string): string {
return [normalizeId(this.getPrefix(testId)), this.tokenMaker.uuid()].join(
'_'
);
}

/*!
* Given a list of resource names (and a test ID), this will return
* a list of all resources that should be deleted to clean up for
Expand Down