serialization - how to add data to the repeatable fields in avro schema? -


i'm trying test avro serde , deserde without code generation (i completed task using code generation). schema follows

{ "type": "record", "name" : "person", "namespace" : "avro", "fields": [     { "name" : "personname", "type": ["null","string"] },     { "name" : "personid", "type": ["null","string"] },     {  "name" : "addresses", "type": {         "type": "array",         "items": [  {           "type" : "record",           "name" : "address",           "fields" : [             { "name" : "addressline1", "type": ["null", "string"] },             { "name" : "addressline2", "type": ["null", "string"] },             { "name" : "city", "type": ["null", "string"] },             { "name" : "state", "type": ["null", "string"] },             { "name" : "zipcode", "type": ["null", "string"] }             ]         }]         }     },     { "name" : "contact", "type" : ["null", "string"]} ] } 

i understand how data added schema.

schema schema = new schema.parser().parse(new file("src/person.avsc.txt")); genericrecord person1 = new genericdata.record(schema); person1.put("personname", "goud"); 

but how add city, state etc address , add addresses?

genericrecord address1 = new genericdata.record(schema); address1.put("city", "sanjose"); 

the above snippet doesn't work. tried genericarray, couldn't head around it.

you need describe inner complex type ("type" : "record", "name" : "address") in separate schema, this:

{   "type" : "record",   "name" : "address",   "fields" : [     { "name" : "addressline1", "type": ["null", "string"] },     { "name" : "addressline2", "type": ["null", "string"] },     { "name" : "city", "type": ["null", "string"] },     { "name" : "state", "type": ["null", "string"] },     { "name" : "zipcode", "type": ["null", "string"] }   ] } 

then may create inner object:

schema innerschema = new schema.parser().parse(new file("person_address.avsc")); genericrecord address = new genericdata.record(innerschema); address.put("addressline1", "adr_1"); address.put("addressline2", "adr_2"); address.put("city", "test_city"); address.put("state", "test_state"); address.put("zipcode", "zipcode_00000"); 

then add inner object created arraylist.

at last, create main object , add staff in it.

here full example in java:

schema innerschema = new schema.parser().parse(new file("person_address.avsc")); genericrecord address = new genericdata.record(innerschema); address.put("addressline1", "adr_1"); address.put("addressline2", "adr_2"); address.put("city", "test_city"); address.put("state", "test_state"); address.put("zipcode", "zipcode_00000");  arraylist<genericrecord> addresses = new arraylist<>(); addresses.add(address);  schema mainschema = new schema.parser().parse(new file("person.avsc")); genericrecord person1 = new genericdata.record(mainschema); person1.put("personname", "goud"); person1.put("personid", "123_id"); person1.put("addresses", addresses); 

result:

{   "personname": "goud",   "personid": "123_id",   "addresses": [     {       "addressline1": "adr_1",       "addressline2": "adr_2",       "city": "test_city",       "state": "test_state",       "zipcode": "zipcode_00000"     }   ],   "contact": "test_contact" } 

Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -