Search in sources :

Example 1 with Encoding

use of com.google.pubsub.v1.Encoding in project HybridTestFramework by dipjyotimetia.

the class PubSub method publishAvroRecords.

public static void publishAvroRecords(String projectId, String topicId, Config config) throws IOException, ExecutionException, InterruptedException {
    Encoding encoding = null;
    TopicName topicName = TopicName.of(projectId, topicId);
    // Get the topic encoding type.
    try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
        encoding = topicAdminClient.getTopic(topicName).getSchemaSettings().getEncoding();
    }
    // Instantiate an avro-tools-generated class defined in `us-states.avsc`.
    Customer customer = Customer.newBuilder().setFirstName("John").setLastName("Doe").setAge(25).setHeight(185.5f).setWeight(85.6f).setAutomatedEmail(false).build();
    Publisher publisher = null;
    block: try {
        publisher = Publisher.newBuilder(topicName).setChannelProvider(config.ChannelProvider()).setCredentialsProvider(config.CredentialProvider()).build();
        // Prepare to serialize the object to the output stream.
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        Encoder encoder = null;
        // Prepare an appropriate encoder for publishing to the topic.
        switch(encoding) {
            case BINARY:
                System.out.println("Preparing a BINARY encoder...");
                encoder = EncoderFactory.get().directBinaryEncoder(byteStream, /*reuse=*/
                null);
                break;
            case JSON:
                System.out.println("Preparing a JSON encoder...");
                encoder = EncoderFactory.get().jsonEncoder(Customer.getClassSchema(), byteStream);
                break;
            default:
                break block;
        }
        // Encode the object and write it to the output stream.
        customer.customEncode(encoder);
        encoder.flush();
        // Publish the encoded object as a Pub/Sub message.
        ByteString data = ByteString.copyFrom(byteStream.toByteArray());
        PubsubMessage message = PubsubMessage.newBuilder().setData(data).build();
        System.out.println("Publishing message: " + message);
        ApiFuture<String> future = publisher.publish(message);
        System.out.println("Published message ID: " + future.get());
    } finally {
        if (publisher != null) {
            publisher.shutdown();
            publisher.awaitTermination(1, TimeUnit.MINUTES);
        }
    }
}
Also used : ApiFuture(com.google.api.core.ApiFuture) TopicAdminClient(com.google.cloud.pubsub.v1.TopicAdminClient) Customer(com.avro.Customer) Encoder(org.apache.avro.io.Encoder) ByteString(com.google.protobuf.ByteString) Encoding(com.google.pubsub.v1.Encoding) Publisher(com.google.cloud.pubsub.v1.Publisher) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PubsubMessage(com.google.pubsub.v1.PubsubMessage) TopicName(com.google.pubsub.v1.TopicName)

Example 2 with Encoding

use of com.google.pubsub.v1.Encoding in project divolte-collector by divolte.

the class GoogleCloudPubSubFlusherTest method testMessagesHaveSchemaFingerprint.

@Test
public void testMessagesHaveSchemaFingerprint() {
    processSingleMessage();
    // Reminder: fingerprint is the SHA-256 hash of the normalized schema,
    // base-64 encoded using the URL-safe encoding,
    // with trailing padding stripped.
    final String expectedFingerPrint = BaseEncoding.base64Url().encode(Hashing.sha256().hashString(SchemaNormalization.toParsingForm(MINIMAL_SCHEMA), StandardCharsets.UTF_8).asBytes()).replace("=", "");
    final PubsubMessage deliveredMessage = getFirstPublishedMessage();
    assertEquals(expectedFingerPrint, deliveredMessage.getAttributesOrThrow("schemaFingerprint"));
}
Also used : ByteString(com.google.protobuf.ByteString) PubsubMessage(com.google.pubsub.v1.PubsubMessage) Test(org.junit.Test)

Example 3 with Encoding

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

the class SubscribeWithAvroSchemaExample method subscribeWithAvroSchemaExample.

public static void subscribeWithAvroSchemaExample(String projectId, String subscriptionId) {
    ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);
    // Prepare a reader for the encoded Avro records.
    SpecificDatumReader<State> reader = new SpecificDatumReader<>(State.getClassSchema());
    // Instantiate an asynchronous message receiver.
    MessageReceiver receiver = (PubsubMessage message, AckReplyConsumer consumer) -> {
        ByteString data = message.getData();
        // Get the schema encoding type.
        String encoding = message.getAttributesMap().get("googclient_schemaencoding");
        // Send the message data to a byte[] input stream.
        InputStream inputStream = new ByteArrayInputStream(data.toByteArray());
        Decoder decoder = null;
        // based on the schema encoding type.
        block: try {
            switch(encoding) {
                case "BINARY":
                    decoder = DecoderFactory.get().directBinaryDecoder(inputStream, /*reuse=*/
                    null);
                    System.out.println("Receiving a binary-encoded message:");
                    break;
                case "JSON":
                    decoder = DecoderFactory.get().jsonDecoder(State.getClassSchema(), inputStream);
                    System.out.println("Receiving a JSON-encoded message:");
                    break;
                default:
                    break block;
            }
            // Obtain an object of the generated Avro class using the decoder.
            State state = reader.read(null, decoder);
            System.out.println(state.getName() + " is abbreviated as " + state.getPostAbbr());
        } catch (IOException e) {
            System.err.println(e);
        }
        // Ack the message.
        consumer.ack();
    };
    Subscriber subscriber = null;
    try {
        subscriber = Subscriber.newBuilder(subscriptionName, receiver).build();
        subscriber.startAsync().awaitRunning();
        System.out.printf("Listening for messages on %s:\n", subscriptionName.toString());
        subscriber.awaitTerminated(30, TimeUnit.SECONDS);
    } catch (TimeoutException timeoutException) {
        subscriber.stopAsync();
    }
}
Also used : ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) ByteString(com.google.protobuf.ByteString) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException) Decoder(org.apache.avro.io.Decoder) PubsubMessage(com.google.pubsub.v1.PubsubMessage) MessageReceiver(com.google.cloud.pubsub.v1.MessageReceiver) ByteArrayInputStream(java.io.ByteArrayInputStream) Subscriber(com.google.cloud.pubsub.v1.Subscriber) State(utilities.State) SpecificDatumReader(org.apache.avro.specific.SpecificDatumReader) AckReplyConsumer(com.google.cloud.pubsub.v1.AckReplyConsumer) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with Encoding

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

