Home | About | Sematext search-lucene.com search-hadoop.com
 Search Hadoop and all its subprojects:

Switch to Threaded View
Avro, mail # user - avro accessing record within an array in C


Copy link to this message
-
Re: avro accessing record within an array in C
Mika Ristimaki 2013-09-26, 08:23
Hi,

I don't quite understand what are you trying to do.  If you need to read items from an array and the array size is 0, there isn't anything to read. No need to get schema either in my opinion. If you really need to check if a field exists in schema, you should be able to use this

if (avro_schema_record_field_get_index(schema, "avro_record_var_1") == -1)
// No field called avro_record_var_1

You might first need to get subschemas or something…

-Mika
On Sep 25, 2013, at 9:20 PM, Mahesh V <[EMAIL PROTECTED]> wrote:

> Quick question.
> How do I read the schema of an empty array?
>  
> for e.g if my schema look like whats shown below, then given a fieldname e.g. array_record_var_1,
> how do I check if the field exists inside the array , even if the array is empty.
>  
> the below snippet of code works fine if the array has at least one element.
> if the array is not populated, the avro_value_get_size returns 0 and the code does not enter the for loop
>  
>  if(type == AVRO_ARRAY ) {
>                 avro_value_get_size(&field, &array_count);
>                 printf(" array number of elements = %d \n", array_count);
>                 for(j = 0; j < array_count; j++) {
>                     avro_value_get_by_index(&field, j, &subfield, &name),
>                     printf(" field name is : %s , type =", name);
>                     type = avro_value_get_type(&subfield);
>                     print_type(type);
>                     printf("\n");
>                     if(type == AVRO_RECORD) {
>                         avro_value_get_size(&subfield, &rec_count);
>                         printf(" record number of elements = %d \n", rec_count);
>                         for(k = 0; k < rec_count; k++) {
>                             avro_value_get_by_index(&subfield, k, &subrec, &name),
>                             printf(" field name is : %s , type =", name);
>                             type = avro_value_get_type(&subrec);
>                             print_type(type);
>                             printf("\n");
>                         }
>                     }
>                 }
>             }
>
> > > char *some_schema > > > "{"
> > > "\"type\":\"record\", \"name\" : \"some_schema\", \"fields\": [ "
> > > "{ \"name\" : \"some_var_a\" , \"type\" : \"int\" },"
> > > "{"
> > > "\"name\" : \"some_ip\", \"type\" : {"
> > > "\"type\":\"record\", \"name\" : \"some_ip\", \"fields\" : [ "
> > > " { \"name\" : \"ip_version\", \"type\" : \"int\" },"
> > > " { \"name\" : \"ip_v4\", \"type\" : \"long\" },"
> > > " { \"name\" : \"ip_v6\", \"type\" : \"string\" }"
> > > "]}},"
> > > "{ \"name\" : \"some_array\" , \"type\" : "
> > > " { \"type\":\"array\", \"items\": "
> > > "{"
> > > "\"type\":\"record\", \"name\" : \"array_record_1\", \"fields\": [ "
> > > "{ \"name\" : \"array_record_var_1\" , \"type\" : \"int\" },"
> > > "{ \"name\" : \"array_record_var_2\" , \"type\" : \"boolean\" } "
> > > "]"
> > > "}"
>
>
>
>