我正在尝试使用GenericData.Record将avro数据生成到kafka中,但是我得到以下异常:
线程中的异常" main" org.apache.avro.AvroRuntimeException:不是有效的架构字段:emailAddresses.email
这是我的架构:
{
"namespace": "com.cloudurable.User",
"type": "record",
"name": "User",
"fields": [
{"name": "id", "type": "int", "default" : 0},
{"name": "fname", "type": "string", "default" : "EMPTY"},
{"name": "lname", "type": "string", "default" : "EMPTY"},
{"name": "phone_number", "type": "string", "default" : "EMPTY"},
{"name": "age", "type": "int", "default" : 0},
{
"name": "emailAddresses",
"type": {
"type": "record",
"name": "EmailAddress",
"fields": [
{
"name": "email",
"type": "string",
"default" : "EMPTY"
},
{
"name": "address",
"type": "boolean",
"default": false
}
]
}
}
]
}
我的Java代码如下:
GenericData.Record avroRecord = new GenericData.Record(SchemaRegstryClient.getLatestSchema("test2"));
System.out.println(avroRecord.getSchema().getFields());
avroRecord.put("id", i);
avroRecord.put("fName", "Sumit" + i);
avroRecord.put("lName", "Gupta " +i);
avroRecord.put("phone_number", "98911 " +i);
avroRecord.put("age", i);
avroRecord.put("emailAddresses.email", "sumit@gmail.com");
avroRecord.put("emailAddresses.address", true);
任何人都知道如何将嵌套值放在avro记录中?
答案 0 :(得分:2)
您无法使用'。'设置内部值。因此,您必须手动创建内部对象(emailAddresses)并将其设置为顶级记录字段。
final GenericData.Record emailAddresses = new GenericData.Record(schema.getField("emailAddresses").schema());
emailAddresses.put("email", "sumit@gmail.com");
emailAddresses.put("address", true);
avroRecord.put("emailAddresses", emailAddresses);