Beispiel #1
0
static void
process_file(const char *in_filename, const char *out_filename)
{
    avro_file_reader_t  reader;
    avro_file_writer_t  writer;

    if (in_filename == NULL) {
        if (avro_file_reader_fp(stdin, "<stdin>", 0, &reader)) {
            fprintf(stderr, "Error opening <stdin>:\n  %s\n",
                    avro_strerror());
            exit(1);
        }
    } else {
        if (avro_file_reader(in_filename, &reader)) {
            fprintf(stderr, "Error opening %s:\n  %s\n",
                    in_filename, avro_strerror());
            exit(1);
        }
    }

    avro_schema_t  wschema;
    avro_value_iface_t  *iface;
    avro_value_t  value;

    wschema = avro_file_reader_get_writer_schema(reader);
    iface = avro_generic_class_from_schema(wschema);
    avro_generic_value_new(iface, &value);

    if (avro_file_writer_create_with_codec
            (out_filename, wschema, &writer, codec, block_size)) {
        fprintf(stderr, "Error creating %s:\n  %s\n",
                out_filename, avro_strerror());
        exit(1);
    }

    while (avro_file_reader_read_value(reader, &value) == 0) {
        if (avro_file_writer_append_value(writer, &value)) {
            fprintf(stderr, "Error writing to %s:\n  %s\n",
                    out_filename, avro_strerror());
            exit(1);
        }
        avro_value_reset(&value);
    }

    avro_file_reader_close(reader);
    avro_file_writer_close(writer);
    avro_value_decref(&value);
    avro_value_iface_decref(iface);
    avro_schema_decref(wschema);
}
Beispiel #2
0
int main(int argc, char *argv[]) {
    FILE *input;

    avro_schema_t schema;
    avro_file_writer_t out;
    const char *key;

    int opt, opterr = 0, verbose = 0, memstat = 0, errabort = 0, strjson = 0;
    char *schema_arg = NULL;
    char *codec = NULL;
    char *endptr = NULL;
    char *outpath = NULL;
    size_t block_sz = 0;
    size_t max_str_sz = 0;
    extern char *optarg;
    extern int optind, optopt;

    while ((opt = getopt(argc, argv, "c:s:S:b:z:dmxjh")) != -1) {
        switch (opt) {
        case 's':
            schema_arg = optarg;
            break;
        case 'S':
            schema_arg = read_schema_file(optarg);
            break;
        case 'b':
            block_sz = strtol(optarg, &endptr, 0);
            if (*endptr) {
                fprintf(stderr, "ERROR: Invalid block size for -b: %s\n", optarg);
                opterr++;
            }
            break;
        case 'z':
            max_str_sz = strtol(optarg, &endptr, 0);
            if (*endptr) {
                fprintf(stderr, "ERROR: Invalid maximum string size for -z: %s\n", optarg);
                opterr++;
            }
            break;
        case 'c':
            codec = optarg;
            break;
        case 'd':
            verbose = 1;
            break;
        case 'x':
            errabort = 1;
            break;
        case 'j':
            strjson = 1;
            break;
        case 'm':
            #if defined(__linux__)
              memstat = 1;
            #else
              usage_error(argv[0], "Memory stats is a Linux-only feature!");
            #endif
            break;
        case 'h':
            print_help(argv[0]);
            exit(0);
        case ':':
            fprintf(stderr, "ERROR: Option -%c requires an operand\n", optopt);
            opterr++;
            break;
        case '?':
            fprintf(stderr, "ERROR: Unrecognized option: -%c\n", optopt);
            opterr++;
        }
    }

    int file_args_cnt = (argc - optind);
    if (file_args_cnt == 0) {
        usage_error(argv[0], "Please provide at least one file name argument");
    }
    if (file_args_cnt > 2) {
        fprintf(stderr, "Too many file name arguments: %d!\n", file_args_cnt);
        usage_error(argv[0], 0);
    }

    if (opterr) usage_error(argv[0], 0);
    if (!schema_arg) usage_error(argv[0], "Please provide correct schema!");

    if (!codec) codec = "null";
    else if (strcmp(codec, "snappy") && strcmp(codec, "deflate") && strcmp(codec, "lzma") && strcmp(codec, "null")) {
        fprintf(stderr, "ERROR: Invalid codec %s, valid codecs: snappy, deflate, lzma, null\n", codec);
        exit(EXIT_FAILURE);
    }

    if ((argc - optind) == 1) {
        input = stdin;
        outpath = argv[optind];
    } else {
        outpath = argv[optind+1];
        input = fopen(argv[optind], "rb");
        if ( errno != 0 ) {
            fprintf(stderr, "ERROR: Cannot open input file: %s: ", argv[optind]);
            perror(0);
            exit(EXIT_FAILURE);
        }
    }

    if (avro_schema_from_json_length(schema_arg, strlen(schema_arg), &schema)) {
        fprintf(stderr, "ERROR: Unable to parse schema: '%s'\n", schema_arg);
        exit(EXIT_FAILURE);
    }

    if (!strcmp(outpath, "-")) {
        if (avro_file_writer_create_with_codec_fp(stdout, outpath, 0, schema, &out, codec, block_sz)) {
            fprintf(stderr, "ERROR: avro_file_writer_create_with_codec_fp FAILED: %s\n", avro_strerror());
            exit(EXIT_FAILURE);
        }

    } else {
        remove(outpath);
        if (avro_file_writer_create_with_codec(outpath, schema, &out, codec, block_sz)) {
            fprintf(stderr, "ERROR: avro_file_writer_create_with_codec FAILED: %s\n", avro_strerror());
            exit(EXIT_FAILURE);
        }
    }

    if (verbose)
        fprintf(stderr, "Using codec: %s\n", codec);

    process_file(input, out, schema, verbose, memstat, errabort, strjson, max_str_sz);

    if (verbose)
        printf("Closing writer....\n");
    avro_file_writer_close(out);
}