You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by zwlin <zw...@foxmail.com> on 2016/05/06 09:16:49 UTC

in avro-c, how can i have a map with array values?

how can i have a map with array values?

 

i use avro1.7.7 and my schema is like this:

 

    {

             "type":"map",

             "values":{

                       "type":"array",

                       "items":"int"

             }

    }

 

my program is like this:

 

main.c

 

         /*

         * File:   main.c

         * Author: zwlin

         *

         * Created on 2016年4月21日, 下午5:16

         */

 

         #include <stdio.h>

         #include <stdarg.h>

         #include <avro.h>

 

    #define iout(i); sout("%d",i);

         #define cout(c); sout("%c",c);

         #define lout(l); sout("%ld",l);

         #define piout(str,i); sout("%s:%d",str,i);

         #define psout(str,s); sout("%s:%s",str,s);

         #define pcout(str,c); sout("%s:%c",str,c);

         #define plout(str,l);sout("%s,%l",str,l);

 

         //for output

 

         void sout(const char *format, ...) {

                   va_list args;

                   va_start(args, format);

                   vfprintf(stdout, format, args); 

                   fprintf(stdout, "\n");

                   va_end(args);

         }

 

         int main() {

                   int res, i, j, k;

 

                   avro_schema_t schema;

                   avro_datum_t rec;

 

                   //schema

                   avro_schema_t int_array_schema =
avro_schema_array(avro_schema_int());

                   avro_schema_t int_map_schema =
avro_schema_map(int_array_schema);

                   avro_schema_t int_union_schema = avro_schema_union();

                   avro_schema_union_append(int_union_schema,
int_map_schema);

                   avro_schema_union_append(int_union_schema,
avro_schema_null());

 

                   //choose schema

                   //    schema = int_array_schema;

                   schema = int_map_schema;

                   //    schema = int_union_schema;

 

                   //print schema

                   char schemaPrintBuf [1024];

                   avro_writer_t jswriter =
avro_writer_memory(schemaPrintBuf, 1024);

                   avro_schema_to_json(schema, jswriter);

                   psout("schema", schemaPrintBuf);

 

 

                   //data

                   int intry[] = {9, 8, 7, 6, 5, 4, 3};

                   avro_datum_t int_array = avro_array(int_array_schema);

                   for (i = 0; i < 7; ++i) {

                            avro_datum_t vt = avro_int32(intry[i]);

                            res = avro_array_append_datum(int_array, vt);

                            avro_datum_decref(vt);

                   }

                   avro_datum_t int_map = avro_map(int_map_schema);

                   res = avro_map_set(int_map, "intarray", int_array);

                   avro_datum_decref(int_array);

                   avro_datum_t int_a_union_datum =
avro_union(int_union_schema, 0, int_map);

 

                   //choose data

                   //    rec = int_array;

                   rec = int_map;

                   //    rec = int_a_union_datum;

 

                   //print data detail

                   sout("");

                   char * json;

                   sout("rec:");

                   avro_datum_to_json(rec, 0, &json);

                   sout(json);

 

                   //serialize

                   char buf[1024];

                   avro_writer_t writer = avro_writer_memory(buf, 1024);

                   res = avro_write_data(writer, schema, rec);

                   if (res) {

                            psout("write result", avro_strerror());

                   }

                   long len = avro_size_data(writer, schema, rec);

                   piout("data len", len);

 

                   //read

                   avro_reader_t reader = avro_reader_memory(buf, 1024);

                   avro_datum_t rslt;

                   res = avro_read_data(reader, schema, schema, &rslt);

                   if (res) {

                            psout("read error ", avro_strerror());

                   }

 

                   //read data

                   sout("");

                   sout("rslt:");

                   avro_datum_to_json(rslt, 0, &json);

                   sout(json);

                   return 0;

         }

 

this is my target schema:    **avro_schema_t schema**;<br>

and this is my data:    **avro_datum_t rec**;

 

in `//choose schema` and `//choose data` part,

 

if `schema = int_array_schema` and `rec = int_array`,<br>the output is like
this:<br>

[int_arry_schema output picture][1]

 

if `schema = int_map_schema` and `rec = int_map`,<br>the output is like
this:<br>

[int_map_schema output picture][2]

<br>**Segmentation fault** appears here: `res = avro_write_data(writer,
schema, rec);`.

 

 

 

so, how can i have a map with arrays as it's values?

 

 

  [1]: http://i.stack.imgur.com/3RBME.png

  [2]: http://i.stack.imgur.com/v9Mcn.png



---
Avast 防毒软件已对此电子邮件执行病毒检查。
https://www.avast.com/antivirus