|
| 1 | +// Copyright 2024 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 | +// https://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 | +// This is a generated sample, using the typeless sample bot. Please |
| 16 | +// look for the source TypeScript sample (.ts) for modifications. |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +/** |
| 20 | + * This sample demonstrates how to perform basic operations on topics with |
| 21 | + * the Google Cloud Pub/Sub API. |
| 22 | + * |
| 23 | + * For more information, see the README.md under /pubsub and the documentation |
| 24 | + * at https://cloud.google.com/pubsub/docs. |
| 25 | + */ |
| 26 | + |
| 27 | +// sample-metadata: |
| 28 | +// title: Create Topic With Cloud Storage Ingestion |
| 29 | +// description: Creates a new topic, with Cloud Storage ingestion enabled. |
| 30 | +// usage: node createTopicWithCloudStorageIngestion.js <topic-name> <bucket> <input-format> <text-delimiter> <match-glob> <minimum-object-creation-time> |
| 31 | + |
| 32 | +// [START pubsub_create_topic_with_cloud_storage_ingestion] |
| 33 | +/** |
| 34 | + * TODO(developer): Uncomment these variables before running the sample. |
| 35 | + */ |
| 36 | +// const topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID'; |
| 37 | +// const bucket = 'YOUR_BUCKET_NAME'; |
| 38 | +// const inputFormat = 'text'; |
| 39 | +// const textDelimiter = '\n'; |
| 40 | +// const matchGlob = '**.txt'; |
| 41 | +// const minimumObjectCreateTime = 'YYYY-MM-DDThh:mm:ssZ; |
| 42 | + |
| 43 | +// Imports the Google Cloud client library |
| 44 | +const {PubSub} = require('@google-cloud/pubsub'); |
| 45 | + |
| 46 | +// Creates a client; cache this for further use |
| 47 | +const pubSubClient = new PubSub(); |
| 48 | + |
| 49 | +async function createTopicWithCloudStorageIngestion( |
| 50 | + topicNameOrId, |
| 51 | + bucket, |
| 52 | + inputFormat, |
| 53 | + textDelimiter, |
| 54 | + matchGlob, |
| 55 | + minimumObjectCreateTime |
| 56 | +) { |
| 57 | + const minimumDate = Date.parse(minimumObjectCreateTime); |
| 58 | + const topicMetadata = { |
| 59 | + name: topicNameOrId, |
| 60 | + ingestionDataSourceSettings: { |
| 61 | + cloudStorage: { |
| 62 | + bucket, |
| 63 | + minimumObjectCreateTime: { |
| 64 | + seconds: minimumDate / 1000, |
| 65 | + nanos: (minimumDate % 1000) * 1000, |
| 66 | + }, |
| 67 | + matchGlob, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }; |
| 71 | + |
| 72 | + // Make a format appropriately. |
| 73 | + switch (inputFormat) { |
| 74 | + case 'text': |
| 75 | + topicMetadata.ingestionDataSourceSettings.cloudStorage.textFormat = { |
| 76 | + delimiter: textDelimiter, |
| 77 | + }; |
| 78 | + break; |
| 79 | + case 'avro': |
| 80 | + topicMetadata.ingestionDataSourceSettings.cloudStorage.avroFormat = {}; |
| 81 | + break; |
| 82 | + case 'pubsub_avro': |
| 83 | + topicMetadata.ingestionDataSourceSettings.cloudStorage.pubsubAvroFormat = |
| 84 | + {}; |
| 85 | + break; |
| 86 | + default: |
| 87 | + console.error('inputFormat must be in ("text", "avro", "pubsub_avro")'); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + // Creates a new topic with Cloud Storage ingestion. |
| 92 | + await pubSubClient.createTopic(topicMetadata); |
| 93 | + console.log(`Topic ${topicNameOrId} created with Cloud Storage ingestion.`); |
| 94 | +} |
| 95 | +// [END pubsub_create_topic_with_cloud_storage_ingestion] |
| 96 | + |
| 97 | +function main( |
| 98 | + topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID', |
| 99 | + bucket = 'YOUR_BUCKET_NAME', |
| 100 | + inputFormat = 'text', |
| 101 | + textDelimiter = '\n', |
| 102 | + matchGlob = '**.txt', |
| 103 | + minimumObjectCreateTime = 'YYYY-MM-DDThh:mm:ssZ' |
| 104 | +) { |
| 105 | + createTopicWithCloudStorageIngestion( |
| 106 | + topicNameOrId, |
| 107 | + bucket, |
| 108 | + inputFormat, |
| 109 | + textDelimiter, |
| 110 | + matchGlob, |
| 111 | + minimumObjectCreateTime |
| 112 | + ).catch(err => { |
| 113 | + console.error(err.message); |
| 114 | + process.exitCode = 1; |
| 115 | + }); |
| 116 | +} |
| 117 | + |
| 118 | +main(...process.argv.slice(2)); |
0 commit comments