本文整理汇总了Java中org.apache.avro.io.BinaryEncoder类的典型用法代码示例。如果您正苦于以下问题:Java BinaryEncoder类的具体用法?Java BinaryEncoder怎么用?Java BinaryEncoder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BinaryEncoder类属于org.apache.avro.io包,在下文中一共展示了BinaryEncoder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: encode
import org.apache.avro.io.BinaryEncoder; //导入依赖的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);
}
}
示例2: processSinglex
import org.apache.avro.io.BinaryEncoder; //导入依赖的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"));
}
示例3: serialize
import org.apache.avro.io.BinaryEncoder; //导入依赖的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);
}
}
示例4: serialize
import org.apache.avro.io.BinaryEncoder; //导入依赖的package包/类
@Override
public byte[] serialize(String s, WrapperAppMessage wrapperAppMessage) {
DatumWriter<WrapperAppMessage> datumWriter = new SpecificDatumWriter<>(wrapperAppMessage.getSchema());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
BinaryEncoder binaryEncoder = EncoderFactory.get().binaryEncoder(outputStream, null);
try {
datumWriter.write(wrapperAppMessage, binaryEncoder);
binaryEncoder.flush();//带缓冲区的binaryEncoder和直接directBinaryEncoder不一样,需要flush一下,否则字节数组没有数据
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
byte[] data = outputStream.toByteArray();
return data;
}
示例5: publish
import org.apache.avro.io.BinaryEncoder; //导入依赖的package包/类
public void publish(BrokerStats brokerStats) throws IOException {
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
BinaryEncoder binaryEncoder = avroEncoderFactory.binaryEncoder(stream, null);
avroEventWriter.write(brokerStats, binaryEncoder);
binaryEncoder.flush();
IOUtils.closeQuietly(stream);
String key = brokerStats.getName() + "_" + System.currentTimeMillis();
int numPartitions = kafkaProducer.partitionsFor(destTopic).size();
int partition = brokerStats.getId() % numPartitions;
Future<RecordMetadata> future = kafkaProducer.send(
new ProducerRecord(destTopic, partition, key.getBytes(), stream.toByteArray()));
future.get();
OpenTsdbMetricConverter.incr("kafka.stats.collector.success", 1, "host=" + HOSTNAME);
} catch (Exception e) {
LOG.error("Failure in publish stats", e);
OpenTsdbMetricConverter.incr("kafka.stats.collector.failure", 1, "host=" + HOSTNAME);
throw new RuntimeException("Avro serialization failure", e);
}
}
示例6: sendMessage
import org.apache.avro.io.BinaryEncoder; //导入依赖的package包/类
public synchronized void sendMessage(String clusterName, String message) {
int numRetries = 0;
while (numRetries < MAX_RETRIES) {
try {
long timestamp = System.currentTimeMillis();
OperatorAction operatorAction = new OperatorAction(timestamp, clusterName, message);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
BinaryEncoder binaryEncoder = avroEncoderFactory.binaryEncoder(stream, null);
avroWriter.write(operatorAction, binaryEncoder);
binaryEncoder.flush();
IOUtils.closeQuietly(stream);
String key = "" + System.currentTimeMillis();
ProducerRecord producerRecord =
new ProducerRecord(topic, key.getBytes(), stream.toByteArray());
Future<RecordMetadata> future = kafkaProducer.send(producerRecord);
future.get();
LOG.info("Send an message {} to action report : ", message);
break;
} catch (Exception e) {
LOG.error("Failed to publish report message {}: {}", clusterName, message, e);
numRetries++;
}
}
}
示例7: serializeRecord
import org.apache.avro.io.BinaryEncoder; //导入依赖的package包/类
/**
* Serialize the record to prepare for publishing.
*
* @param record the GenericRecord
* @param schema the Avro Schema
* @param ggAvroSchema the internal representation of the Avro schema
* @return the serialized record
* @throws IOException if there is a problem
*/
private byte[] serializeRecord(GenericRecord record, Schema schema,
@SuppressWarnings("unused") AvroSchema ggAvroSchema) throws IOException {
byte[] rval;
BinaryEncoder encoder = null;
// serialize the record into a byte array
ByteArrayOutputStream out = new ByteArrayOutputStream();
DatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(schema);
encoder = EncoderFactory.get().directBinaryEncoder(out, encoder);
writer.write(record, encoder);
encoder.flush();
rval = out.toByteArray();
//out.close(); // noop in the Apache version, so not bothering
return rval;
}
示例8: send
import org.apache.avro.io.BinaryEncoder; //导入依赖的package包/类
public void send(COL_RDBMS event) throws Exception {
EncoderFactory avroEncoderFactory = EncoderFactory.get();
SpecificDatumWriter<COL_RDBMS> avroEventWriter = new SpecificDatumWriter<COL_RDBMS>(COL_RDBMS.SCHEMA$);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
BinaryEncoder binaryEncoder = avroEncoderFactory.binaryEncoder(stream,null);
try {
avroEventWriter.write(event, binaryEncoder);
binaryEncoder.flush();
} catch (IOException e) {
e.printStackTrace();
throw e;
}
IOUtils.closeQuietly(stream);
KeyedMessage<String, byte[]> data = new KeyedMessage<String, byte[]>(
TOPIC, stream.toByteArray());
producer.send(data);
}
示例9: send
import org.apache.avro.io.BinaryEncoder; //导入依赖的package包/类
public void send(COL_ONEM2M event) throws Exception {
EncoderFactory avroEncoderFactory = EncoderFactory.get();
SpecificDatumWriter<COL_ONEM2M> avroEventWriter = new SpecificDatumWriter<COL_ONEM2M>(COL_ONEM2M.SCHEMA$);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
BinaryEncoder binaryEncoder = avroEncoderFactory.binaryEncoder(stream,null);
try {
avroEventWriter.write(event, binaryEncoder);
binaryEncoder.flush();
} catch (IOException e) {
e.printStackTrace();
throw e;
}
IOUtils.closeQuietly(stream);
KeyedMessage<String, byte[]> data = new KeyedMessage<String, byte[]>(
TOPIC, stream.toByteArray());
producer.send(data);
}
示例10: offerMessage
import org.apache.avro.io.BinaryEncoder; //导入依赖的package包/类
/**
* @see ClientConnection#offerMessage(ServerToClient)
*/
public boolean offerMessage(ServerToClient message) {
Session session = getSession();
if (session == null || !session.isOpen()) return false;
if (inFlightMessages.incrementAndGet() > MAX_IN_FLIGHT_MESSAGES) {
inFlightMessages.decrementAndGet();
return false;
}
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(stream, null);
writeToClient.write(message, encoder);
encoder.flush();
} catch (Exception e) {
log.warn("Failed to encode message to client", e);
session.close(StatusCode.SERVER_ERROR, "Internal server error");
return true;
}
session.getRemote().sendBytes(ByteBuffer.wrap(stream.toByteArray()), new WriteCallback() {
@Override
public void writeSuccess() {
inFlightMessages.decrementAndGet();
}
@Override
public void writeFailed(Throwable error) {
inFlightMessages.decrementAndGet();
log.info("Sending message to WebSocket client failed: ", error);
}
});
return true;
}
示例11: processSinglex
import org.apache.avro.io.BinaryEncoder; //导入依赖的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"));
}
示例12: processList
import org.apache.avro.io.BinaryEncoder; //导入依赖的package包/类
/**
* Process list.
*
* @param weathers the weathers
* @throws Exception the exception
*/
public void processList(List<Weather> weathers) throws Exception {
long before = System.currentTimeMillis();
BinaryEncoder binaryEncoder = null;
BinaryDecoder binaryDecoder = null;
Weather weatherRead = null;
for (Weather weather : weathers) {
DatumWriter<Weather> datumWriterWeather = new SpecificDatumWriter<Weather>(Weather.getClassSchema());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] byteData = null;
try {
binaryEncoder = EncoderFactory.get().binaryEncoder(baos, binaryEncoder);
datumWriterWeather.write(weather, binaryEncoder);
binaryEncoder.flush();
byteData = baos.toByteArray();
} finally {
baos.close();
}
DatumReader<Weather> datumReaderWeather = new SpecificDatumReader<Weather>(Weather.getClassSchema());
binaryDecoder = DecoderFactory.get().binaryDecoder(byteData, binaryDecoder);
weatherRead = datumReaderWeather.read(weatherRead, binaryDecoder);
// System.out.println("After Binary Read: " + weatherRead.toString());
}
System.out.println("size=" + weathers.size() + ", elapsed: " + (System.currentTimeMillis()-before));
}
示例13: serialize
import org.apache.avro.io.BinaryEncoder; //导入依赖的package包/类
@Override public byte[] serialize(String topic, GenericRecord data) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out, null);
Schema schema = data.getSchema();
DatumWriter<GenericRecord> writer = new SpecificDatumWriter<GenericRecord>(schema);
try {
writer.write(data, encoder);
encoder.flush();
out.close();
return out.toByteArray();
}
catch (IOException e) {
LOG.error("Error encoding Avro record into bytes: {}", e.getMessage());
return null;
}
}
示例14: write
import org.apache.avro.io.BinaryEncoder; //导入依赖的package包/类
@Override
public void write(Kryo kryo, Output output, Object object) {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(byteStream, null);
try {
writer.write(object, encoder);
encoder.flush();
byte[] bytes = byteStream.toByteArray();
output.writeInt(bytes.length, true);
output.write(bytes);
output.flush();
} catch (IOException e) {
throw new AppException(e);
}
}
示例15: serialize
import org.apache.avro.io.BinaryEncoder; //导入依赖的package包/类
public byte[] serialize(String topic, GenericRecord data)
throws SerializationException {
Schema schema = data.getSchema();
MD5Digest schemaId = null;
try {
schemaId = schemaRegistry.register(topic, schema);
ByteArrayOutputStream out = new ByteArrayOutputStream();
// MAGIC_BYTE | schemaId-bytes | avro_payload
out.write(LiAvroSerDeHelper.MAGIC_BYTE);
out.write(schemaId.asBytes());
BinaryEncoder encoder = encoderFactory.directBinaryEncoder(out, null);
DatumWriter<GenericRecord> writer = new GenericDatumWriter<>(schema);
writer.write(data, encoder);
encoder.flush();
byte[] bytes = out.toByteArray();
out.close();
return bytes;
} catch (IOException | SchemaRegistryException e) {
throw new SerializationException(e);
}
}