0
我有一个Avro的模式是这样的 -如何在解码原始字节数组后更改特定的字段值?
{
"type":"record",
"name":"new_user",
"namespace":"com.hello",
"fields":[
{
"name":"user_id",
"type":[
"long",
"null"
]
},
{
"name":"segment",
"type":[
"string",
"null"
]
}
]
}
我用我上面的Avro架构这样的序列化数据,并且给了我一个字节数组,并工作正常 -
public static void main(String[] args) throws IOException {
Schema schema = new Parser()
.parse("{ \"type\":\"record\", \"name\":\"new_user\", \"namespace\":\"com.hello\", \"fields\":[ { \"name\":\"user_id\", \"type\":[ \"long\", \"null\" ] }, { \"name\":\"segment\", \"type\":[ \"string\", \"null\" ] } ] }");
byte[] originalAvrodata = getAvroBinaryData(schema);
// how to get newAvroData byte array in which user_id
// is change to some other random long number?
}
private static byte[] getAvroBinaryData(Schema schema) throws IOException {
GenericRecord record = new GenericData.Record(schema);
record.put("user_id", 123456L);
record.put("segment", "hello");
GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(schema);
ByteArrayOutputStream os = new ByteArrayOutputStream();
Encoder e = EncoderFactory.get().binaryEncoder(os, null);
writer.write(record, e);
e.flush();
byte[] byteData = os.toByteArray();
return byteData;
}
问题陈述:
我需要将originalAvrodata
字节数组解码,然后将user_id
字段值改变为其它一些long
编号然后构造一个newAvroData
字节数组使用相同的模式应该有user_id
字段值到一些随机long
数字。这有可能通过任何使用Avro的机会来实现吗?