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

The following examples show how to use com.fasterxml.jackson.dataformat.csv.CsvMapper#enable() . 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: CsvUtils.java    From spring-boot-rest-api-helpers with MIT License 11 votes vote down vote up
public static <T> List<T> read(Class<T> clazz, InputStream stream, boolean withHeaders, char separator) throws IOException {
    CsvMapper mapper = new CsvMapper();

    mapper.enable(CsvParser.Feature.TRIM_SPACES);
    mapper.enable(CsvParser.Feature.ALLOW_TRAILING_COMMA);
    mapper.enable(CsvParser.Feature.INSERT_NULLS_FOR_MISSING_COLUMNS);
    mapper.enable(CsvParser.Feature.SKIP_EMPTY_LINES);
    mapper.disable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);
    CsvSchema schema = mapper.schemaFor(clazz).withColumnReordering(true);
    ObjectReader reader;
    if (separator == '\t') {
        schema = schema.withColumnSeparator('\t');
    }
    else {
        schema = schema.withColumnSeparator(',');
    }
    if (withHeaders) {
        schema = schema.withHeader();
    }
    else {
        schema = schema.withoutHeader();
    }
    reader = mapper.readerFor(clazz).with(schema);
    return reader.<T>readValues(stream).readAll();
}
 
Example 2
Source File: InventoryReportLineWriter.java    From s3-inventory-usage-examples with Apache License 2.0 7 votes vote down vote up
/**
 * Write a new inventory report to S3 and returns a locator which includes this inventory report's information
 * @return Locator which includes the information of this new report
 * @throws IOException thrown when GZIPOutputStream not created successfully or csvMapper.write() fails
 */
public InventoryManifest.Locator writeCsvFile(List<InventoryReportLine> inventoryReportLine) throws IOException{
    CsvMapper csvMapper = new CsvMapper();
    csvMapper.enable(JsonGenerator.Feature.IGNORE_UNKNOWN);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
    csvMapper.writer(schema).writeValues(gzipOutputStream).writeAll(inventoryReportLine).close();
    byte[] zipByteArray = byteArrayOutputStream.toByteArray();

    InputStream zipInputStream = new ByteArrayInputStream(zipByteArray);
    ObjectMetadata metaData = new ObjectMetadata();
    metaData.setContentLength(zipByteArray.length);
    PutObjectRequest request = new PutObjectRequest(bucketName, outputInventoryReportKey, zipInputStream, metaData);
    s3Client.putObject(request);

    return this.buildLocator(zipByteArray);
}
 
Example 3
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 4
Source File: ApplicationReporter.java    From cf-butler with Apache License 2.0 5 votes vote down vote up
protected AppUsageReport readAppUsageReport(String filename) throws JsonParseException, JsonMappingException, IOException {
    String content = readFile(filename);
    if (filename.endsWith(".json")) {
        return mapper.readValue(content, AppUsageReport.class);
    } else if (filename.endsWith(".csv")) {
        CsvMapper csvMapper = new CsvMapper();
        csvMapper.enable(CsvParser.Feature.WRAP_AS_ARRAY);
        File csvFile = new File(filename);
        MappingIterator<String[]> it = csvMapper.readerFor(String[].class).readValues(csvFile);
        AppUsageReportBuilder builder = AppUsageReport.builder();
        List<AppUsageMonthly> reports = new ArrayList<>();
        int rowNum = 0;
        while (it.hasNext()) {
            String[] row = it.next();
            if (rowNum > 0) {
                AppUsageMonthlyBuilder amb = AppUsageMonthly.builder();
                for (int i = 0; i < row.length; i++) {
                    if (i == 0) {
                        String[] period = row[i].split("-");
                        if (period.length == 2) {
                            amb.month(Integer.valueOf(period[1]));
                        }
                        amb.year(Integer.valueOf(period[0]));
                    }
                    if (i == 1) {
                        amb.averageAppInstances(Double.valueOf(row[i]));
                    }
                    if (i == 2) {
                        amb.maximumAppInstances(Integer.valueOf(row[i]));
                    }
                    if (i == 3) {
                        amb.appInstanceHours(Double.valueOf(row[i]));
                    }
                }
                reports.add(amb.build());
            }
            rowNum++;
        }
        builder.monthlyReports(reports);
        return builder.build();
    } else {
        return AppUsageReport.builder().build();
    }
}
 
Example 5
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);
        }
    }
 
