开发者

Question populating nested records in Avro using a GenericRecord

Suppose I’ve got the following schema:

{
 "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" }
   ]
  }
 }
]
}

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);

But how would I set the city, for example? How do I represent the key as a string that the r.put method can u开发者_JS百科nderstand?

Thanks


For the schema above:

GenericRecord t = new GenericData.Record(sch.getField("address").schema());
t.put("city","beijing");
r.put("address",t);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