本文介绍了使用GenericRecord在Avro中填充嵌套记录的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有以下架构:
{
"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表示每个创建的配置文件.要添加名字,可以轻松执行以下操作:
I’m using a GenericRecord to represent each Profile that gets created. To add a firstName, it’s easy to do the following:
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);
例如,我将如何设置城市?如何将密钥表示为r.put方法可以理解的字符串?
But how would I set the city, for example? How do I represent the key as a string that the r.put method can understand?
谢谢
推荐答案
对于上述架构:
GenericRecord t = new GenericData.Record(sch.getField("address").schema());
t.put("city","beijing");
r.put("address",t);
这篇关于使用GenericRecord在Avro中填充嵌套记录的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!