Example usage for org.apache.commons.csv CSVParser forEach

List of usage examples for org.apache.commons.csv CSVParser forEach

Introduction

In this page you can find the example usage for org.apache.commons.csv CSVParser forEach.

Prototype

default void forEach(Consumer<? super T> action) 

Source Link

Document

Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

Usage

From source file:fi.vm.kapa.identification.adapter.service.AdapterPropertyMapper.java

public Map<String, String> getMapFromFromCsvFile(String filename, CSVFormat csvFormat) throws IOException {
    try (FileReader fileReader = new FileReader(new File(filename))) {
        CSVParser csvParser = new CSVParser(fileReader, csvFormat);
        Map<String, String> map = new HashMap<>();
        csvParser.forEach(record -> map.put(record.get(0), record.get(1)));
        return map;
    } catch (IOException e) {
        logger.error("Failed to open CSV file {} for reading", filename);
        throw e;/*from ww  w .j  a v a2s. c  om*/
    }
}

From source file:com.ibm.watson.catalyst.jumpqa.template.TemplateReader.java

@Override
public List<ITemplate> read(final String aString) {
    List<ITemplate> result = new ArrayList<ITemplate>();

    CSVParser parser;
    try {/*from w w  w .j a  v  a2s  .c  o m*/
        parser = CSVParser.parse(aString, format);
    } catch (IOException e) {
        throw new RuntimeException("IOError parsing CSV.", e);
    }
    final TemplateFactory tf = new TemplateFactory();
    parser.forEach((record) -> result.add(tf.readRecord(record)));

    logger.info("Read " + result.size() + " templates");
    return result;
}

From source file:fi.vm.kapa.identification.proxy.utils.SessionHandlingUtils.java

@PostConstruct
public void initSessionHandlingService() throws Exception {
    try {//from  w  w  w .ja v a2 s  .  com
        CSVParser csvParser = new CSVParser(new FileReader(new File(attributeMapFile)),
                CSVFormat.DEFAULT.withDelimiter(';').withCommentMarker('#'));

        CSVParser csvLegacyParser = new CSVParser(new FileReader(new File(legacyAttributeMapFile)),
                CSVFormat.DEFAULT.withDelimiter(';').withCommentMarker('#'));

        /* The attribute mapper files have the following syntax:
         * [SP-attribute-key];[External-attribute-mapper-key]
         */
        csvParser.forEach(record -> attributeMap.put(record.get(0), record.get(1)));
        csvLegacyParser.forEach(record -> legacyAttributeMap.put(record.get(0), record.get(1)));
    } catch (Exception e) {
        logger.error("Error initializing CSV parser", e);
    }
}

From source file:com.webtide.jetty.load.generator.jenkins.LoadGeneratorBuilder.java

protected void parseTimeValues(FilePath workspace, Path responseTimeResultFilePath,
        List<Resource.NodeListener> nodeListeners) throws Exception {
    Path responseTimeResultFile = Files.createTempFile("loadgenerator_result_responsetime", ".csv");

    workspace.child(responseTimeResultFilePath.toString())
            .copyTo(Files.newOutputStream(responseTimeResultFile));

    CSVParser csvParser = new CSVParser(Files.newBufferedReader(responseTimeResultFile),
            CSVFormat.newFormat('|'));

    csvParser.forEach(strings -> {
        Values values = new Values() //
                .eventTimestamp(Long.parseLong(strings.get(0))) //
                .method(strings.get(1)) //
                .path(strings.get(2)) //
                .status(Integer.parseInt(strings.get(3))) //
                .size(Long.parseLong(strings.get(4))) //
                .responseTime(Long.parseLong(strings.get(5))) //
                .latencyTime(Long.parseLong(strings.get(6)));

        for (Resource.NodeListener listener : nodeListeners) {
            listener.onResourceNode(values.getInfo());
        }//  www.  j  a  v  a 2s.  co  m
    });

    Files.deleteIfExists(responseTimeResultFile);
}