org.supercsv.io.CsvMapReader Java Examples

The following examples show how to use org.supercsv.io.CsvMapReader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CsvUtils.java    From website with GNU Affero General Public License v3.0 6 votes vote down vote up
public static List<String> getCsvLines(String filename, String[] headers, boolean skipHeader) throws Exception {

        try (CsvMapReader csvReader = new CsvMapReader(new FileReader(filename), CsvPreference.STANDARD_PREFERENCE)){
            List<String> result = new ArrayList<String>();
            if (skipHeader) {
                csvReader.getHeader(true);
            }
           
            while (csvReader.read(headers) != null ) {
               String rawRow = csvReader.getUntokenizedRow();
               result.add(rawRow);
            }
            return result;
        }  

    }
 
Example #2
Source File: DtaParser.java    From ipst with Mozilla Public License 2.0 5 votes vote down vote up
public static Map<String, String> readWithCsvMapReader(Path dicoFile) throws Exception {

        Map<String, String> retMap = new HashMap<>();
        try (ICsvMapReader mapReader = new CsvMapReader(Files.newBufferedReader(dicoFile, StandardCharsets.UTF_8), CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE)) {
            final String[] header = mapReader.getHeader(true);
            LOGGER.debug(" cvsheader length: " + header.length);
            final CellProcessor[] rowProcessors = new CellProcessor[header.length];
            for (int i = 0; i < rowProcessors.length; i++) {
                if (i == 0) {
                    rowProcessors[i] = new NotNull();
                } else {
                    rowProcessors[i] = null;
                }
            }

            Map<String, Object> componentMap;
            while ((componentMap = mapReader.read(header, rowProcessors)) != null) {
                //System.out.println(String.format("lineNo=%s, rowNo=%s, mapping=%s", mapReader.getLineNumber(), mapReader.getRowNumber(), customerMap));
                String eurostagId = (String) componentMap.get(header[1]);
                String cimId = (String) componentMap.get(header[0]);
                if (eurostagId == null) {
                    LOGGER.warn("eurostagId=" + eurostagId + ", cimId=" + cimId);
                } else {
                    if (retMap.containsKey(eurostagId)) {
                        LOGGER.warn("eurostagId=" + eurostagId + " already in the map");
                    }
                    retMap.put(eurostagId, cimId);
                }
            }
        }

        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("ids map: " + retMap);
        }
        return retMap;

    }
 
Example #3
Source File: ITER_CSVHeaders.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
@Override
public List<List<NodeValue>> exec(NodeValue csv) {
    if (csv.getDatatypeURI() != null
            && !csv.getDatatypeURI().equals(datatypeUri)
            && !csv.getDatatypeURI().equals("http://www.w3.org/2001/XMLSchema#string")) {
        LOG.debug("The URI of NodeValue1 MUST be <" + datatypeUri + ">"
                + "or <http://www.w3.org/2001/XMLSchema#string>."
        );
    }
    try {
        String sourceCSV = String.valueOf(csv.asNode().getLiteralLexicalForm());

        InputStream is = new ByteArrayInputStream(sourceCSV.getBytes("UTF-8"));
        BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));

        CsvMapReader mapReader = new CsvMapReader(br, CsvPreference.STANDARD_PREFERENCE);
        String headers_str[] = mapReader.getHeader(true);

        final List<List<NodeValue>> listNodeValues = new ArrayList<>();
        Node node;
        NodeValue nodeValue;
        for (String header : headers_str) {
            node = NodeFactory.createLiteral(header);
            nodeValue = new NodeValueNode(node);
            listNodeValues.add(Collections.singletonList(nodeValue));
        }
        return listNodeValues;
    } catch (Exception ex) {
        if(LOG.isDebugEnabled()) {
            Node compressed = LogUtils.compress(csv.asNode());
            LOG.debug("No evaluation for " + compressed, ex);
        }
        throw new ExprEvalException("No evaluation ", ex);
    }
}
 
Example #4
Source File: CsvParser.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
@Override
public void setup(OperatorContext context)
{
  delimitedParserSchema = new DelimitedSchema(schema);
  preference = new CsvPreference.Builder(delimitedParserSchema.getQuoteChar(),
      delimitedParserSchema.getDelimiterChar(), delimitedParserSchema.getLineDelimiter()).build();
  nameMapping = delimitedParserSchema.getFieldNames().toArray(
      new String[delimitedParserSchema.getFieldNames().size()]);
  header = StringUtils.join(nameMapping, (char)delimitedParserSchema.getDelimiterChar() + "");
  processors = getProcessor(delimitedParserSchema.getFields());
  csvStringReader = new ReusableStringReader();
  csvMapReader = new CsvMapReader(csvStringReader, preference);
  csvBeanReader = new CsvBeanReader(csvStringReader, preference);
}
 
