Embedded List of Records Using Avro Generic Record

I’m posting this here, because I know someone out there will be Googling how to do this like I was.

I didn’t find much and ended up spending a lot of time reading through the Avro API and using my debugger. Hopefully this will help someone.

The code below is an example how to construct an avro object that has an embedded (nested) list of avro objects using the GenericRecord class. In the example, there is a User avro record that contains a list of phoneNumber records.


String schemaDescription = "[ { \n"
	+ "\"type\" : \"record\", \n"
	+ "\"name\" : \"PhoneNumber\", \n"
	+ "\"namespace\" : \"com.test.namespace.avro\", \n"
	+ "\"fields\" : [ \n"
	+ "   {\"name\" : \"id\", \"type\": \"long\", \"doc\": \"Attribute id\" }, \n"
	+ "   {\"name\" : \"updateDate\",  \"type\": \"long\", \"doc\": \"Update date of the attribute\" }, \n"
	+ "   {\"name\" : \"value\", \"type\" : \"string\", \"doc\": \"Value of the attribute\" } \n"
	+ " ] \n"
	+ "}, \n"
	+ "{ \n"
	+ "\"type\" : \"record\", \n"
	+ "\"name\" : \"User\", \n"
	+ "\"namespace\" : \"com.test.namespace.avro\", \n"
	+ "\"fields\" : [ \n"
	+ "   {\"name\": \"timestamp\", \"type\": \"long\"},\n"
	+ "   {\"name\": \"username\", \"type\": \"string\" },\n"
	+ "   {\"name\" : \"phoneNumbers\", \"type\" : [\"null\", {\"type\" : \"array\", \"items\" : \"PhoneNumber\"}] } \n"
	+ "] \n"
	+ "}] \n";
Schema schema = new Schema.Parser().parse(schemaDescription);
Schema phoneNumberSchema = schema.getTypes().get(0);

List list = new ArrayList<>();

Date d = new Date();
GenericRecord genericPhoneNumber = new GenericData.Record(phoneNumberSchema);
genericPhoneNumber.put("id", Long.valueOf(1L));
genericPhoneNumber.put("updateDate", d.getTime());
genericPhoneNumber.put("value", "111-555-5555");
list.add(genericPhoneNumber);

genericPhoneNumber = new GenericData.Record(phoneNumberSchema);
genericPhoneNumber.put("id", Long.valueOf(2L));
genericPhoneNumber.put("updateDate", d.getTime());
genericPhoneNumber.put("value", "222-666-6666");
list.add(genericPhoneNumber);

Schema userSchema = schema.getTypes().get(1);
GenericRecord genericUser = new GenericData.Record(userSchema);
genericUser.put("phoneNumbers", list);
genericUser.put("username", "test_username");
genericUser.put("timestamp", d.getTime());
Advertisements
This entry was posted in code example, coding, java and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s