Skip to content

Instantly share code, notes, and snippets.

@titouanc
Last active January 16, 2020 17:15
Show Gist options
  • Save titouanc/0df61b807d06ca7611cc6708f12fc938 to your computer and use it in GitHub Desktop.
Save titouanc/0df61b807d06ca7611cc6708f12fc938 to your computer and use it in GitHub Desktop.
A minimal (working ?) example of writing data with avro-c
/* Compile with:
* gcc -std=c99 -Wall -Wextra -Werror $(pkg-config --cflags avro-c) -o test-avro test-avro.c $(pkg-config --libs avro-c)
*/
#include <avro.h>
#define OUTFILE "out.avro"
#define OUTCODEC "null"
#define SCHEMA \
"{"\
" \"type\": \"record\","\
" \"name\": \"engine\","\
" \"fields\": ["\
" {\"type\": [\"null\",\"double\"], \"name\": \"rpm\"},"\
" {\"type\": [\"null\",\"double\"], \"name\": \"hours\"}"\
" ]"\
"}"
static int add_record(avro_schema_t schema, avro_file_writer_t writer, double rpm)
{
// Create some data
avro_datum_t record = avro_record(schema);
avro_datum_t engine_rpm = avro_double(rpm);
if (avro_record_set(record, "rpm", engine_rpm)){
fprintf(stderr, "Unable to add rpm into Avro record\n");
return -1;
}
avro_datum_decref(engine_rpm);
// Write data to file
int res = avro_file_writer_append(writer, record);
if (res){
fprintf(stderr, "Unable to write Avro record to file: %s\n", avro_strerror());
}
avro_datum_decref(record);
return res;
}
int main(int argc, const char **argv)
{
(void) argc;
(void) argv;
// Load schema
char schema_json_string[4096];
strncpy(schema_json_string, SCHEMA, sizeof(schema_json_string));
avro_schema_t schema;
if (avro_schema_from_json(schema_json_string, 0, &schema, NULL)){
fprintf(stderr, "Unable to load Avro schema: %s\n", avro_strerror());
return -1;
}
// Open output file
avro_file_writer_t writer;
if (avro_file_writer_create_with_codec(OUTFILE, schema, &writer, OUTCODEC, 0)){
fprintf(stderr, "Unable to create output file: %s\n", avro_strerror());
return -1;
}
add_record(schema, writer, 723.5);
// Cleanup
avro_file_writer_close(writer);
avro_schema_decref(schema);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment