0

Sample snippet of code

KEY = (char *)malloc(sizeof(argRkMessage->key)+1);
    if( KEY != NULL )
    {
       //Logger
    }
    reader_class = avro_generic_class_from_schema(KEYSchema);
    check_i(avro_generic_value_new(reader_class, &val));
    /*Send the memory in the buffer into the reader*/
    nested_reader = avro_reader_memory( argRkMessage->key, sizeof(argRkMessage->key) );
    check_i(avro_value_read( nested_reader, &val ));
    type = avro_value_get_type(&val);
    if ( type == AVRO_RECORD )
    {
//logger
    }
    check_i(avro_value_get_size(&val, &field_count));
    check_i(avro_value_get_by_name(&val, "Key", &field, NULL));
    type = avro_value_get_type(&field);
    if ( type == AVRO_STRING )
    {
//logger
    }
    check_i(avro_value_get_string(&field, &KEY, &size));


printf(“Key is:%s", KEY);

The problem i have is "KEY" always comes to be a NULL string.

I did try a solution to do a hexdump of the contents of argRkMessage->key , that seems to have the value.

I am using librdkafka and avro.c to get this implemented. The sample code is a stripped down version of a C-Kafka consumer process that is intended to get complex Kafka messages encoded in AVRO format.

Olaf Kock
  • 43,342
  • 7
  • 54
  • 84
Jyo
  • 1
  • `KEY` is NULL after the call to `malloc`? – yano Jan 07 '20 at 17:41
  • printf(“Key is:%s", KEY); Here i am getting it as null. size is 1 (\0). – Jyo Jan 07 '20 at 18:30
  • basically, i am not able to get the value using : avro_value_get_string(). Not sure what is wrong here. KEY is defined as a const char *. I did a malloc just to be sure. The documentation never required it to. field is an avro_value_t and size is a size_t. – Jyo Jan 07 '20 at 18:31
  • Can someone help get a C program sample of : Reading from kafka using librdkafka library - for a message which has avro encoded key and message body. We can use librdkafka and avro c library. Kind of still stuck with the above snippet. Any help is appreciated. – Jyo Jan 14 '20 at 20:12

0 Answers0