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