Search in sources :

Example 26 with CSVFormat

use of org.apache.commons.csv.CSVFormat in project bookish by parrt.

the class DataTable method parseCSV.

public void parseCSV(String csv) {
    try {
        Reader in = new StringReader(csv);
        CSVFormat format = CSVFormat.EXCEL.withHeader();
        CSVParser parser = format.parse(in);
        this.firstColIsIndex = false;
        for (CSVRecord record : parser) {
            if (!firstColIsIndex && Character.isAlphabetic(record.get(0).charAt(0))) {
                // latch if we see alpha not number
                firstColIsIndex = true;
            }
            List<String> row = new ArrayList<>();
            for (int i = 0; i < record.size(); i++) {
                String v = record.get(i);
                if (!NumberUtils.isDigits(v) && NumberUtils.isCreatable(v)) {
                    v = String.format("%.4f", Precision.round(Double.valueOf(v), 4));
                } else {
                    v = abbrevString(v, 25);
                }
                row.add(v);
            }
            rows.add(row);
        }
        Set<String> colNames = parser.getHeaderMap().keySet();
        if (!firstColIsIndex) {
            // remove index column name
            colNames.remove("");
        }
        this.colNames.addAll(colNames);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : CSVParser(org.apache.commons.csv.CSVParser) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) Reader(java.io.Reader) CSVFormat(org.apache.commons.csv.CSVFormat) CSVRecord(org.apache.commons.csv.CSVRecord) ParrtStrings.abbrevString(us.parr.lib.ParrtStrings.abbrevString)

Example 27 with CSVFormat

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

the class CassandraDbHelper method loadCf.

public static void loadCf(KeyspaceMetadata ks, Session session, String cfName, String[] columns, Path sourceFile, boolean parseHeader) throws Exception {
    TableMetadata tableMetadata = ks.getTable(cfName);
    PreparedStatement prepared = session.prepare(createInsertStatement(cfName, columns));
    CSVFormat csvFormat = CSV_DUMP_FORMAT;
    if (parseHeader) {
        csvFormat = csvFormat.withFirstRecordAsHeader();
    } else {
        csvFormat = CSV_DUMP_FORMAT.withHeader(columns);
    }
    try (CSVParser csvParser = new CSVParser(Files.newBufferedReader(sourceFile), csvFormat)) {
        csvParser.forEach(record -> {
            BoundStatement boundStatement = prepared.bind();
            for (String column : columns) {
                setColumnValue(tableMetadata, column, record, boundStatement);
            }
            session.execute(boundStatement);
        });
    }
}
Also used : CSVParser(org.apache.commons.csv.CSVParser) CSVFormat(org.apache.commons.csv.CSVFormat)

Example 28 with CSVFormat

use of org.apache.commons.csv.CSVFormat 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) HashMap(java.util.HashMap) CSVFormat(org.apache.commons.csv.CSVFormat)

Example 29 with CSVFormat

use of org.apache.commons.csv.CSVFormat in project fql by CategoricalData.

the class ToCsvPragmaInstance method getFormat.

public static CSVFormat getFormat(AqlOptions op) {
    String format0 = "Default";
    CSVFormat format = CSVFormat.valueOf(format0);
    format = format.withDelimiter((Character) op.getOrDefault(AqlOption.csv_field_delim_char));
    format = format.withQuote((Character) op.getOrDefault(AqlOption.csv_quote_char));
    format = format.withEscape((Character) op.getOrDefault(AqlOption.csv_escape_char));
    format = format.withQuoteMode(QuoteMode.ALL);
    format = format.withNullString(null);
    return format;
}
Also used : CSVFormat(org.apache.commons.csv.CSVFormat)

Example 30 with CSVFormat

use of org.apache.commons.csv.CSVFormat in project opentest by mcdcorp.

the class ReadCsv method run.