Example 6
Source File: TCSV.java    From Llunatic with GNU General Public License v3.0 5 votes vote down vote up
public void test() throws IOException {
        CsvMapper mapper = new CsvMapper();
        mapper.enable(CsvParser.Feature.WRAP_AS_ARRAY);
        CsvSchema schema = CsvSchema.emptySchema();
//        schema = schema.withHeader();
//        schema = schema.withQuoteChar('\'');
//        File csvFile = new File("/Temp/llunatic/doctors/10k/doctor.csv");
        File csvFile = new File("/Users/donatello/Temp/chaseBench-workspace/LUBM/data/01k/src-emailAddress.csv");
        long start = new Date().getTime();
        MappingIterator<String[]> it = mapper.readerFor(String[].class).with(schema).readValues(csvFile);
        String[] row = it.next();
        System.out.println(Arrays.asList(row));
        long end = new Date().getTime();
        System.out.println("**** " + (end - start) + " ms");
//        while (it.hasNext()) {
//            String[] row = it.next();
//            System.out.println(Arrays.asList(row));
//        }
    }
 
Example 7
Source File: ServiceInstanceReporter.java    From cf-butler with Apache License 2.0 4 votes vote down vote up
protected List<NormalizedServicePlanMonthlyUsage> readServiceUsageReport(String filename) throws JsonParseException, JsonMappingException, IOException {
    String content = readFile(filename);
    if (filename.endsWith(".json")) {
        return NormalizedServicePlanMonthlyUsage.listOf(mapper.readValue(content, ServiceUsageReport.class));
    } else if (filename.endsWith(".csv")) {
        CsvMapper csvMapper = new CsvMapper();
        csvMapper.enable(CsvParser.Feature.WRAP_AS_ARRAY);
        File csvFile = new File(filename);
        MappingIterator<String[]> it = csvMapper.readerFor(String[].class).readValues(csvFile);
        List<NormalizedServicePlanMonthlyUsage> reports = new ArrayList<>();
        int rowNum = 0;
        while (it.hasNext()) {
            String[] row = it.next();
            if (rowNum > 0) {
                NormalizedServicePlanMonthlyUsageBuilder amb = NormalizedServicePlanMonthlyUsage.builder();
                for (int i = 0; i < row.length; i++) {
                    if (i == 0) {
                        String[] period = row[i].split("-");
                        if (period.length == 2) {
                            amb.month(Integer.valueOf(period[1]));
                        }
                        amb.year(Integer.valueOf(period[0]));
                    }
                    if (i == 1) {
                        amb.serviceName(row[i]);
                    }
                    if (i == 2) {
                        amb.serviceGuid(row[i]);
                    }
                    if (i == 3) {
                        amb.servicePlanName(row[i]);
                    }
                    if (i == 4) {
                        amb.servicePlanGuid(row[i]);
                    }
                    if (i == 5) {
                        amb.averageInstances(Double.valueOf(row[i]));
                    }
                    if (i == 6) {
                        amb.maximumInstances(Integer.valueOf(row[i]));
                    }
                    if (i == 7) {
                        amb.durationInHours(Double.valueOf(row[i]));
                    }
                }
                NormalizedServicePlanMonthlyUsage usage = amb.build();
                if (StringUtils.isNotBlank(usage.getServicePlanGuid())) {
                    reports.add(usage);
                }
            }
            rowNum++;
        }
        return reports;
    } else {
        return NormalizedServicePlanMonthlyUsage.listOf(ServiceUsageReport.builder().build());
    }
}
 
Example 8
Source File: DAOCSVRepair.java    From BART with MIT License 4 votes vote down vote up
private MappingIterator<String[]> getMappingIterator(BufferedReader br) throws IOException {
    CsvMapper mapper = new CsvMapper();
    mapper.enable(CsvParser.Feature.WRAP_AS_ARRAY);
    CsvSchema schema = CsvSchema.emptySchema().withColumnSeparator(CSV_CHAR_SEP).withQuoteChar(CSV_CHAR_QUOTE_SEPARATOR);
    return mapper.readerFor(String[].class).with(schema).readValues(br);
}
 
Example 9
Source File: DAOCSVRepair.java    From BART with MIT License 4 votes vote down vote up
private MappingIterator<String[]> getMappingIterator(String line) throws IOException {
    CsvMapper mapper = new CsvMapper();
    mapper.enable(CsvParser.Feature.WRAP_AS_ARRAY);
    CsvSchema schema = CsvSchema.emptySchema().withColumnSeparator(CSV_CHAR_SEP).withQuoteChar(CSV_CHAR_QUOTE_SEPARATOR);
    return mapper.readerFor(String[].class).with(schema).readValues(line);
}