@Override public Object readResponse(Schema writer, Schema reader, Decoder in) throws IOException { return new GenericDatumReader<>(writer, reader, data).read(null, in); }
public void readFields(byte[] bytes, int offset, int length, Schema writerSchema, Schema readerSchema) throws IOException { fileSchema = writerSchema; record = new GenericData.Record(writerSchema); binaryDecoder = DecoderFactory.get().binaryDecoder(bytes, offset, length - offset, binaryDecoder); GenericDatumReader<GenericRecord> gdr = new GenericDatumReader<GenericRecord>(writerSchema, readerSchema); record = gdr.read(null, binaryDecoder); }
/** * Deserialize payload using payload schema */ public GenericRecord deserializePayload(byte[] payload, Schema payloadSchema) throws IOException, ExecutionException { Decoder decoder = this.decoderFactory.binaryDecoder(payload, null); GenericDatumReader<GenericRecord> reader = this.readers.get(payloadSchema); return reader.read(null, decoder); } }
/** * Deserialize a {@link GenericRecord} from a byte array. This method is not intended for high performance. */ public static GenericRecord slowDeserializeGenericRecord(byte[] serializedRecord, Schema schema) throws IOException { Decoder decoder = DecoderFactory.get().binaryDecoder(serializedRecord, null); GenericDatumReader<GenericRecord> reader = new GenericDatumReader<>(schema); return reader.read(null, decoder); } }
@Override public Exception readError(Schema writer, Schema reader, Decoder in) throws IOException { Object error = new GenericDatumReader<>(writer, reader, data) .read(null,in); if (error instanceof CharSequence) return new AvroRuntimeException(error.toString()); // system error return new AvroRemoteException(error); }
/** Called to read a single field of a record. May be overridden for more * efficient or alternate implementations.*/ protected void readField(Object r, Field f, Object oldDatum, ResolvingDecoder in, Object state) throws IOException { data.setField(r, f.name(), f.pos(), read(oldDatum, f.schema(), in), state); }
@Override public GenericContainer read(Kryo kryo, Input input, Class<GenericContainer> aClass) { Schema theSchema = this.getSchema(input.readString()); GenericDatumReader<GenericContainer> reader = new GenericDatumReader<>(theSchema); Decoder decoder = DecoderFactory .get() .directBinaryDecoder(input, null); GenericContainer foo; try { foo = reader.read(null, decoder); } catch (IOException e) { throw new RuntimeException(e); } return foo; } }
/** Called to read a single field of a record. May be overridden for more * efficient or alternate implementations.*/ protected void readField(Object r, Field f, Object oldDatum, ResolvingDecoder in, Object state) throws IOException { data.setField(r, f.name(), f.pos(), read(oldDatum, f.schema(), in), state); }
@Override void readInternal(Decoder d) throws IOException { GenericDatumReader<Object> reader = new GenericDatumReader<>(schema); for (int i = 0; i < count; i++) { reader.read(null, d); } }
@Override public GenericRecord decode(byte[] bytes) { try { Decoder decoder = DecoderFactory.get().binaryDecoder(bytes, null); org.apache.avro.generic.GenericRecord avroRecord = datumReader.read( null, decoder); return new GenericAvroRecord(schema, fields, avroRecord); } catch (IOException e) { throw new SchemaSerializationException(e); } }
@Override public void readFields(DataInput in) throws IOException { Schema schema = AvroSerdeUtils.getSchemaFor(in.readUTF()); fileSchema = AvroSerdeUtils.getSchemaFor(in.readUTF()); recordReaderID = UID.read(in); record = new GenericData.Record(schema); binaryDecoder = DecoderFactory.defaultFactory().createBinaryDecoder((InputStream) in, binaryDecoder); GenericDatumReader<GenericRecord> gdr = new GenericDatumReader<GenericRecord>(schema); record = gdr.read(record, binaryDecoder); }
public Object toObject(byte[] bytes) { Decoder decoder = DecoderFactory.defaultFactory().createBinaryDecoder(bytes, null); GenericDatumReader<Object> reader = null; try { reader = new GenericDatumReader<Object>(typeDef); return reader.read(null, decoder); } catch(IOException e) { throw new SerializationException(e); } }
@Override void readInternal(Decoder d) throws IOException { for (int i = 0; i < count; i++) { getReader().read(null, d); } } @Override
@Override @SuppressWarnings("unchecked") public D read(D reuse, Decoder in) throws IOException { ResolvingDecoder resolver = getResolver(actual, expected); resolver.configure(in); D result = (D) read(reuse, expected, resolver); resolver.drain(); return result; }
public GenericRecord reencode(GenericRecord r) throws AvroSerdeException { baos.reset(); BinaryEncoder be = EncoderFactory.get().directBinaryEncoder(baos, null); gdw.setSchema(r.getSchema()); try { gdw.write(r, be); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); binaryDecoder = DecoderFactory.defaultFactory().createBinaryDecoder(bais, binaryDecoder); return gdr.read(r, binaryDecoder); } catch (IOException e) { throw new AvroSerdeException("Exception trying to re-encode record to new schema", e); } } }
@Override @SuppressWarnings("unchecked") public D read(D reuse, Decoder in) throws IOException { ResolvingDecoder resolver = getResolver(actual, expected); resolver.configure(in); D result = (D) read(reuse, expected, resolver); resolver.drain(); return result; }
@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); }
@Test public void testJsonRecordOrdering() throws IOException { String value = "{\"b\": 2, \"a\": 1}"; Schema schema = new Schema.Parser().parse("{\"type\": \"record\", \"name\": \"ab\", \"fields\": [" + "{\"name\": \"a\", \"type\": \"int\"}, {\"name\": \"b\", \"type\": \"int\"}" + "]}"); GenericDatumReader<Object> reader = new GenericDatumReader<>(schema); Decoder decoder = DecoderFactory.get().jsonDecoder(schema, value); Object o = reader.read(null, decoder); Assert.assertEquals("{\"a\": 1, \"b\": 2}", o.toString()); }
private <T> GenericRecord testJsonDecoder (String testType, byte[] bytes, T entityObj) throws IOException { ReflectData rdata = ReflectData.AllowNull.get(); Schema schema = rdata.getSchema(entityObj.getClass()); GenericDatumReader<GenericRecord> datumReader = new GenericDatumReader<>(schema); Decoder decoder = DecoderFactory.get().jsonDecoder(schema, new String(bytes)); GenericRecord r = datumReader.read(null, decoder); return r; }
@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()); }