|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package pubsub; |
| 18 | + |
| 19 | +// [START pubsub_create_topic_with_schema_revisions] |
| 20 | + |
| 21 | +import com.google.api.gax.rpc.AlreadyExistsException; |
| 22 | +import com.google.cloud.pubsub.v1.TopicAdminClient; |
| 23 | +import com.google.pubsub.v1.Encoding; |
| 24 | +import com.google.pubsub.v1.SchemaName; |
| 25 | +import com.google.pubsub.v1.SchemaSettings; |
| 26 | +import com.google.pubsub.v1.Topic; |
| 27 | +import com.google.pubsub.v1.TopicName; |
| 28 | +import java.io.IOException; |
| 29 | + |
| 30 | +public class CreateTopicWithSchemaRevisionsExample { |
| 31 | + |
| 32 | + public static void main(String... args) throws Exception { |
| 33 | + // TODO(developer): Replace these variables before running the sample. |
| 34 | + String projectId = "your-project-id"; |
| 35 | + String topicId = "your-topic-id"; |
| 36 | + // Use an existing schema. |
| 37 | + String schemaId = "your-schema-id"; |
| 38 | + // Choose either BINARY or JSON message serialization in this topic. |
| 39 | + Encoding encoding = Encoding.BINARY; |
| 40 | + // Set the minimum and maximum revsion ID |
| 41 | + String firstRevisionId = "your-revision-id"; |
| 42 | + String lastRevisionId = "your-revision-id"; |
| 43 | + |
| 44 | + createTopicWithSchemaRevisionsExample( |
| 45 | + projectId, topicId, schemaId, firstRevisionId, lastRevisionId, encoding); |
| 46 | + } |
| 47 | + |
| 48 | + public static void createTopicWithSchemaRevisionsExample( |
| 49 | + String projectId, |
| 50 | + String topicId, |
| 51 | + String schemaId, |
| 52 | + String firstRevisionid, |
| 53 | + String lastRevisionId, |
| 54 | + Encoding encoding) |
| 55 | + throws IOException { |
| 56 | + TopicName topicName = TopicName.of(projectId, topicId); |
| 57 | + SchemaName schemaName = SchemaName.of(projectId, schemaId); |
| 58 | + |
| 59 | + SchemaSettings schemaSettings = |
| 60 | + SchemaSettings.newBuilder() |
| 61 | + .setSchema(schemaName.toString()) |
| 62 | + .setFirstRevisionId(firstRevisionid) |
| 63 | + .setLastRevisionId(lastRevisionId) |
| 64 | + .setEncoding(encoding) |
| 65 | + .build(); |
| 66 | + |
| 67 | + try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { |
| 68 | + |
| 69 | + Topic topic = |
| 70 | + topicAdminClient.createTopic( |
| 71 | + Topic.newBuilder() |
| 72 | + .setName(topicName.toString()) |
| 73 | + .setSchemaSettings(schemaSettings) |
| 74 | + .build()); |
| 75 | + |
| 76 | + System.out.println("Created topic with schema: " + topic.getName()); |
| 77 | + } catch (AlreadyExistsException e) { |
| 78 | + System.out.println(schemaName + "already exists."); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | +// [END pubsub_create_topic_with_schema_revisions] |
0 commit comments