本文整理汇总了Java中org.apache.avro.io.Encoder.flush方法的典型用法代码示例。如果您正苦于以下问题:Java Encoder.flush方法的具体用法?Java Encoder.flush怎么用?Java Encoder.flush使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.avro.io.Encoder
的用法示例。
在下文中一共展示了Encoder.flush方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: jsonReadWriteExample
import org.apache.avro.io.Encoder; //导入方法依赖的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);
}
示例2: binaryReadWriteExample
import org.apache.avro.io.Encoder; //导入方法依赖的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);
}
示例3: main
import org.apache.avro.io.Encoder; //导入方法依赖的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);
}
示例4: main
import org.apache.avro.io.Encoder; //导入方法依赖的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);
}
示例5: main
import org.apache.avro.io.Encoder; //导入方法依赖的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);
}
示例6: testSerializer
import org.apache.avro.io.Encoder; //导入方法依赖的package包/类
/**
* 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()));
}
示例7: convertJsonToAvro
import org.apache.avro.io.Encoder; //导入方法依赖的package包/类
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();
}
示例8: test
import org.apache.avro.io.Encoder; //导入方法依赖的package包/类
@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);
}
示例9: writeContainerless
import org.apache.avro.io.Encoder; //导入方法依赖的package包/类
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);
}
}
示例10: union
import org.apache.avro.io.Encoder; //导入方法依赖的package包/类
@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();
}
示例11: unionNull
import org.apache.avro.io.Encoder; //导入方法依赖的package包/类
@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();
}
示例12: bytes
import org.apache.avro.io.Encoder; //导入方法依赖的package包/类
@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();
}
示例13: testReaderWriterDecodingCompatibility
import org.apache.avro.io.Encoder; //导入方法依赖的package包/类
/** 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);
}
}
示例14: serialize
import org.apache.avro.io.Encoder; //导入方法依赖的package包/类
@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();
}
示例15: encodeAvroGenericRecord
import org.apache.avro.io.Encoder; //导入方法依赖的package包/类
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();
}