Search in sources :

Example 36 with CSVRecord

use of org.apache.commons.csv.CSVRecord in project bootique-jdbc by bootique.

the class CsvReader method loadDataSet.

TableDataSet loadDataSet(Reader dataReader) throws IOException {
    try (CSVParser parser = new CSVParser(dataReader, CSVFormat.DEFAULT, 0, 0)) {
        Iterator<CSVRecord> rows = parser.iterator();
        if (!rows.hasNext()) {
            return new TableDataSet(table, Collections.emptyList(), Collections.emptyList());
        }
        List<Column> header = getHeader(rows.next());
        return readData(header, rows);
    }
}
Also used : Column(io.bootique.jdbc.test.Column) CSVParser(org.apache.commons.csv.CSVParser) CSVRecord(org.apache.commons.csv.CSVRecord)

Example 37 with CSVRecord

use of org.apache.commons.csv.CSVRecord in project onebusaway-application-modules by camsys.

the class FixedRouteParserServiceImpl method parseFixedRouteReportFile.

/**
 * Parses a FixedRouteDataValidation report file into a List of
 * DataValidationMode objects.
 *
 * @param fixedRouteReportFile name of the file containing the report
 *                             in csv format.
 * @return a List of DataValidationMode objects containing the report data.
 */
@Override
public List<DataValidationMode> parseFixedRouteReportFile(File fixedRouteReportFile) {
    List<DataValidationMode> parsedModes = new ArrayList<>();
    if (fixedRouteReportFile != null && fixedRouteReportFile.exists()) {
        DataValidationMode currentMode = null;
        try {
            Reader in = new FileReader(fixedRouteReportFile);
            int i = 0;
            for (CSVRecord record : CSVFormat.DEFAULT.parse(in)) {
                if (i == 0) {
                    i++;
                    // Skip the first record, which is just the column headers
                    continue;
                }
                // When the record being parsed is for a new mode, the parseRecord
                // method will add the previous mode to the parsedModes List.
                currentMode = parseRecord(record, currentMode, parsedModes);
                i++;
            }
        } catch (FileNotFoundException e) {
            _log.info("Exception parsing csv file " + fixedRouteReportFile, e);
            e.printStackTrace();
        } catch (IOException e) {
            _log.info("Exception parsing csv file " + fixedRouteReportFile, e);
            e.printStackTrace();
        }
        // Add in the last mode processed.
        parsedModes.add(currentMode);
    }
    return parsedModes;
}
Also used : DataValidationMode(org.onebusaway.admin.model.ui.DataValidationMode) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) FileReader(java.io.FileReader) FileReader(java.io.FileReader) CSVRecord(org.apache.commons.csv.CSVRecord) IOException(java.io.IOException)

Example 38 with CSVRecord

use of org.apache.commons.csv.CSVRecord in project LogHub by fbacchella.

the class FileMap method mapFromCsv.

private Map<Object, Object> mapFromCsv() {
    try (Reader in = new FileReader(mappingFile)) {
        CSVFormat format;
        switch(csvFormat.toUpperCase()) {
            case "EXCEL":
                format = CSVFormat.EXCEL;
                break;
            case "RFC4180":
                format = CSVFormat.RFC4180;
                break;
            case "TDF":
                format = CSVFormat.TDF;
                break;
            case "DEFAULT":
                format = CSVFormat.DEFAULT;
                break;
            default:
                logger.error("Unknown CSV format name");
                return null;
        }
        boolean withHeaders;
        if (key != null && value != null) {
            format = format.withFirstRecordAsHeader();
            withHeaders = true;
        } else if (keyColumn > 0 && valueColumn > 0) {
            format = format.withSkipHeaderRecord(false);
            withHeaders = false;
        } else {
            logger.error("Neither column name or number defined");
            return null;
        }
        Iterable<CSVRecord> records = format.parse(in);
        Map<Object, Object> mapping = new HashMap<>();
        for (CSVRecord record : records) {
            if (withHeaders) {
                mapping.put(record.get(key), record.get(value));
            } else {
                mapping.put(record.get(keyColumn), record.get(valueColumn));
            }
        }
        return mapping;
    } catch (IOException e) {
        logger.error("Can't read mapping file {}", mappingFile);
        return null;
    }
}
Also used : HashMap(java.util.HashMap) FileReader(java.io.FileReader) Reader(java.io.Reader) FileReader(java.io.FileReader) CSVFormat(org.apache.commons.csv.CSVFormat) CSVRecord(org.apache.commons.csv.CSVRecord) IOException(java.io.IOException)

Example 39 with CSVRecord

use of org.apache.commons.csv.CSVRecord in project structr by structr.

the class FromCsvFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) {
    if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 1, 5)) {
        try {
            final List<Map<String, String>> objects = new LinkedList<>();
            final String source = sources[0].toString();
            String delimiter = ";";
            String quoteChar = "\"";
            String recordSeparator = "\n";
            boolean customColumnNamesSupplied = false;
            switch(sources.length) {
                case 5:
                    customColumnNamesSupplied = (sources[4] instanceof Collection);
                case 4:
                    recordSeparator = (String) sources[3];
                case 3:
                    quoteChar = (String) sources[2];
                case 2:
                    delimiter = (String) sources[1];
                    break;
            }
            CSVFormat format = CSVFormat.newFormat(delimiter.charAt(0));
            if (customColumnNamesSupplied) {
                format = format.withHeader(((Collection<String>) sources[4]).toArray(new String[] {})).withSkipHeaderRecord(false);
            } else {
                format = format.withHeader().withSkipHeaderRecord(true);
            }
            format = format.withQuote(quoteChar.charAt(0));
            format = format.withRecordSeparator(recordSeparator);
            format = format.withIgnoreEmptyLines(true);
            format = format.withIgnoreSurroundingSpaces(true);
            format = format.withQuoteMode(QuoteMode.ALL);
            CSVParser parser = new CSVParser(new StringReader(source), format);
            for (final CSVRecord record : parser.getRecords()) {
                objects.add(record.toMap());
            }
            return objects;
        } catch (Throwable t) {
            logException(t, "{}: Exception for parameter: {}", new Object[] { getName(), getParametersAsString(sources) });
        }
        return "";
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
    }
    return usage(ctx.isJavaScriptContext());
}
Also used : CSVParser(org.apache.commons.csv.CSVParser) StringReader(java.io.StringReader) Collection(java.util.Collection) CSVFormat(org.apache.commons.csv.CSVFormat) CSVRecord(org.apache.commons.csv.CSVRecord) Map(java.util.Map) LinkedList(java.util.LinkedList)

Example 40 with CSVRecord

use of org.apache.commons.csv.CSVRecord 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)

Aggregations

CSVRecord (org.apache.commons.csv.CSVRecord)96 CSVParser (org.apache.commons.csv.CSVParser)47 IOException (java.io.IOException)26 ArrayList (java.util.ArrayList)23 CSVFormat (org.apache.commons.csv.CSVFormat)23 StringReader (java.io.StringReader)19 FileReader (java.io.FileReader)15 Test (org.junit.Test)13 InputStreamReader (java.io.InputStreamReader)12 PreparedStatement (java.sql.PreparedStatement)10 InputStream (java.io.InputStream)9 Reader (java.io.Reader)9 ResultSet (java.sql.ResultSet)9 HashMap (java.util.HashMap)9 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)9 CSVCommonsLoader (org.apache.phoenix.util.CSVCommonsLoader)9 File (java.io.File)6 Map (java.util.Map)6 User (org.eclipse.sw360.datahandler.thrift.users.User)6 FileNotFoundException (java.io.FileNotFoundException)5