3

I am trying to produce avro data into kafka using GenericData.Record but I am getting the following exception:

Exception in thread "main" org.apache.avro.AvroRuntimeException: Not a valid schema field: emailAddresses.email

Here is my Schema:

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

My Java Code is as below:

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

Anyone have idea how can I put nested value in the avro record?

djm.im
  • 3,295
  • 4
  • 30
  • 45
Sumit G
  • 436
  • 8
  • 21

1 Answers1

6

You cannot set inner values using '.'. So, you manually have to create inner object (emailAddresses) and set it as a field of top level record.

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);
Natalia
  • 4,362
  • 24
  • 25
  • 1
    Thanks Natalia, it works. Also do you have idea how do i get the record which i have set here. GenericRecord record = recordInjection.invert(avroRecord._2).get(); System.out.println("id= " + record.get("emailAddresses")); its giving me like json structure, but i want to get individual record. – Sumit G Feb 07 '18 at 07:59
  • I got the answer ((GenericRecord) record.get("emailAddresses")).get("email") works here. – Sumit G Feb 07 '18 at 10:21