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();
// ....
1条答案
按热度按时间rsaldnfx1#
您可以从avro文件中读取数据,然后将其序列化为字节数组。