微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

avro C实现支持流而不是文件输出?

我在 avro经过了C文件
我看到我只能将avro输出文件.如何将序列化的输出缓冲区,以便我可以通过tcp套接字发送.任何帮助深表感谢.

解决方法

一个avro_writer_memory()完全针对这种情况,它需要缓冲区指针和长度作为参数,并给出可用于常规写入功能的avro_writer_t.您可以在测试中找到它的用法,例如 thisthis.最小的例子将是这样的(将编码值输出到stderr,所以更好地重定向到某些文件,并在程序运行后检查):
#include <avro.h>
#include <stdio.h>
#include <unistd.h>

static const char json_schema[] = "{ \"type\": \"string\" }";

int main(void)
{
    char buf[1024];
    avro_writer_t writer;
    avro_schema_t schema;
    avro_value_iface_t* iface;
    avro_value_t val;
    size_t len;

    if (avro_schema_from_json_literal(json_schema,&schema) != 0) {
        printf("Failed to initialize schema\n");
        goto out;
    }
    if ((writer = avro_writer_memory(buf,sizeof(buf))) == NULL) {
        printf("Failed to initialize writer\n");
        goto out_schema;
    }
    if ((iface = avro_generic_class_from_schema(schema)) == NULL) {
        printf("Failed to get class from schema\n");
        goto out_writer;
    }
    if (avro_generic_value_new(iface,&val) != 0) {
        printf("Failed to create new value\n");
        goto out_iface;
    }
    if (avro_value_reset(&val) != 0) {
        printf("Failed to reset value\n");
        goto out_val;
    }
    if (avro_value_set_string(&val,"some string wrapped by avro") != 0) {
        printf("Failed to set value string\n");
        goto out_val;
    }
    if (avro_value_write(writer,&val) != 0) {
        printf("Failed to write value into the buffer\n");
        goto out_val;
    }
    len = avro_writer_tell(writer);
    printf("got %lu bytes\n",(unsigned long)len);
    if (write(STDERR_FILENO,buf,len) != len) {
        printf("Failed to write to stderr,oops\n");
        goto out_val;
    }
out_val:
    avro_value_decref(&val);
out_iface:
    avro_value_iface_decref(iface);
out_writer:
    avro_writer_free(writer);
out_schema:
    avro_schema_decref(schema);
out:
    return 0;
}

此外,还有一个avro_writer_memory_set_dest()允许设置新的缓冲区以供现有的作者使用.

原文地址:https://www.jb51.cc/c/115930.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