Search in sources :

Example 1 with SchemaServiceClient

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

the class CreateAvroSchemaExample method createAvroSchemaExample.

public static void createAvroSchemaExample(String projectId, String schemaId, String avscFile) throws IOException {
    ProjectName projectName = ProjectName.of(projectId);
    SchemaName schemaName = SchemaName.of(projectId, schemaId);
    // Read an Avro schema file formatted in JSON as a string.
    String avscSource = new String(Files.readAllBytes(Paths.get(avscFile)));
    try (SchemaServiceClient schemaServiceClient = SchemaServiceClient.create()) {
        Schema schema = schemaServiceClient.createSchema(projectName, Schema.newBuilder().setName(schemaName.toString()).setType(Schema.Type.AVRO).setDefinition(avscSource).build(), schemaId);
        System.out.println("Created a schema using an Avro schema:\n" + schema);
    } catch (AlreadyExistsException e) {
        System.out.println(schemaName + "already exists.");
    }
}
Also used : AlreadyExistsException(com.google.api.gax.rpc.AlreadyExistsException) ProjectName(com.google.pubsub.v1.ProjectName) Schema(com.google.pubsub.v1.Schema) SchemaServiceClient(com.google.cloud.pubsub.v1.SchemaServiceClient) SchemaName(com.google.pubsub.v1.SchemaName)

Example 2 with SchemaServiceClient

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

the class CreateProtoSchemaExample method createProtoSchemaExample.

public static void createProtoSchemaExample(String projectId, String schemaId, String protoFile) throws IOException {
    ProjectName projectName = ProjectName.of(projectId);
    SchemaName schemaName = SchemaName.of(projectId, schemaId);
    // Read a proto file as a string.
    String protoSource = new String(Files.readAllBytes(Paths.get(protoFile)));
    try (SchemaServiceClient schemaServiceClient = SchemaServiceClient.create()) {
        Schema schema = schemaServiceClient.createSchema(projectName, Schema.newBuilder().setName(schemaName.toString()).setType(Schema.Type.PROTOCOL_BUFFER).setDefinition(protoSource).build(), schemaId);
        System.out.println("Created a schema using a protobuf schema:\n" + schema);
    } catch (AlreadyExistsException e) {
        System.out.println(schemaName + "already exists.");
    }
}
Also used : AlreadyExistsException(com.google.api.gax.rpc.AlreadyExistsException) ProjectName(com.google.pubsub.v1.ProjectName) Schema(com.google.pubsub.v1.Schema) SchemaServiceClient(com.google.cloud.pubsub.v1.SchemaServiceClient) SchemaName(com.google.pubsub.v1.SchemaName)

Example 3 with SchemaServiceClient

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

the class DeleteSchemaExample method deleteSchemaExample.

public static void deleteSchemaExample(String projectId, String schemaId) throws IOException {
    SchemaName schemaName = SchemaName.of(projectId, schemaId);
    try (SchemaServiceClient schemaServiceClient = SchemaServiceClient.create()) {
        schemaServiceClient.deleteSchema(schemaName);
        System.out.println("Deleted a schema:" + schemaName);
    } catch (NotFoundException e) {
        System.out.println(schemaName + "not found.");
    }
}
Also used : NotFoundException(com.google.api.gax.rpc.NotFoundException) SchemaServiceClient(com.google.cloud.pubsub.v1.SchemaServiceClient) SchemaName(com.google.pubsub.v1.SchemaName)

Example 4 with SchemaServiceClient

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

the class GetSchemaExample method getSchemaExample.

public static void getSchemaExample(String projectId, String schemaId) throws IOException {
    SchemaName schemaName = SchemaName.of(projectId, schemaId);
    try (SchemaServiceClient schemaServiceClient = SchemaServiceClient.create()) {
        Schema schema = schemaServiceClient.getSchema(schemaName);
        System.out.println("Got a schema:\n" + schema);
    } catch (NotFoundException e) {
        System.out.println(schemaName + "not found.");
    }
}
Also used : Schema(com.google.pubsub.v1.Schema) NotFoundException(com.google.api.gax.rpc.NotFoundException) SchemaServiceClient(com.google.cloud.pubsub.v1.SchemaServiceClient) SchemaName(com.google.pubsub.v1.SchemaName)

Example 5 with SchemaServiceClient

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

the class SchemaIT method tearDown.

@After
public void tearDown() throws Exception {
    // Delete the schemas if they have not been cleaned up.
    try (SchemaServiceClient schemaServiceClient = SchemaServiceClient.create()) {
        schemaServiceClient.deleteSchema(protoSchemaName);
        schemaServiceClient.deleteSchema(avroSchemaName);
    } catch (NotFoundException ignored) {
    // Ignore this as resources may have already been cleaned up.
    }
    // Delete the subscriptions.
    try (SubscriptionAdminClient subscriptionAdmin = SubscriptionAdminClient.create()) {
        subscriptionAdmin.deleteSubscription(avroSubscriptionName.toString());
        subscriptionAdmin.deleteSubscription(protoSubscriptionName.toString());
    } catch (NotFoundException ignored) {
    // Ignore this as resources may have already been cleaned up.
    }
    // Delete the topics.
    try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
        topicAdminClient.deleteTopic(avroTopicName.toString());
        topicAdminClient.deleteTopic(protoTopicName.toString());
    } catch (NotFoundException ignored) {
    // Ignore this as resources may have already been cleaned up.
    }
    System.setOut(null);
}
Also used : TopicAdminClient(com.google.cloud.pubsub.v1.TopicAdminClient) SubscriptionAdminClient(com.google.cloud.pubsub.v1.SubscriptionAdminClient) NotFoundException(com.google.api.gax.rpc.NotFoundException) SchemaServiceClient(com.google.cloud.pubsub.v1.SchemaServiceClient) After(org.junit.After)

Aggregations

SchemaServiceClient (com.google.cloud.pubsub.v1.SchemaServiceClient)5 SchemaName (com.google.pubsub.v1.SchemaName)4 NotFoundException (com.google.api.gax.rpc.NotFoundException)3 Schema (com.google.pubsub.v1.Schema)3 AlreadyExistsException (com.google.api.gax.rpc.AlreadyExistsException)2 ProjectName (com.google.pubsub.v1.ProjectName)2 SubscriptionAdminClient (com.google.cloud.pubsub.v1.SubscriptionAdminClient)1 TopicAdminClient (com.google.cloud.pubsub.v1.TopicAdminClient)1 After (org.junit.After)1