Refine search
@SuppressWarnings("unchecked") private void readObject(ObjectInputStream inputStream) throws ClassNotFoundException, IOException { recordClazz = (Class<? extends SpecificRecord>) inputStream.readObject(); schemaString = (String) inputStream.readObject(); if (recordClazz != null) { schema = SpecificData.get().getSchema(recordClazz); } else { schema = new Schema.Parser().parse(schemaString); } datumWriter = new SpecificDatumWriter<>(schema); arrayOutputStream = new ByteArrayOutputStream(); encoder = EncoderFactory.get().binaryEncoder(arrayOutputStream, null); } }
private byte[] serializeEvent(Event event, boolean useAvroEventFormat) throws IOException { byte[] bytes; if (useAvroEventFormat) { if (!tempOutStream.isPresent()) { tempOutStream = Optional.of(new ByteArrayOutputStream()); } if (!writer.isPresent()) { writer = Optional.of(new SpecificDatumWriter<AvroFlumeEvent>(AvroFlumeEvent.class)); } tempOutStream.get().reset(); AvroFlumeEvent e = new AvroFlumeEvent(toCharSeqMap(event.getHeaders()), ByteBuffer.wrap(event.getBody())); encoder = EncoderFactory.get().directBinaryEncoder(tempOutStream.get(), encoder); writer.get().write(e, encoder); encoder.flush(); bytes = tempOutStream.get().toByteArray(); } else { bytes = event.getBody(); } return bytes; }
@Override public void writeExternal(ObjectOutput out) throws IOException { new SpecificDatumWriter(getSchema()) .write(this, SpecificData.getEncoder(out)); }
@Override public int drainTo(OutputStream target) throws IOException { int written; if (getPartial() != null) { written = (int) IoUtils.copy(getPartial(), target); } else { Schema reqSchema = message.getRequest(); CountingOutputStream outputStream = new CountingOutputStream(target); BinaryEncoder out = ENCODER_FACTORY.binaryEncoder(outputStream, null); int i = 0; for (Schema.Field param : reqSchema.getFields()) { new SpecificDatumWriter<>(param.schema()).write(args[i++], out); } out.flush(); args = null; written = outputStream.getWrittenCount(); } return written; } }
protected static byte[] serializeAvroRecord(SpecificDatumWriter<GenericRecord> writer, GenericRecord record) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); Encoder encoder = EncoderFactory.get().directBinaryEncoder(out, null); writer.write(record, encoder); encoder.flush(); ByteBuffer serialized = ByteBuffer.allocate(out.toByteArray().length); serialized.put(out.toByteArray()); return serialized.array(); }
private byte[] getSerializedMessage(IndexedRecord message, Schema schema) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(4096); SpecificDatumWriter<IndexedRecord> writer = new SpecificDatumWriter<>(); DataFileWriter<IndexedRecord> dfw = null; try { dfw = new DataFileWriter<>(writer).create(schema, baos); dfw.append(message); } finally { if (dfw != null) { dfw.close(); } } return baos.toByteArray(); }
/** * Convert AvroHttpRequest to JSON String. */ public String toString(final AvroHttpRequest request) { final DatumWriter<AvroHttpRequest> configurationWriter = new SpecificDatumWriter<>(AvroHttpRequest.class); try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) { final JsonEncoder encoder = EncoderFactory.get().jsonEncoder(AvroHttpRequest.SCHEMA$, out); configurationWriter.write(request, encoder); encoder.flush(); return out.toString(JSON_CHARSET); } catch (final IOException e) { throw new RuntimeException(e); } }
/** * Encode record to Json and then convert to byte array. * * @param record the object to encode * @return the byte[] * @throws IOException Signals that an I/O exception has occurred. */ public byte[] encodeToJsonBytes(T record) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); jsonEncoder = EncoderFactory.get().jsonEncoder(this.schema, baos, true); avroWriter.write(record, jsonEncoder); jsonEncoder.flush(); baos.flush(); return baos.toByteArray(); }
public ByteBuffer serializeDatumEnvelope(final DatumEnvelope envelope) { try { final SpecificDatumWriter<DatumEnvelope> envelopeWriter = new SpecificDatumWriter<>(envelope.getSchema()); final ByteArrayOutputStream envelopeByteStream = new ByteArrayOutputStream(); BinaryEncoder binaryEncoder = EncoderFactory.get().directBinaryEncoder(envelopeByteStream, null); binaryEncoder = EncoderFactory.get().directBinaryEncoder(envelopeByteStream, binaryEncoder); envelopeWriter.write(envelope, binaryEncoder); binaryEncoder.flush(); envelopeByteStream.flush(); return ByteBuffer.wrap(envelopeByteStream.toByteArray()); } catch (final Exception e) { throw new RuntimeException("Could not serialize datum envelope", e); } }
@Override public byte[] write(@NonNull final GenericRecord record) throws InvalidDataException { final SpecificDatumWriter<GenericRecord> datumWriter = new SpecificDatumWriter<>(getSchema()); final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byteArrayOutputStream.reset(); final BinaryEncoder binaryEncoder = new EncoderFactory().binaryEncoder(byteArrayOutputStream, null); try { datumWriter.write(record, binaryEncoder); binaryEncoder.flush(); } catch (Exception e) { throw new InvalidDataException("Error encoding record", e); } return byteArrayOutputStream.toByteArray(); } }
public static String toString(final SpecificRecord record) { final String jsonEncodedRecord; try { final Schema schema = record.getSchema(); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final Encoder encoder = EncoderFactory.get().jsonEncoder(schema, bos); final SpecificDatumWriter datumWriter = new SpecificDatumWriter(record.getClass()); datumWriter.write(record, encoder); encoder.flush(); jsonEncodedRecord = new String(bos.toByteArray(), Charset.forName("UTF-8")); } catch (final IOException e) { throw new RuntimeException(e); } return jsonEncodedRecord; }
private byte[] serialize(Map<String, Object> data) throws IOException { data = (data == null) ? Collections.emptyMap() : data; BulletAvro record = new BulletAvro(data); ByteArrayOutputStream stream = new ByteArrayOutputStream(2048); EncoderFactory encoderFactory = new EncoderFactory(); Encoder encoder = encoderFactory.binaryEncoder(stream, null); WRITER.write(record, encoder); encoder.flush(); return stream.toByteArray(); }
/** * * * * @param avroObject Avro object to serialized * @return byte array of serialized avro data * @throws IOException */ private byte[] serializeAvroSimpleFeature(final AvroSimpleFeature avroObject) throws IOException { final ByteArrayOutputStream os = new ByteArrayOutputStream(); final BinaryEncoder encoder = ef.binaryEncoder(os, null); datumWriter.setSchema(avroObject.getSchema()); datumWriter.write(avroObject, encoder); encoder.flush(); return os.toByteArray(); }
public AvroSerializer(Schema schema, SchemaVersionWriter schemaVersionWriter) throws IOException { this.closer = Closer.create(); this.byteArrayOutputStream = new ByteArrayOutputStream(); this.out = this.closer.register(new DataOutputStream(this.byteArrayOutputStream)); this.encoder = getEncoder(schema, this.out); this.schemaVersionWriter = schemaVersionWriter; this.writer = new SpecificDatumWriter<>(schema); }
@Test public void testResolveUnion() throws IOException { final SpecificDatumWriter<TestRecordWithUnion> writer = new SpecificDatumWriter<>(); Schema schema = TestRecordWithUnion.SCHEMA$; ByteArrayOutputStream out = new ByteArrayOutputStream(); JsonEncoder encoder = EncoderFactory.get().jsonEncoder(schema, out); writer.setSchema(schema); TestRecordWithUnion c = TestRecordWithUnion.newBuilder(). setKind(Kind.BAR).setValue("rab").build(); writer.write(c, encoder); encoder.flush(); out.close(); String expectedJson = String.format( "{'kind':{'org.apache.avro.test.Kind':'%s'},'value':{'string':'%s'}}", c.getKind().toString(), c.getValue()).replace('\'', '"'); assertEquals(expectedJson, out.toString("UTF-8")); }
/** Tests that non Stringable datum are rejected by specific writers. */ @Test public void testNonStringable() throws Exception { final Schema string = Schema.create(Type.STRING); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final Encoder encoder = EncoderFactory.get().directBinaryEncoder(baos, null); final DatumWriter<Object> writer = new SpecificDatumWriter<>(string); try { writer.write(new Object(), encoder); fail("Non stringable object should be rejected."); } catch (ClassCastException cce) { // Expected error } } }
/** * Converts a {@link org.apache.gobblin.metrics.MetricReport} to bytes to send through Kafka. * * <p> * Actual serialized record will be prepended with a schema version generated by {@link #schemaVersionWriter}. * </p> * * @param record MetricReport to serialize. * @return Serialized bytes. */ public synchronized byte[] serializeRecord(T record) { try { this.byteArrayOutputStream.reset(); // Write schema versioning information. this.schemaVersionWriter.writeSchemaVersioningInformation(record.getSchema(), this.out); // Now write the record itself. this.writer.write(record, this.encoder); this.encoder.flush(); return this.byteArrayOutputStream.toByteArray(); } catch (IOException exception) { LOGGER.warn("Could not serialize Avro record for Kafka Metrics.", exception); return null; } }
.build(); DatumWriter<HandshakeRequest> handshakeWriter = new SpecificDatumWriter<>(HandshakeRequest.class); BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(byteBufferOutputStream, null);
@Override public DatumWriter createDatumWriter(Schema schema) { return new SpecificDatumWriter(schema, this); }
/** * Serializes the field object using the datumWriter. * * @param <T> class type of object to be serialized. * @param datumWriter AVRO datum writer for given schema. * @param object object to be serialized. * @param os output stream which serialized content is written. * @throws IOException occurred while serializing the object to bytes. */ public static<T> void serialize(OutputStream os, SpecificDatumWriter<T> datumWriter, T object) throws IOException { BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(os, null); datumWriter.write(object, encoder); encoder.flush(); }