Search in sources :

Example 6 with CSVPrinter

use of org.apache.commons.csv.CSVPrinter in project opennms by OpenNMS.

the class RScriptExecutor method toCsv.

/**
     * Convert the table to a CSV string.
     */
protected static StringBuilder toCsv(final RowSortedTable<Long, String, Double> table) throws IOException {
    final String[] columnNames = table.columnKeySet().toArray(new String[] {});
    final StringBuilder sb = new StringBuilder();
    final CSVPrinter printer = CSVFormat.RFC4180.withHeader(columnNames).print(sb);
    for (long rowIndex : table.rowKeySet()) {
        for (String columnName : columnNames) {
            Double value = table.get(rowIndex, columnName);
            if (value == null) {
                value = Double.NaN;
            }
            printer.print(value);
        }
        printer.println();
    }
    return sb;
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter)

Example 7 with CSVPrinter

use of org.apache.commons.csv.CSVPrinter in project jackrabbit-oak by apache.

the class CSVFileGenerator method generate.

public void generate(FluentIterable<BinaryResource> binaries) throws IOException {
    Closer closer = Closer.create();
    int count = 0;
    try {
        CSVPrinter printer = new CSVPrinter(Files.newWriter(outFile, Charsets.UTF_8), CSVFileBinaryResourceProvider.FORMAT);
        closer.register(printer);
        for (BinaryResource br : binaries) {
            count++;
            printer.printRecord(br.getBlobId(), br.getByteSource().size(), br.getMimeType(), br.getEncoding(), br.getPath());
        }
        printer.flush();
        log.info("Generated csv output at {} with {} entries", outFile.getAbsolutePath(), count);
    } finally {
        closer.close();
    }
}
Also used : Closer(com.google.common.io.Closer) CSVPrinter(org.apache.commons.csv.CSVPrinter)

Aggregations

CSVPrinter (org.apache.commons.csv.CSVPrinter)7 File (java.io.File)2 PrintWriter (java.io.PrintWriter)2 Closer (com.google.common.io.Closer)1 BufferedWriter (java.io.BufferedWriter)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 StringWriter (java.io.StringWriter)1 Iterator (java.util.Iterator)1 CSVFormat (org.apache.commons.csv.CSVFormat)1 MemoryBlobStore (org.apache.jackrabbit.oak.spi.blob.MemoryBlobStore)1 Test (org.junit.Test)1