the class PublishAvroRecordsExample method publishAvroRecordsExample.

public static void publishAvroRecordsExample(String projectId, String topicId) throws IOException, ExecutionException, InterruptedException {
    Encoding encoding = null;
    TopicName topicName = TopicName.of(projectId, topicId);
    // Get the topic encoding type.
    try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
        encoding = topicAdminClient.getTopic(topicName).getSchemaSettings().getEncoding();
    }
    // Instantiate an avro-tools-generated class defined in `us-states.avsc`.
    State state = State.newBuilder().setName("Alaska").setPostAbbr("AK").build();
    Publisher publisher = null;
    block: try {
        publisher = Publisher.newBuilder(topicName).build();
        // Prepare to serialize the object to the output stream.
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        Encoder encoder = null;
        // Prepare an appropriate encoder for publishing to the topic.
        switch(encoding) {
            case BINARY:
                System.out.println("Preparing a BINARY encoder...");
                encoder = EncoderFactory.get().directBinaryEncoder(byteStream, /*reuse=*/
                null);
                break;
            case JSON:
                System.out.println("Preparing a JSON encoder...");
                encoder = EncoderFactory.get().jsonEncoder(State.getClassSchema(), byteStream);
                break;
            default:
                break block;
        }
        // Encode the object and write it to the output stream.
        state.customEncode(encoder);
        encoder.flush();
        // Publish the encoded object as a Pub/Sub message.
        ByteString data = ByteString.copyFrom(byteStream.toByteArray());
        PubsubMessage message = PubsubMessage.newBuilder().setData(data).build();
        System.out.println("Publishing message: " + message);
        ApiFuture<String> future = publisher.publish(message);
        System.out.println("Published message ID: " + future.get());
    } finally {
        if (publisher != null) {
            publisher.shutdown();
            publisher.awaitTermination(1, TimeUnit.MINUTES);
        }
    }
}
Also used : ApiFuture(com.google.api.core.ApiFuture) TopicAdminClient(com.google.cloud.pubsub.v1.TopicAdminClient) State(utilities.State) Encoder(org.apache.avro.io.Encoder) ByteString(com.google.protobuf.ByteString) Encoding(com.google.pubsub.v1.Encoding) Publisher(com.google.cloud.pubsub.v1.Publisher) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PubsubMessage(com.google.pubsub.v1.PubsubMessage) TopicName(com.google.pubsub.v1.TopicName)

Example 5 with Encoding

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

the class CreateTopicWithSchemaExample method main.

public static void main(String... args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "your-project-id";
    String topicId = "your-topic-id";
    // Use an existing schema.
    String schemaId = "your-schema-id";
    // Choose either BINARY or JSON message serialization in this topic.
    Encoding encoding = Encoding.BINARY;
    createTopicWithSchemaExample(projectId, topicId, schemaId, encoding);
}
Also used : Encoding(com.google.pubsub.v1.Encoding)

Aggregations

ByteString (com.google.protobuf.ByteString)6 PubsubMessage (com.google.pubsub.v1.PubsubMessage)5 TopicAdminClient (com.google.cloud.pubsub.v1.TopicAdminClient)4 Encoding (com.google.pubsub.v1.Encoding)4 TopicName (com.google.pubsub.v1.TopicName)4 ApiFuture (com.google.api.core.ApiFuture)3 Publisher (com.google.cloud.pubsub.v1.Publisher)3 AckReplyConsumer (com.google.cloud.pubsub.v1.AckReplyConsumer)2 MessageReceiver (com.google.cloud.pubsub.v1.MessageReceiver)2 Subscriber (com.google.cloud.pubsub.v1.Subscriber)2 ProjectSubscriptionName (com.google.pubsub.v1.ProjectSubscriptionName)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 TimeoutException (java.util.concurrent.TimeoutException)2 Encoder (org.apache.avro.io.Encoder)2 State (utilities.State)2 State (utilities.StateProto.State)2 Customer (com.avro.Customer)1 AlreadyExistsException (com.google.api.gax.rpc.AlreadyExistsException)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 SchemaName (com.google.pubsub.v1.SchemaName)1