public void jsonReadWriteExample() throws IOException { Employee employee = Employee.newBuilder().setFirstName("Gaurav") .setLastName("Mazra").setSex(SEX.MALE).build(); DatumWriter<Employee> employeeWriter = new SpecificDatumWriter<>(Employee.class); byte[] data; try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { Encoder jsonEncoder = EncoderFactory.get().jsonEncoder(Employee.getClassSchema(), baos); employeeWriter.write(employee, jsonEncoder); jsonEncoder.flush(); data = baos.toByteArray(); } // serialized data System.out.println(new String(data)); DatumReader<Employee> employeeReader = new SpecificDatumReader<>(Employee.class); Decoder decoder = DecoderFactory.get().jsonDecoder(Employee.getClassSchema(), new String(data)); employee = employeeReader.read(null, decoder); //data after deserialization System.out.println(employee); }
public static void main(String[] args) throws Exception { Employee employee = Employee.newBuilder().setFirstName("Gaurav") .setLastName("Mazra").setSex(SEX.MALE).build(); byte[] payload; DatumWriter<Employee> datumWriter = new SpecificDatumWriter<>(Employee.class); try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { Encoder out = EncoderFactory.get().binaryEncoder(baos, null); datumWriter.write(employee, out ); out.flush(); payload = baos.toByteArray(); } catch (Exception e) { System.err.println(e); throw e; } System.out.println(new String(payload)); System.out.println(payload.length); }
public static void main(String[] args) throws Exception { Node node = new Node(); node.setValue("Gaurav"); node.setNext(node); byte[] payload; DatumWriter<Node> datumWriter = new ReflectDatumWriter<>(Node.class); try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { Encoder out = EncoderFactory.get().binaryEncoder(baos, null); datumWriter.write(node, out ); out.flush(); payload = baos.toByteArray(); } catch (Exception e) { System.err.println(e); throw e; } System.out.println(new String(payload)); System.out.println(payload.length); }
public static void main(String[] args) throws Exception { Node node = new Node(); node.setValue("Gaurav"); node.setNext(node); byte[] payload; DatumWriter<Node> datumWriter = new SpecificDatumWriter<>(Node.class); try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { Encoder out = EncoderFactory.get().binaryEncoder(baos, null); datumWriter.write(node, out ); out.flush(); payload = baos.toByteArray(); } catch (Exception e) { System.err.println(e); throw e; } System.out.println(new String(payload)); System.out.println(payload.length); }
public void binaryReadWriteExample() throws IOException { Employee employee = Employee.newBuilder().setFirstName("Gaurav") .setLastName("Mazra").setSex(SEX.MALE).build(); DatumWriter<Employee> employeeWriter = new SpecificDatumWriter<>(Employee.class); byte[] data; try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { Encoder binaryEncoder = EncoderFactory.get().binaryEncoder(baos, null); employeeWriter.write(employee, binaryEncoder); binaryEncoder.flush(); data = baos.toByteArray(); } // serialized data System.out.println(data); DatumReader<Employee> employeeReader = new SpecificDatumReader<>(Employee.class); Decoder binaryDecoder = DecoderFactory.get().binaryDecoder(data, null); employee = employeeReader.read(null, binaryDecoder); //data after deserialization System.out.println(employee); }
/** * tests Avro Serializer */ @Test public void testSerializer() throws Exception { Context context = new Context(); String schemaFile = getClass().getResource("/schema.avsc").getFile(); context.put(ES_AVRO_SCHEMA_FILE, schemaFile); avroSerializer.configure(context); Schema schema = new Schema.Parser().parse(new File(schemaFile)); GenericRecord user = generateGenericRecord(schema); DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<GenericRecord>(schema); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Encoder encoder = new EncoderFactory().binaryEncoder(outputStream, null); datumWriter.write(user, encoder); encoder.flush(); Event event = EventBuilder.withBody(outputStream.toByteArray()); XContentBuilder expected = generateContentBuilder(); XContentBuilder actual = avroSerializer.serialize(event); JsonParser parser = new JsonParser(); assertEquals(parser.parse(expected.string()), parser.parse(actual.string())); }
static void convertJsonToAvro(InputStream inputStream, OutputStream outputStream, Schema schema) throws IOException { DatumReader<Object> reader = new GenericDatumReader<>(schema); DatumWriter<Object> writer = new GenericDatumWriter<>(schema); Encoder binaryEncoder = EncoderFactory.get().binaryEncoder(outputStream, null); JsonDecoder jsonDecoder = DecoderFactory.get().jsonDecoder(schema, inputStream); Object datum = null; while (true) { try { datum = reader.read(datum, jsonDecoder); } catch (EOFException eofException) { break; } writer.write(datum, binaryEncoder); binaryEncoder.flush(); } outputStream.flush(); }
/** * Convert a GenericRecord to a byte array. */ public static byte[] recordToByteArray(GenericRecord record) throws IOException { Closer closer = Closer.create(); try { ByteArrayOutputStream out = closer.register(new ByteArrayOutputStream()); Encoder encoder = EncoderFactory.get().directBinaryEncoder(out, null); DatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(record.getSchema()); writer.write(record, encoder); byte[] byteArray = out.toByteArray(); return byteArray; } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } }
public byte[] compressURI(final URI uri) { Preconditions.checkNotNull(uri); try { final ByteArrayOutputStream stream = new ByteArrayOutputStream(); final Encoder encoder = EncoderFactory.get().directBinaryEncoder(stream, null); final DatumWriter<Object> writer = new GenericDatumWriter<Object>( AvroSchemas.COMPRESSED_IDENTIFIER); this.dictionary.keyFor(uri); // ensure a compressed version of URI is available final Object generic = encodeIdentifier(uri); writer.write(generic, encoder); return stream.toByteArray(); } catch (final IOException ex) { throw new Error("Unexpected exception (!): " + ex.getMessage(), ex); } }
@Test public void test() throws Throwable { System.out.println(AvroSchemas.RECORD.toString()); final Record resource = HBaseTestUtils.getMockResource(); //final Object generic = AvroSerialization.toGenericData(resource); final Object generic = null; final ByteArrayOutputStream jsonStream = new ByteArrayOutputStream(); final ByteArrayOutputStream binaryStream = new ByteArrayOutputStream(); final Encoder jsonEncoder = EncoderFactory.get().jsonEncoder(AvroSchemas.RECORD, jsonStream); final Encoder binaryEncoder = EncoderFactory.get().directBinaryEncoder(binaryStream, null); final DatumWriter<Object> writer = new GenericDatumWriter<Object>(AvroSchemas.RECORD); writer.write(generic, jsonEncoder); writer.write(generic, binaryEncoder); binaryEncoder.flush(); jsonEncoder.flush(); final byte[] bytes = binaryStream.toByteArray(); final String json = new String(jsonStream.toByteArray(), Charsets.UTF_8); System.out.println(bytes.length + " bytes: " + BaseEncoding.base16().encode(bytes)); System.out.println("JSON:\n" + json); }
public byte[] compressURI(final URI uri) { Preconditions.checkNotNull(uri); try { final ByteArrayOutputStream stream = new ByteArrayOutputStream(); final Encoder encoder = EncoderFactory.get().directBinaryEncoder(stream, null); final DatumWriter<Object> writer = new GenericDatumWriter<Object>( Schemas.COMPRESSED_IDENTIFIER); this.dictionary.keyFor(uri); // ensure a compressed version of URI is available final Object generic = encodeIdentifier(uri); writer.write(generic, encoder); return stream.toByteArray(); } catch (final IOException ex) { throw new Error("Unexpected exception (!): " + ex.getMessage(), ex); } }
private Consumer<IndexedRecord> getWritingConsumer(Encoder[] encoder) { return new Consumer<IndexedRecord>() { GenericDatumWriter<IndexedRecord> writer = null; @Override public void accept(IndexedRecord ir) { if (writer == null) { writer = new GenericDatumWriter<>(ir.getSchema()); try { if (json) { encoder[0] = EncoderFactory.get().jsonEncoder(ir.getSchema(), output); } else { encoder[0] = EncoderFactory.get().binaryEncoder(output, null); } } catch (IOException ioe) { throw new RuntimeException(ioe); } } writeIndexedRecord(writer, encoder[0], ir); } }; }
/** * Write double. Users can cast long, float and integer to double. * */ protected void writeDouble(Object datum, Encoder out) throws IOException { double num; if (datum instanceof Integer) { num = ((Integer) datum).doubleValue(); } else if (datum instanceof Long) { num = ((Long) datum).doubleValue(); } else if (datum instanceof Float) { num = ((Float) datum).doubleValue(); } else if (datum instanceof Double) { num = (Double) datum; } else throw new IOException("Cannot convert to double:" + datum.getClass()); out.writeDouble(num); }
public void create(String dirname, String filename, GenericData.Record[] records) throws Exception { Schema schema = records[0].getSchema(); File dir = new File(tmpFolder.getRoot(), dirname); if (!dir.exists()) { dir.mkdirs(); } File file = new File(dir, filename); DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<GenericRecord>(schema) { @Override protected void writeBytes(Object datum, Encoder out) throws IOException { super.writeBytes(ByteBuffer.wrap((byte[]) datum), out); } }; DataFileWriter<GenericRecord> dataFileWriter = new DataFileWriter<GenericRecord>(datumWriter); dataFileWriter.create(schema, file); TestSchema.append(dataFileWriter, records); dataFileWriter.close(); }
@Override @SuppressWarnings({ "rawtypes", "unchecked" }) protected void writeMap(Schema schema, Object datum, Encoder out) throws IOException { if (writeDirtyBits) { // write extra state information for maps StatefulMap<Utf8, ?> map = (StatefulMap) datum; out.writeInt(map.states().size()); for (Entry<Utf8, State> e2 : map.states().entrySet()) { out.writeString(e2.getKey()); out.writeInt(e2.getValue().ordinal()); } } super.writeMap(schema, datum, out); }
@Override public byte[] serializeColumnValueToBytes(String fieldName, Object columnValue) { Field field = avroSchema.getAvroSchema().getField(fieldName); DatumWriter<Object> datumWriter = fieldDatumWriters.get(fieldName); if (field == null) { throw new SchemaValidationException("Invalid field name " + fieldName + " for schema " + avroSchema.toString()); } if (datumWriter == null) { throw new SchemaValidationException("No datum writer for field name: " + fieldName); } ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); Encoder encoder = getColumnEncoder(field.schema(), byteOut); AvroUtils.writeAvroEntity(columnValue, encoder, fieldDatumWriters.get(fieldName)); return byteOut.toByteArray(); }
@Test public void testReadEncoderOutput() throws Exception { ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); Encoder encoder = new MemcmpEncoder(byteOutputStream); encoder.writeFloat(1.1f); InputStream in = new ByteArrayInputStream(byteOutputStream.toByteArray()); Decoder decoder = new MemcmpDecoder(in); float readFloat = decoder.readFloat(); assertEquals(1.1f, readFloat, 0.0001); byteOutputStream = new ByteArrayOutputStream(); encoder = new MemcmpEncoder(byteOutputStream); encoder.writeDouble(1.1d); in = new ByteArrayInputStream(byteOutputStream.toByteArray()); decoder = new MemcmpDecoder(in); double readDouble = decoder.readDouble(); assertEquals(1.1d, readDouble, 0.0001); byteOutputStream = new ByteArrayOutputStream(); encoder = new MemcmpEncoder(byteOutputStream); encoder.writeString("hello there"); in = new ByteArrayInputStream(byteOutputStream.toByteArray()); decoder = new MemcmpDecoder(in); Utf8 readString = decoder.readString(null); assertEquals("hello there", readString.toString()); }
private void writeContainerless(Record src, OutputStream dst) { try { GenericDatumWriter datumWriter = new GenericDatumWriter(); Encoder encoder = null; Schema schema = null; for (Object attachment : src.get(Fields.ATTACHMENT_BODY)) { Preconditions.checkNotNull(attachment); GenericContainer datum = (GenericContainer) attachment; schema = getSchema(datum, schema); datumWriter.setSchema(schema); if (encoder == null) { // init if (format == Format.containerlessJSON) { encoder = EncoderFactory.get().jsonEncoder(schema, dst); } else { encoder = EncoderFactory.get().binaryEncoder(dst, null); } } datumWriter.write(datum, encoder); } encoder.flush(); } catch (IOException e) { throw new MorphlineRuntimeException(e); } }
/** * Performs a deep copy of a avro node message. This does not reuse existing * decoders/encoders, so if you're going to do a lot of copying, use a NodeCopier. * * @param node The node to copy. * @return A copy of the node. */ public static Node copy(Node node) { try { ByteArrayOutputStream out = new ByteArrayOutputStream(); Encoder encoder = EncoderFactory.get().directBinaryEncoder(out, null); SpecificDatumWriter<Node> writer = new SpecificDatumWriter<Node>(Node.SCHEMA$); writer.write(node, encoder); SpecificDatumReader<Node> reader = new SpecificDatumReader<Node>(Node.SCHEMA$); Decoder decoder = DecoderFactory.get().binaryDecoder( new ByteArrayInputStream(out.toByteArray()), null); return reader.read(null, decoder); } catch (IOException e) { throw new RuntimeException("Unable to copy node."); } }
@Test public void union() throws Exception { Schema schema = Schemas.simpleUnion(); Record avro = new GenericRecordBuilder(schema).set("id", 1L).set("str", "hello").build(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Encoder encoder = new JasvornoEncoder(schema, outputStream); DatumWriter<Record> datumWriter = new GenericDatumWriter<Record>(schema); datumWriter.write(avro, encoder); encoder.flush(); assertThat(new String(outputStream.toByteArray()), is("{\"id\":1,\"str\":\"hello\"}")); outputStream.close(); }
@Test public void unionNull() throws Exception { Schema schema = Schemas.simpleUnion(); Record avro = new GenericRecordBuilder(schema).set("id", 1L).set("str", null).build(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Encoder encoder = new JasvornoEncoder(schema, outputStream); DatumWriter<Record> datumWriter = new GenericDatumWriter<Record>(schema); datumWriter.write(avro, encoder); encoder.flush(); assertThat(new String(outputStream.toByteArray()), is("{\"id\":1,\"str\":null}")); outputStream.close(); }
@Test public void bytes() throws Exception { Schema schema = Schemas.bytes(); Record avro = new GenericRecordBuilder(schema).set("str", ByteBuffer.wrap(new byte[] { 0x0, 0x1, 0x2 })).build(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Encoder encoder = new JasvornoEncoder(schema, outputStream); DatumWriter<Record> datumWriter = new GenericDatumWriter<Record>(schema); datumWriter.write(avro, encoder); encoder.flush(); assertThat(new String(outputStream.toByteArray()), is("{\"str\":\"\\u0000\\u0001\\u0002\"}")); outputStream.close(); }
/** Tests the reader/writer compatibility at decoding time. */ @Test public void testReaderWriterDecodingCompatibility() throws Exception { for (DecodingTestCase testCase : DECODING_COMPATIBILITY_TEST_CASES) { final Schema readerSchema = testCase.getReaderSchema(); final Schema writerSchema = testCase.getWriterSchema(); final Object datum = testCase.getDatum(); final Object expectedDecodedDatum = testCase.getDecodedDatum(); LOG.debug( "Testing incompatibility of reader {} with writer {}.", readerSchema, writerSchema); LOG.debug("Encode datum {} with writer {}.", datum, writerSchema); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final Encoder encoder = EncoderFactory.get().binaryEncoder(baos, null); final DatumWriter<Object> datumWriter = new GenericDatumWriter<Object>(writerSchema); datumWriter.write(datum, encoder); encoder.flush(); LOG.debug( "Decode datum {} whose writer is {} with reader {}.", new Object[]{datum, writerSchema, readerSchema}); final byte[] bytes = baos.toByteArray(); final Decoder decoder = DecoderFactory.get().resolvingDecoder( writerSchema, readerSchema, DecoderFactory.get().binaryDecoder(bytes, null)); final DatumReader<Object> datumReader = new GenericDatumReader<Object>(readerSchema); final Object decodedDatum = datumReader.read(null, decoder); assertEquals(String.format( "Expecting decoded value %s when decoding value %s whose writer schema is %s " + "using reader schema %s, but value was %s.", expectedDecodedDatum, datum, writerSchema, readerSchema, decodedDatum), expectedDecodedDatum, decodedDatum); } }
@SuppressWarnings("unchecked") public static byte[] serialize(Object datum, Schema schema) { ByteArrayOutputStream out = new ByteArrayOutputStream(); Encoder encoder = EncoderFactory.get().binaryEncoder(out, null); ReflectDatumWriter writer = new ReflectDatumWriter(schema); try { writer.write(datum, encoder); encoder.flush(); } catch (IOException ex) { Throwables.propagate(ex); } return out.toByteArray(); }
private byte[] encodeAvroGenericRecord(Schema schema, GenericRecord record) throws IOException { DatumWriter<IndexedRecord> msgDatumWriter = new GenericDatumWriter<>(schema); ByteArrayOutputStream os = new ByteArrayOutputStream(); Encoder encoder = EncoderFactory.get().binaryEncoder(os, null); msgDatumWriter.write(record, encoder); encoder.flush(); return os.toByteArray(); }
public static byte[] dataToByteArray(Schema schema, GenericRecord datum) throws IOException { GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(schema); ByteArrayOutputStream os = new ByteArrayOutputStream(); try { Encoder e = EncoderFactory.get().binaryEncoder(os, null); writer.write(datum, e); e.flush(); byte[] byteData = os.toByteArray(); return byteData; } finally { os.close(); } }
protected Encoder createEncoder() throws IOException { switch(codecType) { case BINARY: return EncoderFactory.get().binaryEncoder(getOrCreateOutputStream(), null); case JSON: return EncoderFactory.get().jsonEncoder(schema, getOrCreateOutputStream()); } return null; }
/** * Writes a boolean[] to the output. */ public static void writeBoolArray(Encoder out, boolean[] boolArray) throws IOException { out.writeInt(boolArray.length); int byteArrLength = (int)Math.ceil(boolArray.length / 8.0); byte b = 0; byte[] arr = new byte[byteArrLength]; int i = 0; int arrIndex = 0; for(i=0; i<boolArray.length; i++) { if(i % 8 == 0 && i != 0) { arr[arrIndex++] = b; b = 0; } b >>= 1; if(boolArray[i]) b |= 0x80; else b &= 0x7F; } if(i % 8 != 0) { for(int j=0; j < 8 - (i % 8); j++) { //shift for the remaining byte b >>=1; b &= 0x7F; } } arr[arrIndex++] = b; out.writeFixed(arr); }
public static String jsonFromGenericRecord(GenericRecord record) { ByteArrayOutputStream out = new ByteArrayOutputStream(); GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<>(DefaultTopicSchema.MESSAGE_V0); try { Encoder encoder = new JsonEncoder(DefaultTopicSchema.MESSAGE_V0, out); writer.write(record, encoder); encoder.flush(); } catch (IOException e) { LOG.error("Unable to serialize avro record due to error " + e); } return out.toString(); }
@Override protected void process() throws Exception { InputStream avscInput = new FileInputStream("data/twitter.avsc"); Schema.Parser parser = new Schema.Parser(); Schema schema = parser.parse(avscInput); final StreamingOutput<OutputTuple> out = getOutput(0); /* Serializing to a byte array */ for (int i = 0; i < 100; i++) { GenericRecord producedDatum = new GenericData.Record(schema); producedDatum.put("username", "Frank"); producedDatum.put("tweet", "This Avro message really rocks: " + i); producedDatum.put("timestamp", new Long(1048298232L + i)); GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(schema); ByteArrayOutputStream os = new ByteArrayOutputStream(); Encoder e = EncoderFactory.get().binaryEncoder(os, null); writer.write(producedDatum, e); e.flush(); byte[] byteData = os.toByteArray(); os.close(); /* Now submit tuple */ OutputTuple tuple = out.newTuple(); Blob blobData = ValueFactory.newBlob(byteData); tuple.setBlob(0, blobData); out.submit(tuple); } // Make the set of tuples a window. out.punctuate(Punctuation.WINDOW_MARKER); }