简体   繁体   English

将AVRO文件发布到kafka主题

[英]Publish an AVRO file to a kafka topic

I have aa file contains the data in an AVRO format data need to publish directly to a kafka topic. 我有一个文件包含AVRO格式的数据,需要直接发布到kafka主题的数据。 Is there any utilities available without much data parsing in my code? 在我的代码中没有大量数据解析的情况下,有没有可用的实用程序? Using kafka 1.0 version. 使用kafka 1.0版本。

You can read the data from the AVRO file and then serialize it into bytes array. 您可以从AVRO文件中读取数据,然后将其序列化为bytes数组。

final Schema avroSchema = new Schema.Parser().parse(new File("yourAvroSchema.avsc"));            
File avroFile="yourAvroFile.avro"

// Read as GenericRecord
final GenericDatumReader<GenericRecord> genericDatumReader = new GenericDatumReader<>(avroSchema );
final DataFileReader<GenericRecord> genericRecords = new DataFileReader<>(avroFile, genericDatumReader);

// Serialization
ByteArrayOutputStream out = new ByteArrayOutputStream();
DatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(avroSchema);

Encoder binaryEncoder = EncoderFactory.get().binaryEncoder(out, null);

while (genericRecords.hasNext()) {
    writer.write(genericRecords.next(), binaryEncoder);
}
binaryEncoder.flush();
out.close();

// ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM