私はObjective-Cでプログラミングしています。データのシリアル化にはApache Avroを使用しています。C言語のアブロ・アレイ・タイプに任意のデータを設定
マイアブロスキーマはこれです:私のObjective-Cのコードでは
{
"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
}
]
}
、私はTeacher
オブジェクトの配列を持って、各教師のオブジェクトは、name
& age
の値が含まれています。
上記のスキーマでAvroを使用して教師配列データをファイルに書きたいと思います。私は主に、上記のスキーマで定義された配列Employees
にデータを書き込む方法について懸念しています。
はここに私のコードです(私はそれを行うにはCスタイルのコードを使用する必要があり、私は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 ++;
}
私が聞きたい質問は、上記のコードのコメントに実際にあります。
私はそのAvro Cのドキュメントに従っていますが、どうすればteacher
をemployees
に追加できますか?私の上記のコードでは、各教師のname
をemployees
配列に追加しました。