Search in sources :

Example 41 with CSVPrinter

use of org.apache.commons.csv.CSVPrinter in project thingsboard by thingsboard.

the class SqlDbHelper method dumpTableIfExists.

public static Path dumpTableIfExists(Connection conn, String tableName, String[] columns, String[] defaultValues, String dumpPrefix, boolean printHeader) throws Exception {
    if (tableExists(conn, tableName)) {
        Path dumpFile = Files.createTempFile(dumpPrefix, null);
        Files.deleteIfExists(dumpFile);
        CSVFormat csvFormat = CSV_DUMP_FORMAT;
        if (printHeader) {
            csvFormat = csvFormat.withHeader(columns);
        }
        try (CSVPrinter csvPrinter = new CSVPrinter(Files.newBufferedWriter(dumpFile), csvFormat)) {
            try (PreparedStatement stmt = conn.prepareStatement("SELECT * FROM " + tableName)) {
                try (ResultSet tableRes = stmt.executeQuery()) {
                    ResultSetMetaData resMetaData = tableRes.getMetaData();
                    Map<String, Integer> columnIndexMap = new HashMap<>();
                    for (int i = 1; i <= resMetaData.getColumnCount(); i++) {
                        String columnName = resMetaData.getColumnName(i);
                        columnIndexMap.put(columnName.toUpperCase(), i);
                    }
                    while (tableRes.next()) {
                        dumpRow(tableRes, columnIndexMap, columns, defaultValues, csvPrinter);
                    }
                }
            }
        }
        return dumpFile;
    } else {
        return null;
    }
}
Also used : Path(java.nio.file.Path) CSVPrinter(org.apache.commons.csv.CSVPrinter) ResultSetMetaData(java.sql.ResultSetMetaData) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet) CSVFormat(org.apache.commons.csv.CSVFormat) PreparedStatement(java.sql.PreparedStatement)

Example 42 with CSVPrinter

use of org.apache.commons.csv.CSVPrinter in project camel by apache.

the class CsvMarshaller method marshal.

/**
     * Marshals the given object into the given stream.
     *
     * @param exchange     Exchange (used for access to type conversion)
     * @param object       Body to marshal
     * @param outputStream Output stream of the CSV
     * @throws NoTypeConversionAvailableException if the body cannot be converted
     * @throws IOException                        if we cannot write into the given stream
     */
public void marshal(Exchange exchange, Object object, OutputStream outputStream) throws NoTypeConversionAvailableException, IOException {
    CSVPrinter printer = new CSVPrinter(new OutputStreamWriter(outputStream, IOHelper.getCharsetName(exchange)), format);
    try {
        Iterator it = ObjectHelper.createIterator(object);
        while (it.hasNext()) {
            Object child = it.next();
            printer.printRecord(getRecordValues(exchange, child));
        }
    } finally {
        IOHelper.close(printer);
    }
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) Iterator(java.util.Iterator) OutputStreamWriter(java.io.OutputStreamWriter)

Example 43 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 44 with CSVPrinter

use of org.apache.commons.csv.CSVPrinter in project hmftools by hartwigmedical.

the class PatientCancerTypes method writeRecords.

public static void writeRecords(@NotNull final String outputPath, @NotNull final List<PatientCancerTypes> patientCancerTypes) throws IOException {
    final CSVFormat format = CSVFormat.DEFAULT.withNullString("").withHeader(Header.class);
    final CSVPrinter printer = new CSVPrinter(new FileWriter(outputPath), format);
    printer.printRecords(patientCancerTypes.stream().map(PatientCancerTypes::csvRecord).collect(Collectors.toList()));
    printer.close();
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) FileWriter(java.io.FileWriter) CSVFormat(org.apache.commons.csv.CSVFormat)

Example 45 with CSVPrinter

use of org.apache.commons.csv.CSVPrinter in project hmftools by hartwigmedical.

the class PortalClinicalData method writeRecords.

public static void writeRecords(@NotNull final String outputPath, @NotNull final List<PortalClinicalData> patientCancerTypes) throws IOException {
    final CSVFormat format = CSVFormat.DEFAULT.withHeader(Header.class);
    final CSVPrinter printer = new CSVPrinter(new FileWriter(outputPath), format);
    printer.printRecords(patientCancerTypes.stream().map(PortalClinicalData::csvRecord).collect(Collectors.toList()));
    printer.close();
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) FileWriter(java.io.FileWriter) CSVFormat(org.apache.commons.csv.CSVFormat)

Aggregations

CSVPrinter (org.apache.commons.csv.CSVPrinter)75 IOException (java.io.IOException)26 CSVFormat (org.apache.commons.csv.CSVFormat)21 OutputStreamWriter (java.io.OutputStreamWriter)18 StringWriter (java.io.StringWriter)15 ArrayList (java.util.ArrayList)15 Writer (java.io.Writer)13 FileOutputStream (java.io.FileOutputStream)12 Test (org.junit.Test)11 FileWriter (java.io.FileWriter)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)8 BufferedWriter (java.io.BufferedWriter)7 PrintWriter (java.io.PrintWriter)5 JSONArray (org.json.JSONArray)5 File (java.io.File)4 Map (java.util.Map)4 Word (ai.elimu.model.content.Word)3 Path (java.nio.file.Path)3 GZIPOutputStream (java.util.zip.GZIPOutputStream)3 Letter (ai.elimu.model.content.Letter)2