Hi together,
I'm trying to get familiar with
AVRO<https://avro.apache.org/docs/1.8.1/spec.html> in C. I've defined an AVRO
Schema (see below) with simple fields, which aren't the problem. But also there
are an array of (sub)-records, which I can't access correctly. There are no
compiler errors or other AVRO errors, but the AVRO file won't be filled with
data... Without the array of (sub)-records, everything works fine.
Maybe some of you have an idea why my code isn't still working. Therefore I
can't find any examples or documentations, which describe the procedure to fill
subrecords with data.
PS: I've extracted only the important parts from the whole code; otherwise the
code would be too long for here. So, I hope I didn't forget any necessary parts.
Thanks for your help. Best regards, Julian
Here the code:
const char METAFILE_SCHEMA[] =
"{\
\"name\" : \"metafile\",\
\"type\" : \"record\",\
\"fields\" : [\
{ \"name\" : \"metaId\", \"type\" : \"long\" },\
{ \"name\" : \"logFilesCount\", \"type\" : \"int\" },\
{ \"name\" : \"logDataReference\", \"type\" : {\
\"type\" : \"array\",\
\"items\" : {\
\"name\" : \"LogFileRef\",\
\"type\" : \"record\",\
\"fields\" : [\
{ \"name\" : \"deviceType\", \"type\" : \"int\" },\
{ \"name\" : \"deviceId\", \"type\" : \"int\" }\
]\
}\
}\
},\
{ \"name\" : \"timestamp\", \"type\" : \"long\" }\
]\
}";
avro_file_writer_t file;
avro_schema_t metafile_schema;
avro_value_iface_t *metafile_iface;
avro_value_t metafile_record;
avro_value_t currentField;
avro_value_t element;
avro_value_t logDataReferenceArray;
if(avro_schema_from_json_literal(METAFILE_SCHEMA, &metafile_schema)) {
fprintf(stderr, "Unable to parse metafile schema\n");
exit(EXIT_FAILURE);
}
metafile_iface = avro_generic_class_from_schema(metafile_schema);
try(avro_generic_value_new(metafile_iface, &metafile_record), "Error creating
instance of metafile record");
remove("metafile.avro");
avro_file_writer_create("metafile.avro", metafile_schema, &file);
int64_t metaId = 9999;
avro_value_get_by_name(&metafile_record, "metaId", ¤tField, NULL);
avro_value_set_long(¤tField, metaId);
avro_value_get_by_name(&metafile_record, "logDataReference",
&logDataReferenceArray, NULL);
try(avro_value_append(&logDataReferenceArray, &element, NULL), "Error in
appending record in logdatareference array");
avro_value_get_by_name(&element, "deviceType", ¤tField, NULL);
avro_value_set_int(¤tField, 2);
avro_value_get_by_name(&element, "deviceId", ¤tField, NULL);
avro_value_set_int(¤tField, 3);
avro_file_writer_append_value(file, &metafile_record);
avro_file_writer_close(file);
avro_schema_decref(metafile_schema);
avro_value_decref(&metafile_record);
avro_value_iface_decref(metafile_iface);