Search in sources :

Example 1 with SchemaSettings

use of com.google.pubsub.v1.SchemaSettings in project java-pubsub by googleapis.

the class CreateTopicWithSchemaExample method createTopicWithSchemaExample.

public static void createTopicWithSchemaExample(String projectId, String topicId, String schemaId, Encoding encoding) throws IOException {
    TopicName topicName = TopicName.of(projectId, topicId);
    SchemaName schemaName = SchemaName.of(projectId, schemaId);
    SchemaSettings schemaSettings = SchemaSettings.newBuilder().setSchema(schemaName.toString()).setEncoding(encoding).build();
    try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
        Topic topic = topicAdminClient.createTopic(Topic.newBuilder().setName(topicName.toString()).setSchemaSettings(schemaSettings).build());
        System.out.println("Created topic with schema: " + topic.getName());
    } catch (AlreadyExistsException e) {
        System.out.println(schemaName + "already exists.");
    }
}
Also used : AlreadyExistsException(com.google.api.gax.rpc.AlreadyExistsException) TopicAdminClient(com.google.cloud.pubsub.v1.TopicAdminClient) Topic(com.google.pubsub.v1.Topic) SchemaSettings(com.google.pubsub.v1.SchemaSettings) SchemaName(com.google.pubsub.v1.SchemaName) TopicName(com.google.pubsub.v1.TopicName)

Aggregations

AlreadyExistsException (com.google.api.gax.rpc.AlreadyExistsException)1 TopicAdminClient (com.google.cloud.pubsub.v1.TopicAdminClient)1 SchemaName (com.google.pubsub.v1.SchemaName)1 SchemaSettings (com.google.pubsub.v1.SchemaSettings)1 Topic (com.google.pubsub.v1.Topic)1 TopicName (com.google.pubsub.v1.TopicName)1