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.");
}
}
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.");
}
}
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.");
}
}
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.");
}
}
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);
}
Aggregations