@Override protected GenericRecord decodeRecord(ByteArrayBasedKafkaRecord messageAndOffset) throws IOException { byte[] payload = messageAndOffset.getMessageBytes(); Schema recordSchema = getRecordSchema(payload); Decoder decoder = getDecoder(payload); this.reader.get().setSchema(recordSchema); try { GenericRecord record = this.reader.get().read(null, decoder); record = convertRecord(record); return record; } catch (IOException e) { log.error(String.format("Error during decoding record for partition %s: ", this.getCurrentPartition())); throw e; } }
@Override public void setSchema(Schema actual) { // if expected is unset and actual is a specific record, // then default expected to schema of currently loaded class if (getExpected() == null && actual != null && actual.getType() == Schema.Type.RECORD) { SpecificData data = getSpecificData(); Class c = data.getClass(actual); if (c != null && SpecificRecord.class.isAssignableFrom(c)) setExpected(data.getSchema(c)); } super.setSchema(actual); }
@Override public void setSchema(Schema actual) { // if expected is unset and actual is a specific record, // then default expected to schema of currently loaded class if (getExpected() == null && actual != null && actual.getType() == Schema.Type.RECORD) { SpecificData data = getSpecificData(); Class c = data.getClass(actual); if (c != null && SpecificRecord.class.isAssignableFrom(c)) setExpected(data.getSchema(c)); } super.setSchema(actual); }
@Override public T deserialize(byte[] message) throws IOException { // read record checkAvroInitialized(); inputStream.setBuffer(message); Schema readerSchema = getReaderSchema(); GenericDatumReader<T> datumReader = getDatumReader(); datumReader.setSchema(readerSchema); return datumReader.read(null, decoder); }
public Object toObject(byte[] bytes) { Integer version = Integer.valueOf(bytes[0]); if(version > newestVersion) throw new SerializationException("Client needs to rebootstrap! \n Writer's schema version greater than Reader"); Schema typeDefWriter = Schema.parse(typeDefVersions.get(version)); Decoder decoder = DecoderFactory.defaultFactory().createBinaryDecoder(bytes, 1, bytes.length - 1, null); GenericDatumReader<Object> reader = null; try { reader = new GenericDatumReader<Object>(typeDefWriter, typeDef); // writer's schema reader.setSchema(typeDefWriter); // Reader's schema reader.setExpected(typeDef); return reader.read(null, decoder); } catch(IOException e) { throw new SerializationException(e); } }
data.length - MD5Digest.MD5_BYTES_LENGTH - 1, null); _datumReader.setExpected(outputSchema); _datumReader.setSchema(schema); try { GenericRecord record = _datumReader.read(null, decoder);
/** * Convert the payload in the input record to a deserialized object with the latest schema * * @param inputRecord the input record * @return the schema'ed payload object */ protected P upConvertPayload(GenericRecord inputRecord) throws DataConversionException { try { Schema payloadSchema = getPayloadSchema(inputRecord); // Set writer schema latestPayloadReader.setSchema(payloadSchema); byte[] payloadBytes = getPayloadBytes(inputRecord); Decoder decoder = DecoderFactory.get().binaryDecoder(payloadBytes, null); // 'latestPayloadReader.read' will convert the record from 'payloadSchema' to the latest payload schema return latestPayloadReader.read(null, decoder); } catch (Exception e) { throw new DataConversionException(e); } } }
private Record decodeGenericBlob(Schema expectedSchema, Schema schemaOfBlob, byte[] blob) throws IOException { if (blob == null) { return null; } GenericDatumReader<Record> reader = new GenericDatumReader<>(); reader.setExpected(expectedSchema); reader.setSchema(schemaOfBlob); Decoder decoder = encoderType == EncoderType.BINARY ? DecoderFactory.get().binaryDecoder(blob, null) : DecoderFactory.get().jsonDecoder(schemaOfBlob, new ByteArrayInputStream(blob)); return reader.read(null, decoder); } }
@Override public T deserialize(byte[] message) throws IOException { checkAvroInitialized(); getInputStream().setBuffer(message); Schema writerSchema = schemaCoder.readSchema(getInputStream()); Schema readerSchema = getReaderSchema(); GenericDatumReader<T> datumReader = getDatumReader(); datumReader.setSchema(writerSchema); datumReader.setExpected(readerSchema); return datumReader.read(null, getDecoder()); }
@Test public void testDecodeFromSources() throws IOException { GenericDatumReader<Object> reader = new GenericDatumReader<>(); reader.setSchema(schema);
@Override public CamusWrapper<Record> decode(Message message) { try { GenericDatumReader<Record> reader = new GenericDatumReader<Record>(); Schema schema = super.registry.getLatestSchemaByTopic(super.topicName).getSchema(); reader.setSchema(schema); return new CamusWrapper<Record>(reader.read(null, decoderFactory.jsonDecoder(schema, new String(message.getPayload(), //Message.payloadOffset(message.magic()), kafka.message.Message.MagicOffset(), message.getPayload().length - kafka.message.Message.MagicOffset())))); } catch (Exception e) { throw new RuntimeException(e); } } }
public static GenericRecord genericRecordFromJson(String json, Schema schema) throws IOException { GenericDatumReader<GenericRecord> reader = new GenericDatumReader<GenericRecord>(); AvroAdapter avroAdapter = AvroAdapterFinder.getAvroAdapter(); Decoder jsonDecoder = avroAdapter.createJsonDecoder(schema, json); reader.setSchema(schema); GenericRecord record = reader.read(null, jsonDecoder); return record; } }
public static GenericRecord genericRecordFromBytes(byte[] bytes, Schema schema) throws IOException { GenericDatumReader<GenericRecord> reader = new GenericDatumReader<GenericRecord>(); AvroAdapter avroAdapter = AvroAdapterFinder.getAvroAdapter(); Decoder binaryDecoder = avroAdapter.createBinaryDecoder(bytes); reader.setSchema(schema); GenericRecord record = reader.read(null, binaryDecoder); return record; }
@Override protected GenericRecord decodeRecord(ByteArrayBasedKafkaRecord messageAndOffset) throws IOException { byte[] payload = messageAndOffset.getMessageBytes(); Schema recordSchema = getRecordSchema(payload); Decoder decoder = getDecoder(payload); this.reader.get().setSchema(recordSchema); try { GenericRecord record = this.reader.get().read(null, decoder); record = convertRecord(record); return record; } catch (IOException e) { log.error(String.format("Error during decoding record for partition %s: ", this.getCurrentPartition())); throw e; } }
@Override public void setSchema(Schema actual) { // if expected is unset and actual is a specific record, // then default expected to schema of currently loaded class if (getExpected() == null && actual != null && actual.getType() == Schema.Type.RECORD) { SpecificData data = getSpecificData(); Class c = data.getClass(actual); if (c != null && SpecificRecord.class.isAssignableFrom(c)) setExpected(data.getSchema(c)); } super.setSchema(actual); }
@Override public void setSchema(Schema actual) { // if expected is unset and actual is a specific record, // then default expected to schema of currently loaded class if (getExpected() == null && actual != null && actual.getType() == Schema.Type.RECORD) { SpecificData data = getSpecificData(); Class c = data.getClass(actual); if (c != null && SpecificRecord.class.isAssignableFrom(c)) setExpected(data.getSchema(c)); } super.setSchema(actual); }
@Override public T deserialize(byte[] message) throws IOException { // read record checkAvroInitialized(); inputStream.setBuffer(message); Schema readerSchema = getReaderSchema(); GenericDatumReader<T> datumReader = getDatumReader(); datumReader.setSchema(readerSchema); return datumReader.read(null, decoder); }
@Override public T deserialize(byte[] message) throws IOException { // read record checkAvroInitialized(); inputStream.setBuffer(message); Schema readerSchema = getReaderSchema(); GenericDatumReader<T> datumReader = getDatumReader(); datumReader.setSchema(readerSchema); return datumReader.read(null, decoder); }
@Override public T deserialize(byte[] message) throws IOException { checkAvroInitialized(); getInputStream().setBuffer(message); Schema writerSchema = schemaCoder.readSchema(getInputStream()); Schema readerSchema = getReaderSchema(); GenericDatumReader<T> datumReader = getDatumReader(); datumReader.setSchema(writerSchema); datumReader.setExpected(readerSchema); return datumReader.read(null, getDecoder()); }
@Override public T deserialize(byte[] message) throws IOException { checkAvroInitialized(); getInputStream().setBuffer(message); Schema writerSchema = schemaCoder.readSchema(getInputStream()); Schema readerSchema = getReaderSchema(); GenericDatumReader<T> datumReader = getDatumReader(); datumReader.setSchema(writerSchema); datumReader.setExpected(readerSchema); return datumReader.read(null, getDecoder()); }