本文整理汇总了Java中org.apache.avro.io.EncoderFactory类的典型用法代码示例。如果您正苦于以下问题:Java EncoderFactory类的具体用法?Java EncoderFactory怎么用?Java EncoderFactory使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
EncoderFactory类属于org.apache.avro.io包,在下文中一共展示了EncoderFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: convertAvroToJson
import org.apache.avro.io.EncoderFactory; //导入依赖的package包/类
static void convertAvroToJson(InputStream inputStream, OutputStream outputStream, Schema schema)
throws IOException {
DatumReader<Object> reader = new GenericDatumReader<>(schema);
DatumWriter<Object> writer = new GenericDatumWriter<>(schema);
BinaryDecoder binaryDecoder = DecoderFactory.get().binaryDecoder(inputStream, null);
JsonEncoder jsonEncoder = EncoderFactory.get().jsonEncoder(schema, outputStream, true);
Object datum = null;
while (!binaryDecoder.isEnd()) {
datum = reader.read(datum, binaryDecoder);
writer.write(datum, jsonEncoder);
jsonEncoder.flush();
}
outputStream.flush();
}
示例2: encode
import org.apache.avro.io.EncoderFactory; //导入依赖的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);
}
}
示例3: AvroFileInputStream
import org.apache.avro.io.EncoderFactory; //导入依赖的package包/类
public AvroFileInputStream(FileStatus status) throws IOException {
pos = 0;
buffer = new byte[0];
GenericDatumReader<Object> reader = new GenericDatumReader<Object>();
FileContext fc = FileContext.getFileContext(new Configuration());
fileReader =
DataFileReader.openReader(new AvroFSInput(fc, status.getPath()),reader);
Schema schema = fileReader.getSchema();
writer = new GenericDatumWriter<Object>(schema);
output = new ByteArrayOutputStream();
JsonGenerator generator =
new JsonFactory().createJsonGenerator(output, JsonEncoding.UTF8);
MinimalPrettyPrinter prettyPrinter = new MinimalPrettyPrinter();
prettyPrinter.setRootValueSeparator(System.getProperty("line.separator"));
generator.setPrettyPrinter(prettyPrinter);
encoder = EncoderFactory.get().jsonEncoder(schema, generator);
}
示例4: jsonReadWriteExample
import org.apache.avro.io.EncoderFactory; //导入依赖的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);
}
示例5: binaryReadWriteExample
import org.apache.avro.io.EncoderFactory; //导入依赖的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);
}
示例6: processSinglex
import org.apache.avro.io.EncoderFactory; //导入依赖的package包/类
/**
* Process singlex.
*
* @throws Exception the exception
*/
public void processSinglex() throws Exception {
int base = (int) System.currentTimeMillis();
User user = User.newBuilder().setName("name" + base).setFavoriteColor("color" + base).setFavoriteNumber(base)
.build();
DatumWriter<GenericRecord> datumWriterUser = new GenericDatumWriter<GenericRecord>(User.getClassSchema());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] byteData = null;
try {
BinaryEncoder binaryEncoder = EncoderFactory.get().binaryEncoder(baos, null);
datumWriterUser.write(user, binaryEncoder);
binaryEncoder.flush();
byteData = baos.toByteArray();
} finally {
baos.close();
}
System.out.println(byteData.length);
DatumReader<GenericRecord> datumReaderUser = new GenericDatumReader<GenericRecord>( User.getClassSchema());
GenericRecord genericRecord = datumReaderUser.read(null, DecoderFactory.get().binaryDecoder(byteData, null) );
System.out.println(genericRecord);
System.out.println( genericRecord.get("name"));
}
示例7: getJsonStringFromRecord
import org.apache.avro.io.EncoderFactory; //导入依赖的package包/类
@Override
public String getJsonStringFromRecord(RecordField field)
throws AvroUiSandboxServiceException {
try {
GenericRecord record = FormAvroConverter.createGenericRecordFromRecordField(field);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JsonGenerator jsonGenerator
= new JsonFactory().createJsonGenerator(baos, JsonEncoding.UTF8);
jsonGenerator.useDefaultPrettyPrinter();
JsonEncoder jsonEncoder = EncoderFactory.get().jsonEncoder(record.getSchema(), jsonGenerator);
DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<GenericRecord>(record.getSchema());
datumWriter.write(record, jsonEncoder);
jsonEncoder.flush();
baos.flush();
return new String(baos.toByteArray(), UTF8);
} catch (Exception e) {
throw Utils.handleException(e);
}
}
示例8: serialize
import org.apache.avro.io.EncoderFactory; //导入依赖的package包/类
@Override
public byte[] serialize(String topic, T payload) {
try {
byte[] result = null;
if (payload != null) {
LOGGER.debug("data='{}'", payload);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
BinaryEncoder binaryEncoder = EncoderFactory.get().binaryEncoder(byteArrayOutputStream, null);
DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(payload.getSchema());
datumWriter.write(payload, binaryEncoder);
binaryEncoder.flush();
byteArrayOutputStream.close();
result = byteArrayOutputStream.toByteArray();
LOGGER.debug("serialized data='{}'", DatatypeConverter.printHexBinary(result));
}
return result;
} catch (IOException ex) {
throw new SerializationException("Can't serialize payload='" + payload + "' for topic='" + topic + "'", ex);
}
}
示例9: initialize
import org.apache.avro.io.EncoderFactory; //导入依赖的package包/类
private void initialize() throws IOException, NoSuchAlgorithmException {
SeekableResettableInputBridge in = new SeekableResettableInputBridge(ris);
long pos = in.tell();
in.seek(0L);
fileReader = new DataFileReader<GenericRecord>(in,
new GenericDatumReader<GenericRecord>());
fileReader.sync(pos);
schema = fileReader.getSchema();
datumWriter = new GenericDatumWriter(schema);
out = new ByteArrayOutputStream();
encoder = EncoderFactory.get().binaryEncoder(out, encoder);
schemaHash = SchemaNormalization.parsingFingerprint("CRC-64-AVRO", schema);
schemaHashString = Hex.encodeHexString(schemaHash);
}
示例10: serializeEvent
import org.apache.avro.io.EncoderFactory; //导入依赖的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;
}
示例11: serializeValue
import org.apache.avro.io.EncoderFactory; //导入依赖的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;
}
示例12: serialize
import org.apache.avro.io.EncoderFactory; //导入依赖的package包/类
private byte[] serialize(Object datum, Schema datumSchema) throws FlumeException {
if (schema == null || !datumSchema.equals(schema)) {
schema = datumSchema;
out = new ByteArrayOutputStream();
writer = new ReflectDatumWriter<Object>(schema);
encoder = EncoderFactory.get().binaryEncoder(out, null);
}
out.reset();
try {
writer.write(datum, encoder);
encoder.flush();
return out.toByteArray();
} catch (IOException e) {
throw new FlumeException(e);
}
}
示例13: main
import org.apache.avro.io.EncoderFactory; //导入依赖的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);
}
示例14: main
import org.apache.avro.io.EncoderFactory; //导入依赖的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 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);
}
示例15: main
import org.apache.avro.io.EncoderFactory; //导入依赖的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);
}