Tôi đang lập trình trong Objective-C. Tôi đang sử dụng Apache Avro để tuần tự hóa dữ liệu.đặt dữ liệu arrary thành loại mảng Avro với ngôn ngữ C
schema Avro của tôi là thế này:
{
"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
}
]
}
Trong mã Objective-C của tôi, tôi có một mảng của Teacher
đối tượng, mỗi đối tượng giáo viên chứa giá trị của name
& age
.
Tôi muốn ghi dữ liệu mảng giáo viên vào tệp bằng Avro với lược đồ hiển thị ở trên. Tôi chủ yếu quan tâm về cách ghi dữ liệu vào mảng Employees
được xác định trong giản đồ trên.
Đây là mã của tôi (Tôi có sử dụng mã phong cách C để làm điều đó, tôi làm theo cá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 ++;
}
Các câu hỏi tôi muốn hỏi là thực sự trong các bình luận mã trên.
Tôi đang theo dõi tài liệu Avro C đó, nhưng tôi bị mất cách tôi có thể thêm mỗi teacher
vào employees
? Trong mã trên của tôi, tôi chỉ thêm name
của mỗi giáo viên vào mảng employees
.