我正在使用由字符串和映射组成的简单记录来测试 Avro for java。这是我的架构:
{
"type":"record",
"name":"TableRecord",
"fields":[
{"name":"ActionCode","type":"string"},
{
"name":"Fields",
"type":{"type":"map","values":["string","long","double","null"]}
}
]
}
这是一个非常简单的失败测试用例:
@Test
public void testSingleMapSerialization() throws IOException {
final String schemaStr; // see above
// create some data
Map<String, Object> originalMap = new Hashtable<>();
originalMap.put("Ric", "sZwmXAdYKv");
originalMap.put("QuoteId", 4342740204922826921L);
originalMap.put("CompanyName", "8PKQ9va3nW8pRWb4SjPF2DvdQDBmlZ");
originalMap.put("Category", "AvrIfd");
// serialize data
Schema.Parser parser = new Schema.Parser();
Schema schema = parser.parse(schemaStr);
ByteArrayOutputStream out = new ByteArrayOutputStream();
DatumWriter<GenericRecord> writer = new GenericDatumWriter<>(schema);
Encoder encoder = EncoderFactory.get().binaryEncoder(out, null);
GenericRecord datum = new GenericData.Record(schema);
datum.put("ActionCode", "R");
datum.put("Map", originalMap);
writer.write(datum, encoder);
encoder.flush();
out.flush();
// deserialize data
DatumReader<GenericRecord> reader = new GenericDatumReader<>(schema);
Decoder decoder = DecoderFactory.get().binaryDecoder(out.toByteArray(), null);
datum = new GenericData.Record(schema);
Map<String, Object> deserializedMap = (Map<String, Object>) reader.read(datum, decoder).get("Map");
System.out.println(originalMap);
System.out.println(deserializedMap);
Assert.assertEquals("Maps data don't match", originalMap, deserializedMap);
}
这是测试的输出:
{CompanyName=8PKQ9va3nW8pRWb4SjPF2DvdQDBmlZ, Ric=sZwmXAdYKv, Category=AvrIfd, QuoteId=4342740204922826921}
{QuoteId=4342740204922826921, Category=AvrIfd, CompanyName=8PKQ9va3nW8pRWb4SjPF2DvdQDBmlZ, Ric=sZwmXAdYKv}
java.lang.AssertionError: Maps data don't match expected:<{CompanyName=8PKQ9va3nW8pRWb4SjPF2DvdQDBmlZ, Ric=sZwmXAdYKv, Category=AvrIfd, QuoteId=4342740204922826921}> but was:<{QuoteId=4342740204922826921, Category=AvrIfd, CompanyName=8PKQ9va3nW8pRWb4SjPF2DvdQDBmlZ, Ric=sZwmXAdYKv}>
如您所见,两张地图看起来相同,但测试失败。JUnit 在幕后调用“equals”方法,这应该返回 true。顺便说一句,如果您想知道什么是乱码,我通常会使用随机生成的数据创建测试用例,这就是它的来源。
难道我做错了什么?我不知道字符串序列化/反序列化有什么问题吗?我在网上搜索没有成功。
想法?
谢谢
乔杜德