假设我有以下架构:
{
"name" : "Profile",
"type" : "record",
"fields" : [
{ "name" : "firstName", "type" : "string" },
{ "name" : "address" , "type" : {
"type" : "record",
"name" : "AddressUSRecord",
"fields" : [
{ "name" : "address1" , "type" : "string" },
{ "name" : "address2" , "type" : "string" },
{ "name" : "city" , "type" : "string" },
{ "name" : "state" , "type" : "string" },
{ "name" : "zip" , "type" : "int" },
{ "name" : "zip4", "type": "int" }
]
}
}
]
}
我正在使用GenericRecord来代表创建的每个配置文件。要添加名字,可以轻松执行以下操作:
Schema sch = Schema.parse(schemaFile);
DataFileWriter<GenericRecord> fw = new DataFileWriter<GenericRecord>(new GenericDatumWriter<GenericRecord>()).create(sch, new File(outFile));
GenericRecord r = new GenericData.Record(sch);
r.put(“firstName”, “John”);
fw.append(r);
但是,例如,我将如何设置城市?如何将 key 表示为r.put方法可以理解的字符串?
谢谢
最佳答案
对于以上架构:
GenericRecord t = new GenericData.Record(sch.getField("address").schema());
t.put("city","beijing");
r.put("address",t);
关于avro - 使用GenericRecord在Avro中填充嵌套记录的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5480043/