Search in sources :

Example 1 with CSVReaderBuilder

use of com.opencsv.CSVReaderBuilder in project fql by CategoricalData.

the class InstExpCsv method start2.

/*
	 * @Override public boolean equals(Object obj) { return (obj instanceof
	 * InstExpCsv) && super.equals(obj); }
	 */
/**
 * Expects filenames in the map
 */
public static Map<En, List<String[]>> start2(Map<String, String> map, AqlOptions op, Schema<Ty, En, Sym, Fk, Att> sch, boolean omitCheck) throws Exception {
    Character sepChar = (Character) op.getOrDefault(AqlOption.csv_field_delim_char);
    Character quoteChar = (Character) op.getOrDefault(AqlOption.csv_quote_char);
    Character escapeChar = (Character) op.getOrDefault(AqlOption.csv_escape_char);
    final CSVParser parser = new CSVParserBuilder().withSeparator(sepChar).withQuoteChar(quoteChar).withEscapeChar(escapeChar).withFieldAsNull(CSVReaderNullFieldIndicator.EMPTY_SEPARATORS).build();
    Map<En, List<String[]>> ret = new HashMap<>();
    for (String k : map.keySet()) {
        if (!omitCheck) {
            if (!sch.ens.contains(new En(k))) {
                throw new RuntimeException("Not an entity: " + k);
            }
        }
        File file = new File(map.get(k));
        FileReader fileReader = new FileReader(file);
        final CSVReader reader = new CSVReaderBuilder(fileReader).withCSVParser(parser).withFieldAsNull(CSVReaderNullFieldIndicator.EMPTY_SEPARATORS).build();
        List<String[]> rows = reader.readAll();
        fileReader.close();
        ret.put(new En(k), rows);
    }
    if (!omitCheck) {
        for (En en : sch.ens) {
            if (!ret.containsKey(en)) {
                ret.put(en, new LinkedList<>(Util.singList(Util.union(sch.attsFrom(en).stream().map(Object::toString).collect(Collectors.toList()), sch.fksFrom(en).stream().map(Object::toString).collect(Collectors.toList())).toArray(new String[0]))));
            }
        }
    }
    return ret;
}
Also used : HashMap(java.util.HashMap) CSVReader(com.opencsv.CSVReader) CSVReaderBuilder(com.opencsv.CSVReaderBuilder) En(catdata.aql.exp.SchExpRaw.En) CSVParser(com.opencsv.CSVParser) LinkedList(java.util.LinkedList) List(java.util.List) FileReader(java.io.FileReader) CSVParserBuilder(com.opencsv.CSVParserBuilder) File(java.io.File)

Aggregations

En (catdata.aql.exp.SchExpRaw.En)1 CSVParser (com.opencsv.CSVParser)1 CSVParserBuilder (com.opencsv.CSVParserBuilder)1 CSVReader (com.opencsv.CSVReader)1 CSVReaderBuilder (com.opencsv.CSVReaderBuilder)1 File (java.io.File)1 FileReader (java.io.FileReader)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1