如何获得包含数组值的 map ?
我使用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 schema;
这是我的数据:avro_datum_t rec;
在//选择架构
和//选择数据
部分,
如果 schema = int_array_schema
和 rec = int_array
,
输出如下:
int_arry_schema output picture
如果 schema = int_map_schema
和 rec = int_map
,
输出如下:
int_map_schema output picture
段错误出现在此处:res = avro_write_data(writer, schema, rec);
。
那么,我怎样才能拥有一个包含数组作为值的 map ?
最佳答案
您似乎没有做任何与代码相关的错误;然而,自从该项目转向“通用值”格式并为新 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);
在这种情况下不会崩溃 - 在这种情况下,架构验证可能会有些复杂。
关于c - 如何在avro-c中使用数组作为map的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36881462/