Search in sources :

Example 1 with ParsedSchema

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

the class SchemaRegistryBasedAvroBytesDecoder method parse.

@Override
public GenericRecord parse(ByteBuffer bytes) {
    int length = bytes.limit() - 1 - 4;
    if (length < 0) {
        throw new ParseException(null, "Failed to decode avro message, not enough bytes to decode (%s)", bytes.limit());
    }
    // ignore first \0 byte
    bytes.get();
    // extract schema registry id
    int id = bytes.getInt();
    int offset = bytes.position() + bytes.arrayOffset();
    Schema schema;
    try {
        ParsedSchema parsedSchema = registry.getSchemaById(id);
        schema = parsedSchema instanceof AvroSchema ? ((AvroSchema) parsedSchema).rawSchema() : null;
    } catch (IOException | RestClientException ex) {
        throw new ParseException(null, "Failed to get Avro schema: %s", id);
    }
    if (schema == null) {
        throw new ParseException(null, "Failed to find Avro schema: %s", id);
    }
    DatumReader<GenericRecord> reader = new GenericDatumReader<>(schema);
    try {
        return reader.read(null, DecoderFactory.get().binaryDecoder(bytes.array(), offset, length, null));
    } catch (Exception e) {
        throw new ParseException(null, e, "Fail to decode Avro message for schema: %s!", id);
    }
}
Also used : AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) Schema(org.apache.avro.Schema) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) ParseException(org.apache.druid.java.util.common.parsers.ParseException) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) IOException(java.io.IOException) GenericRecord(org.apache.avro.generic.GenericRecord) ParseException(org.apache.druid.java.util.common.parsers.ParseException) IOException(java.io.IOException) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException)

Aggregations

ParsedSchema (io.confluent.kafka.schemaregistry.ParsedSchema)1 AvroSchema (io.confluent.kafka.schemaregistry.avro.AvroSchema)1 RestClientException (io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException)1 IOException (java.io.IOException)1 Schema (org.apache.avro.Schema)1 GenericDatumReader (org.apache.avro.generic.GenericDatumReader)1 GenericRecord (org.apache.avro.generic.GenericRecord)1 ParseException (org.apache.druid.java.util.common.parsers.ParseException)1