@Override
  @SuppressWarnings("unchecked")
  public int run() throws IOException {
    Preconditions.checkArgument(targets != null && targets.size() == 1, "A data file is required.");

    String source = targets.get(0);

    CodecFactory codecFactory = Codecs.avroCodec(compressionCodecName);

    final Schema schema;
    if (avroSchemaFile != null) {
      schema = Schemas.fromAvsc(open(avroSchemaFile));
    } else {
      schema = getAvroSchema(source);
    }

    final Schema projection = filterSchema(schema, columns);
    Iterable<Record> reader = openDataFile(source, projection);
    boolean threw = true;
    long count = 0;

    DatumWriter<Record> datumWriter = new GenericDatumWriter<>(schema);
    try (DataFileWriter<Record> fileWriter = new DataFileWriter<>(datumWriter)) {
      fileWriter.setCodec(codecFactory);
      try (OutputStream os = overwrite ? create(outputPath) : createWithNoOverwrite(outputPath);
          DataFileWriter<Record> writer = fileWriter.create(projection, os)) {
        for (Record record : reader) {
          writer.append(record);
          count += 1;
        }
      }
      threw = false;
    } catch (RuntimeException e) {
      throw new RuntimeException("Failed on record " + count, e);
    } finally {
      if (reader instanceof Closeable) {
        Closeables.close((Closeable) reader, threw);
      }
    }

    return 0;
  }
@Test
  public void testToAvroCommandFromJson() throws IOException {
    final File jsonInputFile = folder.newFile("sample.json");
    final File avroOutputFile = folder.newFile("sample.avro");

    // Write the json to the file, so we can read it again.
    final String inputJson = "{\"id\": 1, \"name\": \"Alice\"}\n" + "{\"id\": 2, \"name\": \"Bob\"}\n"
        + "{\"id\": 3, \"name\": \"Carol\"}\n"
        + "{\"id\": 4, \"name\": \"Dave\"}";

    try (BufferedWriter writer = new BufferedWriter(new FileWriter(jsonInputFile))) {
      writer.write(inputJson);
    }

    ToAvroCommand cmd = new ToAvroCommand(null);

    JCommander.newBuilder()
        .addObject(cmd)
        .build()
        .parse("--overwrite", jsonInputFile.getAbsolutePath(), "--output", avroOutputFile.getAbsolutePath());

    assert (cmd.run() == 0);
  }