我想使用Apache Avro来序列化我的数据,我的客户端是用C++编写的,而我的服务器是用Java编写的。
My server java code looks like this:
Schema scm = new Schema.Parser().parse("....shcema String....."); ByteArrayInputStream inputStream = new ByteArrayInputStream(record.array()); Decoder coder = new DecoderFactory().directBinaryDecoder(inputStream, null); GenericDatumReader<GenericRecord> reDatumReader = new GenericDatumReader<GenericRecord>(scm); try { GenericRecord result = (GenericRecord)reDatumReader.read(null, coder); //here! the result "name", "num_groups" is empty! System.out.println(result.get("name")+" "+result.get("num_groups")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
And my client code:
std::string schemaDescript ="....shcema String....."; std::stringstream rsStream(schemaDescript); avro::ValidSchema rSchema; avro::compileJsonSchema(rsStream, rSchema); avro::EncoderPtr encoder = avro::binaryEncoder(); std::auto_ptr<avro::OutputStream> oStream = avro::memoryOutputStream(); encoder->init(*oStream); avro::GenericDatum rData(rSchema); avro::GenericRecord sReord = rData.value<avro::GenericRecord>(); sReord.setFieldAt(0, avro::GenericDatum("i am nice")); sReord.setFieldAt(1, avro::GenericDatum(1)); sReord.setFieldAt(2, avro::GenericDatum(12)); sReord.setFieldAt(3, avro::GenericDatum(13)); avro::GenericWriter gwriter(rSchema, encoder); gwriter.write(rData); oStream->flush(); std::auto_ptr<avro::InputStream> inSt = avro::memoryInputStream(*oStream); avro::StreamReader instReader(*inSt); size_t outputLen = oStream->byteCount(); uint8_t* theByteData = new uint8_t[outputLen]; instReader.hasMore(); instReader.readBytes(theByteData, outputLen);
theByteData
发送到服务器,代码正常运行(没有异常),但结果为空,有人可以告诉我出了什么问题吗?并且在Java中,我们使用键获取值:
result.get("name")
;但在C++中,我们使用索引获取值:record.fieldAt(0).value<string>()
。如果我无法使用字符串键获取值,如何将索引匹配到字符串键?