Java Code Examples for com.fasterxml.jackson.dataformat.csv.CsvMapper#writer()

The following examples show how to use com.fasterxml.jackson.dataformat.csv.CsvMapper#writer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JacksonCsv.java    From zerocode with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {

        List<User> users = new ArrayList<>();
        User userHeader = new User("First", "Last", "Age");
        User user1 = new User("First name1", "Last Name1", null);
        User user2 = new User("First name2", "Last Name2", "22");
        users.add(userHeader);
        users.add(user1);
        users.add(user2);

        CsvSchema schema = CsvSchema.builder()
                .addColumn("firstName")
                .addColumn("lastName")
                .addColumn("age", CsvSchema.ColumnType.NUMBER)
                //.addColumn("comments")
                //.setUseHeader(true)
                //.setStrictHeaders(false)
                .build();

//        CsvSchema bootstrapSchema = CsvSchema.emptySchema().withHeader();
//        ObjectMapper mapper = new CsvMapper();

        CsvMapper mapper = new CsvMapper();
        mapper.enable(CsvParser.Feature.WRAP_AS_ARRAY);

        ObjectWriter writer = mapper.writer(schema.withLineSeparator("\n"));
        try {
            writer.writeValue(new File("target/ModifiedUsers.csv"), users);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
 
Example 2
Source File: OffsetSerDe.java    From mirus with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static OffsetSerDe csvOffsetSerDe() {
  CsvMapper csvMapper =
      new CsvMapper().configure(CsvGenerator.Feature.STRICT_CHECK_FOR_QUOTING, true);
  CsvSchema schema = csvMapper.schemaFor(OffsetInfo.class).withLineSeparator("");
  return new OffsetSerDe(
      csvMapper.writer(schema), csvMapper.reader(schema).forType(OffsetInfo.class));
}
 
Example 3
Source File: ZeroCodeReportGeneratorImpl.java    From zerocode with Apache License 2.0 5 votes vote down vote up
public void generateCsvReport(List<ZeroCodeCsvReport> zeroCodeCsvReportRows) {

        /*
         * Write to a CSV file
         */
        CsvSchema schema = CsvSchema.builder()
                .setUseHeader(true)
                .addColumn("scenarioName")
                .addColumn("scenarioLoop", CsvSchema.ColumnType.NUMBER)
                .addColumn("stepName")
                .addColumn("stepLoop", CsvSchema.ColumnType.NUMBER)
                .addColumn("correlationId")
                .addColumn("requestTimeStamp")
                .addColumn("responseDelayMilliSec", CsvSchema.ColumnType.NUMBER)
                .addColumn("responseTimeStamp")
                .addColumn("result")
                .addColumn("method")
                .build();

        CsvMapper csvMapper = new CsvMapper();
        csvMapper.enable(CsvParser.Feature.WRAP_AS_ARRAY);

        ObjectWriter writer = csvMapper.writer(schema.withLineSeparator("\n"));
        try {
            writer.writeValue(
                    new File(TARGET_FULL_REPORT_DIR +
                            TARGET_FULL_REPORT_CSV_FILE_NAME
                            //"_" +
                            //LocalDateTime.now().toString().replace(":", "-") +
                            //".csv"
                    ),
                    zeroCodeCsvReportRows);

        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("Exception while Writing full CSV report. Details: " + e);
        }
    }