avro-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Gill, John" <John.G...@Teradata.com>
Subject Re: C - AVRO - Array of records
Date Wed, 12 Apr 2017 17:04:11 GMT
I think you are doing things backwards in the way you add the new element to your array.

You need to create the value based upon the element schema from logDataReferenceArray and
then append it to the list.

Something like:
// Create the new element based on the element schema
element_iface = avro_generic_class_from_schema(avro_schema_array_items(avro_value_get_schema(logDataReferenceArray)));
avro_generic_value_new(element_iface, &element);

// Set the values in the element
avro_value_get_by_name(&element, "deviceType", &currentField, NULL);
avro_value_set_int(&currentField, 2);

avro_value_get_by_name(&element, "deviceId", &currentField, NULL);
avro_value_set_int(&currentField, 3);

// Append the newly created element to the list
avro_value_append(&logDataReferenceArray, &element, NULL),

Someone else on the list can correct me if I am wrong. But this is the only way I see to do

-- John

From: "Utsch Julian (AE-EB/ENS1)" <Julian.Utsch@de.bosch.com>
Reply-To: "user@avro.apache.org" <user@avro.apache.org>
Date: Wednesday, April 12, 2017 at 2:18 AM
To: "user@avro.apache.org" <user@avro.apache.org>
Subject: C - AVRO - Array of records

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");



 metafile_iface = avro_generic_class_from_schema(metafile_schema);

 try(avro_generic_value_new(metafile_iface, &metafile_record), "Error creating instance
of metafile record");


 avro_file_writer_create("metafile.avro", metafile_schema, &file);

 int64_t metaId = 9999;

 avro_value_get_by_name(&metafile_record, "metaId", &currentField, NULL);

 avro_value_set_long(&currentField, metaId);

 avro_value_get_by_name(&metafile_record, "logDataReference", &logDataReferenceArray,

 try(avro_value_append(&logDataReferenceArray, &element, NULL), "Error in appending
record in logdatareference array");

 avro_value_get_by_name(&element, "deviceType", &currentField, NULL);

 avro_value_set_int(&currentField, 2);

 avro_value_get_by_name(&element, "deviceId", &currentField, NULL);

 avro_value_set_int(&currentField, 3);

avro_file_writer_append_value(file, &metafile_record);





View raw message