Search in sources :

Example 1 with ProtobufSchema

use of io.confluent.kafka.schemaregistry.protobuf.ProtobufSchema in project druid by druid-io.

the class SchemaRegistryBasedProtobufBytesDecoder method parse.

@Override
public DynamicMessage parse(ByteBuffer bytes) {
    // ignore first \0 byte
    bytes.get();
    // extract schema registry id
    int id = bytes.getInt();
    // ignore \0 byte before PB message
    bytes.get();
    int length = bytes.limit() - 2 - 4;
    Descriptors.Descriptor descriptor;
    try {
        ProtobufSchema schema = (ProtobufSchema) registry.getSchemaById(id);
        descriptor = schema.toDescriptor();
    } catch (RestClientException e) {
        LOGGER.error(e.getMessage());
        throw new ParseException(null, e, "Fail to get protobuf schema because of can not connect to registry or failed http request!");
    } catch (IOException e) {
        LOGGER.error(e.getMessage());
        throw new ParseException(null, e, "Fail to get protobuf schema because of invalid schema!");
    }
    try {
        byte[] rawMessage = new byte[length];
        bytes.get(rawMessage, 0, length);
        DynamicMessage message = DynamicMessage.parseFrom(descriptor, rawMessage);
        return message;
    } catch (Exception e) {
        LOGGER.error(e.getMessage());
        throw new ParseException(null, e, "Fail to decode protobuf message!");
    }
}
Also used : RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) ProtobufSchema(io.confluent.kafka.schemaregistry.protobuf.ProtobufSchema) Descriptors(com.google.protobuf.Descriptors) ParseException(org.apache.druid.java.util.common.parsers.ParseException) IOException(java.io.IOException) DynamicMessage(com.google.protobuf.DynamicMessage) ParseException(org.apache.druid.java.util.common.parsers.ParseException) IOException(java.io.IOException) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException)

Example 2 with ProtobufSchema

use of io.confluent.kafka.schemaregistry.protobuf.ProtobufSchema in project druid by druid-io.

the class SchemaRegistryBasedProtobufBytesDecoderTest method parseProtobufSchema.

private ProtobufSchema parseProtobufSchema() throws IOException {
    // Given
    InputStream fin;
    fin = this.getClass().getClassLoader().getResourceAsStream("ProtoTest.proto");
    String protobufString = IOUtils.toString(fin, StandardCharsets.UTF_8);
    fin = this.getClass().getClassLoader().getResourceAsStream("google/protobuf/timestamp.proto");
    String timestampProtobufString = IOUtils.toString(fin, StandardCharsets.UTF_8);
    return new ProtobufSchema(protobufString, Collections.emptyList(), ImmutableMap.of("google/protobuf/timestamp.proto", timestampProtobufString), null, null);
}
Also used : InputStream(java.io.InputStream) ProtobufSchema(io.confluent.kafka.schemaregistry.protobuf.ProtobufSchema)

Aggregations

ProtobufSchema (io.confluent.kafka.schemaregistry.protobuf.ProtobufSchema)2 Descriptors (com.google.protobuf.Descriptors)1 DynamicMessage (com.google.protobuf.DynamicMessage)1 RestClientException (io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ParseException (org.apache.druid.java.util.common.parsers.ParseException)1