本文整理匯總了Java中org.apache.avro.specific.SpecificDatumWriter類的典型用法代碼示例。如果您正苦於以下問題:Java SpecificDatumWriter類的具體用法?Java SpecificDatumWriter怎麽用?Java SpecificDatumWriter使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
SpecificDatumWriter類屬於org.apache.avro.specific包,在下文中一共展示了SpecificDatumWriter類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: encode
import org.apache.avro.specific.SpecificDatumWriter; //導入依賴的package包/類
/**
* Encode the message data provided.
*
* @param <O> The type of the data to encode.
* @param data The message data.
* @throws EncodeMessageContentException Exception thrown if encoding the message content fails.
*/
public <O extends GenericContainer> void encode(O data) throws EncodeMessageContentException {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
this.schema = data.getSchema();
DatumWriter<O> outputDatumWriter = new SpecificDatumWriter<O>(this.schema);
BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(baos, null);
outputDatumWriter.write(data, encoder);
encoder.flush();
this.data = baos.toByteArray();
} catch (Exception ex) {
throw new EncodeMessageContentException(ex);
}
}
示例2: jsonReadWriteExample
import org.apache.avro.specific.SpecificDatumWriter; //導入依賴的package包/類
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);
}
示例3: binaryReadWriteExample
import org.apache.avro.specific.SpecificDatumWriter; //導入依賴的package包/類
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);
}
示例4: serializeEvent
import org.apache.avro.specific.SpecificDatumWriter; //導入依賴的package包/類
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;
}
示例5: serializeValue
import org.apache.avro.specific.SpecificDatumWriter; //導入依賴的package包/類
private byte[] serializeValue(Event event, boolean parseAsFlumeEvent) throws IOException {
byte[] bytes;
if (parseAsFlumeEvent) {
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;
}
示例6: main
import org.apache.avro.specific.SpecificDatumWriter; //導入依賴的package包/類
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);
}
示例7: main
import org.apache.avro.specific.SpecificDatumWriter; //導入依賴的package包/類
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);
}
示例8: serialize
import org.apache.avro.specific.SpecificDatumWriter; //導入依賴的package包/類
@Override
public byte[] serialize(String s, WrapperAppMessage wrapperAppMessage) {
DatumWriter<WrapperAppMessage> datumWriter = new SpecificDatumWriter<>(wrapperAppMessage.getSchema());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
BinaryEncoder binaryEncoder = EncoderFactory.get().binaryEncoder(outputStream, null);
try {
datumWriter.write(wrapperAppMessage, binaryEncoder);
binaryEncoder.flush();//帶緩衝區的binaryEncoder和直接directBinaryEncoder不一樣,需要flush一下,否則字節數組沒有數據
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
byte[] data = outputStream.toByteArray();
return data;
}
示例9: MemberInfoDynSer
import org.apache.avro.specific.SpecificDatumWriter; //導入依賴的package包/類
/**
* 動態序列化:通過動態解析Schema文件進行內容設置,並序列化內容
*
* @throws IOException
*/
public void MemberInfoDynSer() throws IOException {
// 1.解析schema文件內容
Parser parser = new Parser();
Schema mSchema = parser.parse(this.getClass().getResourceAsStream("/Members.avsc"));
// 2.構建數據寫對象
DatumWriter<GenericRecord> mGr = new SpecificDatumWriter<GenericRecord>(mSchema);
DataFileWriter<GenericRecord> mDfw = new DataFileWriter<GenericRecord>(mGr);
// 3.創建序列化文件
mDfw.create(mSchema, new File("/Users/a/Desktop/tmp/members.avro"));
// 4.添加序列化數據
for (int i = 0; i < 20; i++) {
GenericRecord gr = new GenericData.Record(mSchema);
int r = i * new Random().nextInt(50);
gr.put("userName", "light-" + r);
gr.put("userPwd", "2016-" + r);
gr.put("realName", "滔滔" + r + "號");
mDfw.append(gr);
}
// 5.關閉數據文件寫對象
mDfw.close();
System.out.println("Dyn Builder Ser Start Complete.");
}
示例10: 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);
}
示例11: 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);
}
示例12: toBytes
import org.apache.avro.specific.SpecificDatumWriter; //導入依賴的package包/類
@SuppressWarnings("unchecked")
public static <T> byte[] toBytes(T o, Schema schema
, SpecificDatumWriter<T> datumWriter)
throws IOException {
Type type = schema.getType();
switch (type) {
case STRING: return toBytes(((Utf8)o).toString()); // TODO: maybe ((Utf8)o).getBytes(); ?
case BYTES: return ((ByteBuffer)o).array();
case INT: return vintToBytes((Integer)o);
case LONG: return vintToBytes((Long)o);
case FLOAT: return toBytes((Float)o);
case DOUBLE: return toBytes((Double)o);
case BOOLEAN: return (Boolean)o ? new byte[] {1} : new byte[] {0};
case ENUM: return new byte[] { (byte)((Enum<?>) o).ordinal() };
case RECORD: //fall
case MAP:
case ARRAY: return IOUtils.serialize((SpecificDatumWriter<SpecificRecord>)datumWriter, schema, (SpecificRecord)o);
default: throw new RuntimeException("Unknown type: "+type);
}
}
示例13: initialize
import org.apache.avro.specific.SpecificDatumWriter; //導入依賴的package包/類
@Override
public void initialize(Class<K> keyClass, Class<T> persistentClass,
Properties properties) {
setKeyClass(keyClass);
setPersistentClass(persistentClass);
if (this.beanFactory == null) {
this.beanFactory = new BeanFactoryImpl<>(keyClass, persistentClass);
}
schema = this.beanFactory.getCachedPersistent().getSchema();
fieldMap = AvroUtils.getFieldMap(schema);
autoCreateSchema = DataStoreFactory.getAutoCreateSchema(properties, this);
this.properties = properties;
datumReader = new SpecificDatumReader<>(schema);
datumWriter = new SpecificDatumWriter<>(schema);
}
示例14: initializeAvro
import org.apache.avro.specific.SpecificDatumWriter; //導入依賴的package包/類
private void initializeAvro() {
final ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (SpecificRecord.class.isAssignableFrom(type)) {
this.avroData = new SpecificData(cl);
this.schema = this.avroData.getSchema(type);
this.reader = new SpecificDatumReader<>(schema, schema, avroData);
this.writer = new SpecificDatumWriter<>(schema, avroData);
}
else {
final ReflectData reflectData = new ReflectData(cl);
this.avroData = reflectData;
this.schema = this.avroData.getSchema(type);
this.reader = new ReflectDatumReader<>(schema, schema, reflectData);
this.writer = new ReflectDatumWriter<>(schema, reflectData);
}
this.encoder = new DataOutputEncoder();
this.decoder = new DataInputDecoder();
}
示例15: toByteArray
import org.apache.avro.specific.SpecificDatumWriter; //導入依賴的package包/類
/**
* To byte array.
*
* @param iote2eRequest the iote 2 e request
* @return the byte[]
* @throws Exception the exception
*/
public byte[] toByteArray( Iote2eRequest iote2eRequest ) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
if( this.datumWriterIote2eRequest == null ) {
this.datumWriterIote2eRequest = new SpecificDatumWriter<Iote2eRequest>(Iote2eRequest.getClassSchema());
}
this.binaryEncoder = EncoderFactory.get().binaryEncoder(baos, this.binaryEncoder );
this.datumWriterIote2eRequest.write(iote2eRequest, this.binaryEncoder );
this.binaryEncoder.flush();
this.bytes = baos.toByteArray();
return this.bytes;
} catch (Exception e) {
logger.error(e.getMessage(),e);
throw e;
} finally {
baos.close();
}
}