I am programming in Objective-C. I use Apache Avro to serialize data.
My avro scheme is this:
{ "name": "School", "type":"record", "fields":[ { "name":"Employees", "type":["null", {"type": "array", "items":{ "name":"Teacher", "type":"record", "fields":[ {"name":"name", "type":"string"} {"name":"age", "type":"int"} ] } } ], "default":null } ] }
In my Objective-C code, I have an Array object from Teacher , each teacher object contains a value of name and age .
I want to write teacher array data to a file using Avro with the above diagram. I'm mostly worried about how to write data to the Employees array defined in the above diagram.
Here is my code (for this I have to use C style code, I follow the Avro C documentation):
// I don't show this function, it constructs the a `avro_value_t` based on the schema. No problem here. avro_value_t school = [self constructSchoolValueForSchema]; // get "Employees" field avro_value_t employees; avro_value_get_by_name(school, "employees", &employees, 0); int idx = 0; for (Teacher *teacher in teacherArray) { // get name and age NSString *name = teacher.name; int age = teacher.age; // set value to avro data type. // here 'unionField' is the field of 'Employees', it is a Avro union type which is either null or an array as defined in schema above avro_value_t field, unionField; avro_value_set_branch(&employees, 1, &unionField); // based on documentation, I should use 'avro_value_append' avro_value_append(&employees, name, idx); // I get confused here!!!! // in above line of code, I append 'name' to 'employees', //which looks not correct, // because the 'Employees' array is an array of 'Teacher', not arrary of 'name' // What is the correct way to add teacher to 'employees' ? idx ++; }
The question I want to ask is actually in the code comment above.
I follow the Avro C documentation, but am lost, how can I add each Teacher to Employees ? In my code above, I added only each name teacher to the Employees array.