diff --git a/README.md b/README.md index 734989c63..f77054073 100644 --- a/README.md +++ b/README.md @@ -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) | diff --git a/samples/README.md b/samples/README.md index 3f9cfb58a..30dd790c9 100644 --- a/samples/README.md +++ b/samples/README.md @@ -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) @@ -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 ` + + +----- + + + + ### Create a Proto based Schema Creates a new schema definition on a project, using Protos diff --git a/samples/createBigQuerySubscription.js b/samples/createBigQuerySubscription.js new file mode 100644 index 000000000..ec51f3d6b --- /dev/null +++ b/samples/createBigQuerySubscription.js @@ -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 + +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)); diff --git a/samples/package.json b/samples/package.json index fa0da3ae8..b29207249 100644 --- a/samples/package.json +++ b/samples/package.json @@ -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", diff --git a/samples/system-test/subscriptions.test.ts b/samples/system-test/subscriptions.test.ts index 6b20507bf..5861c6dd2 100644 --- a/samples/system-test/subscriptions.test.ts +++ b/samples/system-test/subscriptions.test.ts @@ -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, @@ -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( @@ -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); diff --git a/samples/system-test/testResources.ts b/samples/system-test/testResources.ts index 3592592e9..56342fcac 100644 --- a/samples/system-test/testResources.ts +++ b/samples/system-test/testResources.ts @@ -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