gpt4 book ai didi

avro - 使用GenericRecord在Avro中填充嵌套记录的问题

转载 作者:行者123 更新时间:2023-12-04 05:34:14 24 4
gpt4 key购买 nike

假设我有以下架构:

{
"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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com