当前位置: 首页>>代码示例>>Java>>正文


Java Encoder类代码示例

本文整理汇总了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);
}
 
开发者ID:gauravrmazra,项目名称:gauravbytes,代码行数:23,代码来源:AvroDatumExample.java

示例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);
}
 
开发者ID:gauravrmazra,项目名称:gauravbytes,代码行数:23,代码来源:AvroDatumExample.java

示例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);
	
}
 
开发者ID:gauravrmazra,项目名称:gauravbytes,代码行数:21,代码来源:AvroSerialization.java

示例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);
}
 
开发者ID:gauravrmazra,项目名称:gauravbytes,代码行数:21,代码来源:AvroReflectDataSerializationTest.java

示例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);
}
 
开发者ID:gauravrmazra,项目名称:gauravbytes,代码行数:21,代码来源:AvroCircularRefSerializationTest.java

示例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()));
}
 
开发者ID:cognitree,项目名称:flume-elasticsearch-sink,代码行数:23,代码来源:TestAvroSerializer.java

示例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();
}
 
开发者ID:rkluszczynski,项目名称:avro-cli,代码行数:21,代码来源:RawConverterUtil.java

示例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();
  }
}
 
开发者ID:Hanmourang,项目名称:Gobblin,代码行数:19,代码来源:AvroUtils.java

示例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);
    }
}
 
开发者ID:dkmfbk,项目名称:knowledgestore,代码行数:17,代码来源:AvroSerializer.java

示例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);
}
 
开发者ID:dkmfbk,项目名称:knowledgestore,代码行数:25,代码来源:AvroSchemasTest.java

示例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);
    }
}
 
开发者ID:dkmfbk,项目名称:knowledgestore,代码行数:17,代码来源:SerializerAvro.java

示例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);
        }
    };
}
 
开发者ID:Talend,项目名称:components,代码行数:25,代码来源:DatasetContentWriter.java

示例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);
}
 
开发者ID:linkedin,项目名称:Cubert,代码行数:20,代码来源:PigAvroDatumWriter.java

示例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();

    }
 
开发者ID:viadeo,项目名称:viadeo-avro-utils,代码行数:25,代码来源:AvroUtilTest.java

示例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);
}
 
开发者ID:galaxyeye,项目名称:gora-0.3-simplified,代码行数:17,代码来源:PersistentDatumWriter.java


注:本文中的org.apache.avro.io.Encoder类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。