@Override
public void run() {
    super.run();
    String filePath = this.readStringArgument("file", null);
    String csvString = this.readStringArgument("csvString", null);
    String delimiter = this.readStringArgument("delimiter", null);
    String escapeChar = this.readStringArgument("escapeChar", null);
    String recordSeparator = this.readStringArgument("recordSeparator", null);
    Boolean excludeBom = this.readBooleanArgument("excludeBom", Boolean.TRUE);
    Boolean hasHeader = this.readBooleanArgument("hasHeader", Boolean.FALSE);
    String format = this.readStringArgument("format", "default");
    List<String> fieldNames = this.readArrayArgument("fieldNames", String.class, null);
    try {
        Reader csvReader;
        if (filePath != null) {
            if (excludeBom) {
                csvReader = new InputStreamReader(new BOMInputStream(new FileInputStream(filePath)), CharEncoding.UTF_8);
            } else {
                csvReader = Files.newBufferedReader(Paths.get(filePath), Charsets.UTF_8);
            }
        } else if (csvString != null) {
            csvReader = new StringReader(csvString);
        } else {
            throw new RuntimeException("Neither the \"file\" argument, nor the \"csv\" argument were provided.");
        }
        CSVFormat csvFormat = this.getCsvFormat(format);
        if (hasHeader) {
            csvFormat = csvFormat.withFirstRecordAsHeader();
        }
        if (delimiter != null) {
            csvFormat = csvFormat.withDelimiter(delimiter.charAt(0));
        }
        if (escapeChar != null) {
            csvFormat = csvFormat.withEscape(escapeChar.charAt(0));
        }
        if (recordSeparator != null) {
            csvFormat = csvFormat.withRecordSeparator(recordSeparator);
        }
        List<Map<String, String>> recordsArray = new ArrayList<>();
        CSVParser parser = csvFormat.parse(csvReader);
        Iterable<CSVRecord> records = (Iterable<CSVRecord>) parser;
        for (CSVRecord record : records) {
            if (hasHeader) {
                recordsArray.add(record.toMap());
            } else {
                Map<String, String> recordsMap = new HashMap<>();
                Iterator<String> fields = record.iterator();
                int columnNo = 1;
                while (fields.hasNext()) {
                    String field = fields.next();
                    if (fieldNames != null && fieldNames.size() >= columnNo && fieldNames.get(columnNo - 1) != null) {
                        recordsMap.put(fieldNames.get(columnNo - 1).trim(), field);
                    } else {
                        recordsMap.put(String.format("col%s", columnNo), field);
                    }
                    columnNo++;
                }
                recordsArray.add(recordsMap);
            }
        }
        this.writeOutput("header", parser.getHeaderMap());
        this.writeOutput("records", recordsArray);
    } catch (Exception ex) {
        throw new RuntimeException("Failed to parse CSV", ex);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) FileInputStream(java.io.FileInputStream) BOMInputStream(org.apache.commons.io.input.BOMInputStream) CSVParser(org.apache.commons.csv.CSVParser) StringReader(java.io.StringReader) CSVFormat(org.apache.commons.csv.CSVFormat) CSVRecord(org.apache.commons.csv.CSVRecord) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

CSVFormat (org.apache.commons.csv.CSVFormat)59 IOException (java.io.IOException)23 CSVRecord (org.apache.commons.csv.CSVRecord)22 CSVParser (org.apache.commons.csv.CSVParser)19 ArrayList (java.util.ArrayList)14 StringReader (java.io.StringReader)13 CSVPrinter (org.apache.commons.csv.CSVPrinter)10 InputStream (java.io.InputStream)9 InputStreamReader (java.io.InputStreamReader)8 HashMap (java.util.HashMap)8 SimpleRecordSchema (org.apache.nifi.serialization.SimpleRecordSchema)8 RecordField (org.apache.nifi.serialization.record.RecordField)8 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)8 Test (org.junit.Test)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 Reader (java.io.Reader)7 LinkedHashMap (java.util.LinkedHashMap)7 SchemaNameAsAttribute (org.apache.nifi.schema.access.SchemaNameAsAttribute)7 MapRecord (org.apache.nifi.serialization.record.MapRecord)7 Record (org.apache.nifi.serialization.record.Record)7