Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does avro C implementation support streaming rather than file output?

Tags:

c

avro

I have gone through the C document at avro and I see that I can get only avro output to file. How do I get the serialized output to a buffer so that I can send over a tcp socket. Any help is much appreciated.

like image 973
deadbeef Avatar asked Aug 06 '15 10:08

deadbeef


1 Answers

There is an avro_writer_memory() exactly for this case, it takes buffer pointer and length as parameters and gives you avro_writer_t that can be used in regular write functions. You can find its usage in tests, like this or this. The minimum example is going to be something like this (outputting encoded value to stderr, so better redirect that to some file and examine it after program run):

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

Also, there is an avro_writer_memory_set_dest() that allows to set new buffer to use by the existing writer.

like image 133
Roman Khimov Avatar answered Nov 01 '22 19:11

Roman Khimov