本文整理汇总了Java中org.apache.avro.io.Encoder类的典型用法代码示例。如果您正苦于以下问题:Java Encoder类的具体用法?Java Encoder怎么用?Java Encoder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Encoder类属于org.apache.avro.io包,在下文中一共展示了Encoder类的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: recordToByteArray
import org.apache.avro.io.Encoder; //导入依赖的package包/类
/**
* Convert a GenericRecord to a byte array.
*/
public static byte[] recordToByteArray(GenericRecord record) throws IOException {
Closer closer = Closer.create();
try {
ByteArrayOutputStream out = closer.register(new ByteArrayOutputStream());
Encoder encoder = EncoderFactory.get().directBinaryEncoder(out, null);
DatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(record.getSchema());
writer.write(record, encoder);
byte[] byteArray = out.toByteArray();
return byteArray;
} catch (Throwable t) {
throw closer.rethrow(t);
} finally {
closer.close();
}
}
示例9: compressURI
import org.apache.avro.io.Encoder; //导入依赖的package包/类
public byte[] compressURI(final URI uri) {
Preconditions.checkNotNull(uri);
try {
final ByteArrayOutputStream stream = new ByteArrayOutputStream();
final Encoder encoder = EncoderFactory.get().directBinaryEncoder(stream, null);
final DatumWriter<Object> writer = new GenericDatumWriter<Object>(
AvroSchemas.COMPRESSED_IDENTIFIER);
this.dictionary.keyFor(uri); // ensure a compressed version of URI is available
final Object generic = encodeIdentifier(uri);
writer.write(generic, encoder);
return stream.toByteArray();
} catch (final IOException ex) {
throw new Error("Unexpected exception (!): " + ex.getMessage(), ex);
}
}
示例10: 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);
}
示例11: compressURI
import org.apache.avro.io.Encoder; //导入依赖的package包/类
public byte[] compressURI(final URI uri) {
Preconditions.checkNotNull(uri);
try {
final ByteArrayOutputStream stream = new ByteArrayOutputStream();
final Encoder encoder = EncoderFactory.get().directBinaryEncoder(stream, null);
final DatumWriter<Object> writer = new GenericDatumWriter<Object>(
Schemas.COMPRESSED_IDENTIFIER);
this.dictionary.keyFor(uri); // ensure a compressed version of URI is available
final Object generic = encodeIdentifier(uri);
writer.write(generic, encoder);
return stream.toByteArray();
} catch (final IOException ex) {
throw new Error("Unexpected exception (!): " + ex.getMessage(), ex);
}
}
示例12: getWritingConsumer
import org.apache.avro.io.Encoder; //导入依赖的package包/类
private Consumer<IndexedRecord> getWritingConsumer(Encoder[] encoder) {
return new Consumer<IndexedRecord>() {
GenericDatumWriter<IndexedRecord> writer = null;
@Override
public void accept(IndexedRecord ir) {
if (writer == null) {
writer = new GenericDatumWriter<>(ir.getSchema());
try {
if (json) {
encoder[0] = EncoderFactory.get().jsonEncoder(ir.getSchema(), output);
} else {
encoder[0] = EncoderFactory.get().binaryEncoder(output, null);
}
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
writeIndexedRecord(writer, encoder[0], ir);
}
};
}
示例13: writeDouble
import org.apache.avro.io.Encoder; //导入依赖的package包/类
/**
* Write double. Users can cast long, float and integer to double.
*
*/
protected void writeDouble(Object datum, Encoder out) throws IOException {
double num;
if (datum instanceof Integer) {
num = ((Integer) datum).doubleValue();
} else if (datum instanceof Long) {
num = ((Long) datum).doubleValue();
} else if (datum instanceof Float) {
num = ((Float) datum).doubleValue();
} else if (datum instanceof Double) {
num = (Double) datum;
} else
throw new IOException("Cannot convert to double:" + datum.getClass());
out.writeDouble(num);
}
示例14: create
import org.apache.avro.io.Encoder; //导入依赖的package包/类
public void create(String dirname, String filename, GenericData.Record[] records) throws Exception {
Schema schema = records[0].getSchema();
File dir = new File(tmpFolder.getRoot(), dirname);
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir, filename);
DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<GenericRecord>(schema) {
@Override
protected void writeBytes(Object datum, Encoder out) throws IOException {
super.writeBytes(ByteBuffer.wrap((byte[]) datum), out);
}
};
DataFileWriter<GenericRecord> dataFileWriter = new DataFileWriter<GenericRecord>(datumWriter);
dataFileWriter.create(schema, file);
TestSchema.append(dataFileWriter, records);
dataFileWriter.close();
}
示例15: writeMap
import org.apache.avro.io.Encoder; //导入依赖的package包/类
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void writeMap(Schema schema, Object datum, Encoder out)
throws IOException {
if (writeDirtyBits) {
// write extra state information for maps
StatefulMap<Utf8, ?> map = (StatefulMap) datum;
out.writeInt(map.states().size());
for (Entry<Utf8, State> e2 : map.states().entrySet()) {
out.writeString(e2.getKey());
out.writeInt(e2.getValue().ordinal());
}
}
super.writeMap(schema, datum, out);
}