如何在avro-c中使用数组作为map的值

时间:2016-04-27 06:00:34

标签: c avro

我怎么能有一个包含数组值的地图?

我使用avro1.7.7,我的架构是这样的:

{
    "type":"map",
    "values":{
        "type":"array",
        "items":"int"
    }
}

我的程序是这样的:

的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;
}

这是我的目标架构: avro_schema_t架构;
这是我的数据: avro_datum_t rec ;

//choose schema//choose data部分,

如果schema = int_array_schemarec = int_array,则输出如下:
int_arry_schema output picture

如果schema = int_map_schemarec = int_map,则输出如下:
int_map_schema output picture
分段错误出现在此处:res = avro_write_data(writer, schema, rec);

那么,我怎么能得到一个带数组的地图作为它的值?

1 个答案:

答案 0 :(得分:0)

您似乎没有对代码做任何错误;但是看起来好像项目已经转移到'通用值'格式并为新API实现了旧的包装器,看起来这是向后兼容性的错误。

如果从代码中构建的json重新生成模式,它可以工作,这似乎表明函数回归:

所以在这里,如果我们生成一个新的模式副本:

psout("schema", schemaPrintBuf);
avro_schema_t from_json;
if (!avro_schema_from_json_literal(schemaPrintBuf, &from_json)) {
    fprintf(stderr, "Cannot convert from json literal: %s", avro_strerror());
    exit(1);
}

// then in the write:
res = avro_write_data(writer, from_json, rec);

它不再崩溃 - 这只是替换单个模式引用 - 如果在重新读取时将其他对模式的引用保留,它看起来就像是有效的。

或者,因为您基于已知的手动构造架构构造了avro_datum_t,所以您可以禁用编写器验证,这似乎是崩溃的起源:

 res = avro_write_data(writer, NULL, rec);
在这种情况下,

不会崩溃 - 在这种情况下可能会出现模式验证的一些复杂情况。

相关问题