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


Java EncoderFactory类代码示例

本文整理汇总了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();
}
 
开发者ID:rkluszczynski,项目名称:avro-cli,代码行数:17,代码来源:RawConverterUtil.java

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

}
 
开发者ID:financialforcedev,项目名称:orizuru-java,代码行数:27,代码来源:Message.java

示例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);
}
 
开发者ID:nucypher,项目名称:hadoop-oss,代码行数:18,代码来源:Display.java

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

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

示例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"));
}
 
开发者ID:petezybrick,项目名称:iote2e,代码行数:28,代码来源:AvroByteArray.java

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

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

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

示例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;
}
 
开发者ID:moueimei,项目名称:flume-release-1.7.0,代码行数:22,代码来源:KafkaSink.java

示例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;
}
 
开发者ID:moueimei,项目名称:flume-release-1.7.0,代码行数:25,代码来源:KafkaChannel.java

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

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

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

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


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