gpt4 book ai didi

c - avro C 实现是否支持流式传输而不是文件输出?

转载 作者:太空狗 更新时间:2023-10-29 16:49:53 26 4
gpt4 key购买 nike

我已经在 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() 允许设置新缓冲区以供现有编写器使用。

关于c - avro C 实现是否支持流式传输而不是文件输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31853845/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com