Example #5
Source File: DelimitedFSLoader.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
private void init()
{

  delimitedParserSchema = new DelimitedSchema(schema);
  preference = new CsvPreference.Builder(delimitedParserSchema.getQuoteChar(),
      delimitedParserSchema.getDelimiterChar(), delimitedParserSchema.getLineDelimiter()).build();
  nameMapping = delimitedParserSchema.getFieldNames()
      .toArray(new String[delimitedParserSchema.getFieldNames().size()]);
  header = StringUtils.join(nameMapping, (char)delimitedParserSchema.getDelimiterChar() + "");
  processors = getProcessor(delimitedParserSchema.getFields());
  csvStringReader = new ReusableStringReader();
  csvMapReader = new CsvMapReader(csvStringReader, preference);
}
 
Example #6
Source File: ReadingFeaturesTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetByColumnName() throws IOException {
	String csv = "John,Connor";
	String[] mapping = { "firstName", "lastName" };
	
	CsvMapReader mapReader = new CsvMapReader(new StringReader(csv), STANDARD_PREFERENCE);
	Map<String, String> line = mapReader.read(mapping);
	
	Assert.assertNotNull(line);
	Assert.assertEquals(2, line.size());
	Assert.assertEquals("John", line.get("firstName"));
	Assert.assertEquals("Connor", line.get("lastName"));
}
 
Example #7
Source File: DdbDyrLoader.java    From ipst with Mozilla Public License 2.0 4 votes vote down vote up
private Map<String, String> readWithCsvMapReader(Path mappingFile) throws IOException {
    Map<String, String> retMap = new HashMap<>();
    try (ICsvMapReader mapReader = new CsvMapReader(Files.newBufferedReader(mappingFile, StandardCharsets.UTF_8), CsvPreference.STANDARD_PREFERENCE)) {
        final String[] header = mapReader.getHeader(true);
        log.info(" cvsheader length: " + header.length);
        final CellProcessor[] rowProcessors = new CellProcessor[header.length];
        for (int i = 0; i < rowProcessors.length; i++) {
            if (i == 0) {
                rowProcessors[i] = new NotNull();
            } else {
                rowProcessors[i] = new NotNull();
            }
        }

        Map<String, Object> componentMap;
        while ((componentMap = mapReader.read(header, rowProcessors)) != null) {
            String psseId = (String) componentMap.get(header[0]);
            String rdfId = (String) componentMap.get(header[1]);
            if (psseId == null) {
                log.warn("psseId=" + psseId + ", rdfId=" + rdfId);
            } else {
                if (retMap.containsKey(psseId)) {
                    log.warn("psseId=" + psseId + " already in the map");
                }
                retMap.put(psseId, rdfId);
            }
        }
    }

    if (log.isTraceEnabled()) {
        log.trace("ids map: " + retMap);
    }
    log.info("ids map: " + retMap);
    return retMap;
}
 
Example #8
Source File: CSVParserTest.java    From attic-apex-malhar with Apache License 2.0 3 votes vote down vote up
/**
 * This method creates an instance of csvMapReader.
 *
 * @param reader
 * @param preference
 * @return CSV Map Reader
 */
@Override
protected ICsvMapReader getReader(ReusableStringReader reader, CsvPreference preference)
{
  csvReader = new CsvMapReader(reader, preference);
  return csvReader;
}
 
Example #9
Source File: CsvUtil.java    From openscoring with GNU Affero General Public License v3.0 3 votes vote down vote up
static
public TableEvaluationRequest readTable(BufferedReader reader, CsvPreference format) throws IOException {
	CsvMapReader parser = new CsvMapReader(reader, format);

	String[] header = parser.getHeader(true);

	List<String> columns = Arrays.asList(header);

	TableEvaluationRequest tableRequest = new TableEvaluationRequest()
		.setColumns(columns);

	String idColumn = tableRequest.getIdColumn();

	List<EvaluationRequest> requests = new ArrayList<>();

	while(true){
		Map<String, String> row = parser.read(header);
		if(row == null){
			break;
		}

		String id = null;

		if(idColumn != null){
			id = row.remove(idColumn);
		}

		EvaluationRequest request = new EvaluationRequest(id)
			.setArguments(row);

		requests.add(request);
	}

	tableRequest.setRequests(requests);

	parser.close();

	return tableRequest;
}