Skip to content

Instantly share code, notes, and snippets.

@titouanc
Created January 16, 2020 17:31
Show Gist options
  • Save titouanc/b18c0c54657db4e1f0361e0be9f710f3 to your computer and use it in GitHub Desktop.
Save titouanc/b18c0c54657db4e1f0361e0be9f710f3 to your computer and use it in GitHub Desktop.
#include <avro.h>
#define OUTFILE "out.avro"
#define OUTCODEC "null"
#define SCHEMA \
"{\"type\":\"record\","\
" \"name\":\"Person\","\
" \"fields\":["\
" {\"name\": \"ID\", \"type\": \"long\"},"\
" {\"name\": \"First\", \"type\": \"string\"},"\
" {\"name\": \"Last\", \"type\": \"string\"},"\
" {\"name\": \"Phone\", \"type\": \"string\"},"\
" {\"name\": \"Age\", \"type\": \"int\"}]}"
static int add_record(avro_schema_t schema, avro_file_writer_t writer)
{
static int id = 0;
// Create some data
avro_datum_t record = avro_record(schema);
avro_datum_t person_id = avro_int64(id++);
avro_datum_t first = avro_string("first");
avro_datum_t last = avro_string("last");
avro_datum_t phone = avro_string("phone");
avro_datum_t age = avro_int32(42);
if (avro_record_set(record, "ID", person_id)
|| avro_record_set(record, "First", first)
|| avro_record_set(record, "Last", last)
|| avro_record_set(record, "Phone", phone)
|| avro_record_set(record, "Age", age)){
fprintf(stderr, "Unable to add rpm into Avro record\n");
return -1;
}
avro_datum_decref(person_id);
avro_datum_decref(first);
avro_datum_decref(last);
avro_datum_decref(phone);
avro_datum_decref(age);
// 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);
// 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