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;
}
}
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);
}
}
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;
}
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();
}
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();
}
Aggregations