我有一个自动生成的avro模式,可以对avro内存流进行编码和解码。
现在,我想写入文件,并且我必须通过以下方法来激活文件写入器:
#include "avro/Encoder.hh"
#include "avro/Decoder.hh"
#include "avro/ValidSchema.hh"
#include "avro/Compiler.hh"
#include "avro/DataFile.hh
avro::ValidSchema loadSchema(const char* filename)
{
std::ifstream ifs(filename);
avro::ValidSchema result;
avro::compileJsonSchema(ifs, result);
return result;
}
int
main()
{
avro::ValidSchema cpxSchema = loadSchema("cpx.json");
{
avro::DataFileWriter<c::cpx> dfw("test.bin", cpxSchema);
c::cpx c1;
for (int i = 0; i < 100; i++) {
c1.re = i * 100;
c1.im = i + 100;
dfw.write(c1);
}
dfw.close();
}
{
avro::DataFileReader<c::cpx> dfr("test.bin", cpxSchema);
c::cpx c2;
while (dfr.read(c2)) {
std::cout << '(' << c2.re << ", " << c2.im << ')' << std::endl;
}
}
return 0;
}
因此,我在auto_generated.hh
中存储了自动生成的文件(命令:avrogencpp -i schema.json -o auto_generated.hh -n auto_generated
),并且其中包含了所有相关文件,并且没有从json文件中加载架构(这似乎可以如果我已经自动生成了所有内容,那我就尝试了以下操作:
#include "auto_generated.hh"
int main(){
/*
auto_generated::schema() is going to produce the datum which I am
storing.
auto_generated::schema is the struct which contains the data
members which will be set during the course of data output.
*/
//avro::ValidSchema vs; vs.setSchema(auto_generated::schema);
// auto_generated::schema schema = auto_generated::schema();
// avro::ValidSchema vs(schema);
// avro::ValidSchema vs;
// auto_generated::schema schema = auto_generated::schema();
// vs.setSchema(schema);
avro::ValidSchema vs;
avro::Schema s = auto_generated::schema(); // or without ()
vs.setSchema(s);
// etc.
avro::DataFileWriter<auto_generated::schema> dfw("foo",vs);
}
如何初始化ValidSchema
并构造DataFileWriter
以便将数据写入文件?