本文整理匯總了Java中org.apache.avro.specific.SpecificDatumWriter.write方法的典型用法代碼示例。如果您正苦於以下問題:Java SpecificDatumWriter.write方法的具體用法?Java SpecificDatumWriter.write怎麽用?Java SpecificDatumWriter.write使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.avro.specific.SpecificDatumWriter
的用法示例。
在下文中一共展示了SpecificDatumWriter.write方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: send
import org.apache.avro.specific.SpecificDatumWriter; //導入方法依賴的package包/類
public void send(COL_RDBMS event) throws Exception {
EncoderFactory avroEncoderFactory = EncoderFactory.get();
SpecificDatumWriter<COL_RDBMS> avroEventWriter = new SpecificDatumWriter<COL_RDBMS>(COL_RDBMS.SCHEMA$);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
BinaryEncoder binaryEncoder = avroEncoderFactory.binaryEncoder(stream,null);
try {
avroEventWriter.write(event, binaryEncoder);
binaryEncoder.flush();
} catch (IOException e) {
e.printStackTrace();
throw e;
}
IOUtils.closeQuietly(stream);
KeyedMessage<String, byte[]> data = new KeyedMessage<String, byte[]>(
TOPIC, stream.toByteArray());
producer.send(data);
}
示例2: send
import org.apache.avro.specific.SpecificDatumWriter; //導入方法依賴的package包/類
public void send(COL_ONEM2M event) throws Exception {
EncoderFactory avroEncoderFactory = EncoderFactory.get();
SpecificDatumWriter<COL_ONEM2M> avroEventWriter = new SpecificDatumWriter<COL_ONEM2M>(COL_ONEM2M.SCHEMA$);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
BinaryEncoder binaryEncoder = avroEncoderFactory.binaryEncoder(stream,null);
try {
avroEventWriter.write(event, binaryEncoder);
binaryEncoder.flush();
} catch (IOException e) {
e.printStackTrace();
throw e;
}
IOUtils.closeQuietly(stream);
KeyedMessage<String, byte[]> data = new KeyedMessage<String, byte[]>(
TOPIC, stream.toByteArray());
producer.send(data);
}
示例3: serializeDatum
import org.apache.avro.specific.SpecificDatumWriter; //導入方法依賴的package包/類
public SerializedDatum serializeDatum(final TDomainClass domainObject) {
try {
final SpecificRecord record = avroRoundTripProjector.toAvro(domainObject);
final Schema schema = record.getSchema();
final SpecificDatumWriter bodyWriter = new SpecificDatumWriter(schema);
final ByteArrayOutputStream bodyByteStream = new ByteArrayOutputStream();
final BinaryEncoder binaryEncoder = EncoderFactory.get().directBinaryEncoder(bodyByteStream, null);
bodyWriter.write(record, binaryEncoder);
binaryEncoder.flush();
bodyByteStream.flush();
final ByteBuffer datumBody = ByteBuffer.wrap(bodyByteStream.toByteArray());
bodyByteStream.close();
final int datumSchemaVersion = datumSchemaRepository.getDatumTypeVersion(schema).getVersion();
return new SerializedDatum(datumBody, new DatumTypeVersion(datumTypeId, datumSchemaVersion));
} catch (final Exception e) {
throw new RuntimeException("Could not create datum body", e);
}
}
示例4: serializeDatumEnvelope
import org.apache.avro.specific.SpecificDatumWriter; //導入方法依賴的package包/類
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);
}
}
示例5: getInsertMessageBytes
import org.apache.avro.specific.SpecificDatumWriter; //導入方法依賴的package包/類
private byte[] getInsertMessageBytes(InsertMessage insertMessage){
byte[] result = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out, null);
SpecificDatumWriter writer = new SpecificDatumWriter<InsertMessage>(InsertMessage.getClassSchema());
try {
writer.write(insertMessage, encoder);
encoder.flush();
out.close();
result = out.toByteArray();
}catch (IOException e){
return null;
}
return result;
}
示例6: getInsertMessageBytes
import org.apache.avro.specific.SpecificDatumWriter; //導入方法依賴的package包/類
private byte[] getInsertMessageBytes(InsertMessage insertMessage){
byte[] result = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out, null);
SpecificDatumWriter writer = new SpecificDatumWriter<InsertMessage>(InsertMessage.getClassSchema());
try {
writer.write(insertMessage, encoder);
encoder.flush();
out.close();
result = out.toByteArray();
}catch (IOException e){
return null;
}
return result;
}
示例7: deserialize
import org.apache.avro.specific.SpecificDatumWriter; //導入方法依賴的package包/類
private static <T> byte[] deserialize(
final T avroObject,
Schema avroSchema,
Class<T> avroClass )
throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
BinaryEncoder encoder = ef.binaryEncoder(
os,
null);
if (!writers.containsKey(avroClass.toString())) {
writers.put(
avroClass.toString(),
new SpecificDatumWriter<T>(
avroSchema));
}
SpecificDatumWriter<T> writer = writers.get(avroClass.toString());
writer.write(
avroObject,
encoder);
encoder.flush();
return os.toByteArray();
}
示例8: copy
import org.apache.avro.specific.SpecificDatumWriter; //導入方法依賴的package包/類
/**
* 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.");
}
}
示例9: getObject
import org.apache.avro.specific.SpecificDatumWriter; //導入方法依賴的package包/類
private DBObject getObject(Object o, Schema s){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JsonEncoder e;
try {
e = EncoderFactory.get().jsonEncoder(s, baos);
SpecificDatumWriter w = new SpecificDatumWriter(o.getClass());
w.write(o, e);
e.flush();
DBObject dbObject = (DBObject)JSON.parse(baos.toString());
return dbObject;
} catch (IOException e1) {
log.error("GET OBJECT Exception: {} {}", o.getClass(), ExceptionHandler.logAndtoString(e1,true));
}
return null;
}
示例10: serialize
import org.apache.avro.specific.SpecificDatumWriter; //導入方法依賴的package包/類
/**
* Serializes the field object using the datumWriter.
*/
public static<T extends SpecificRecord> void serialize(OutputStream os,
SpecificDatumWriter<T> datumWriter, Schema schema, T object)
throws IOException {
BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(os, null);
datumWriter.write(object, encoder);
encoder.flush();
}
示例11: getAvroBytes
import org.apache.avro.specific.SpecificDatumWriter; //導入方法依賴的package包/類
public static byte[] getAvroBytes(Map<String, Object> data) {
try {
// Keep this independent from the code
BulletAvro record = new BulletAvro(data);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
SpecificDatumWriter<BulletAvro> writer = new SpecificDatumWriter<>(BulletAvro.class);
Encoder encoder = new EncoderFactory().directBinaryEncoder(stream, null);
stream.reset();
writer.write(record, encoder);
encoder.flush();
return stream.toByteArray();
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
示例12: deserialize
import org.apache.avro.specific.SpecificDatumWriter; //導入方法依賴的package包/類
private static <T> byte[] deserialize(final T avroObject, Schema avroSchema, Class<T> avroClass) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
BinaryEncoder encoder = ef.binaryEncoder(os, null);
if (!writers.containsKey(avroClass.toString())){
writers.put(avroClass.toString(), new SpecificDatumWriter<T>(avroSchema));
}
SpecificDatumWriter<T> writer = writers.get(avroClass.toString());
writer.write(avroObject, encoder);
encoder.flush();
return os.toByteArray();
}
示例13: toBytes
import org.apache.avro.specific.SpecificDatumWriter; //導入方法依賴的package包/類
public byte[] toBytes(T object) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
SpecificDatumWriter<T> datumWriter = null;
try {
Encoder encoder = EncoderFactory.get().binaryEncoder(output, null);
datumWriter = new SpecificDatumWriter<T>(clazz);
datumWriter.write(object, encoder);
encoder.flush();
output.close();
} catch(Exception e) {
throw new SerializationException("An exception has thrown in Avro specific serialization process", e);
}
return output.toByteArray();
}
示例14: transform
import org.apache.avro.specific.SpecificDatumWriter; //導入方法依賴的package包/類
private byte[] transform(SNMPFloat snmp, String schemaName) throws Exception {
Schema schema = new Schema.Parser().parse(new FileInputStream("schemas/"+schemaName+".avsc"));
SpecificDatumWriter<SNMPFloat> writer = new SpecificDatumWriter<>(schema);
ByteArrayOutputStream os = new ByteArrayOutputStream();
os.write(1);
BinaryEncoder enc = EncoderFactory.get().binaryEncoder(os, null);
writer.write(snmp, enc);
enc.flush();
byte[] data = os.toByteArray();
return data;
}
示例15: encodeRecord
import org.apache.avro.specific.SpecificDatumWriter; //導入方法依賴的package包/類
public static byte[] encodeRecord(IndexedRecord record) throws IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
EncoderFactory encoderFactory = EncoderFactory.get();
BinaryEncoder binaryEncoder = encoderFactory.binaryEncoder(stream, null);
SpecificDatumWriter<IndexedRecord> datumWriter = new SpecificDatumWriter<>(record.getSchema());
datumWriter.write(record, binaryEncoder);
binaryEncoder.flush();
} finally {
stream.close();
}
return stream.toByteArray();
}