【问题标题】:Question populating nested records in Avro using a GenericRecord使用 GenericRecord 在 Avro 中填充嵌套记录的问题
【发布时间】:2011-03-29 23:13:52
【问题描述】:

假设我有以下架构:

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

但是,例如,我将如何设置城市?如何将键表示为 r.put 方法可以理解的字符串?

谢谢

【问题讨论】:

    标签: avro


    【解决方案1】:

    对于上面的架构:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-27
      • 2012-07-30
      • 1970-01-01
      • 1970-01-01
      • 2018-10-11
      • 1970-01-01
      相关资源
      最近更新 更多