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

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

Introduction

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

Prototype

public CSVParser(final Reader reader, final CSVFormat format) throws IOException 

Source Link

Document

Customized CSV parser using the given CSVFormat

If you do not read all records from the given reader , you should call #close() on the parser, unless you close the reader .

Usage

From source file:edu.emory.mathcs.nlp.zzz.CSVSentiment.java

public void toTXT(String inputFile) throws Exception {
    CSVParser parser = new CSVParser(IOUtils.createBufferedReader(inputFile), CSVFormat.DEFAULT);
    PrintStream fout = IOUtils.createBufferedPrintStream(inputFile + ".txt");
    List<CSVRecord> records = parser.getRecords();
    CSVRecord record;/*from  w w  w  .  j  av  a 2 s .c  o m*/

    System.out.println(inputFile);

    for (int i = 0; i < records.size(); i++) {
        if (i == 0)
            continue;
        record = records.get(i);
        fout.println(record.get(6));
    }

    fout.close();
    parser.close();
}

From source file:mtsar.resources.WorkerResource.java

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response postWorkersCSV(@Context UriInfo uriInfo, @FormDataParam("file") InputStream stream)
        throws IOException {
    try (final Reader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
        try (final CSVParser csv = new CSVParser(reader, WorkerCSV.FORMAT)) {
            workerDAO.insert(WorkerCSV.parse(stage, csv));
        }//from www.  ja v a2s .c o m
    }
    workerDAO.resetSequence();
    return Response.seeOther(getWorkersURI(uriInfo)).build();
}

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;// w  ww  . java  2 s. com
    }
}

From source file:edu.si.sidora.tabularmetadata.TabularScannerTest.java

@Test
public void testOperation() throws IOException {
    try (Reader reader = new FileReader(smalltestfile);
            final CSVParser parser = new CSVParser(reader, DEFAULT.withHeader())) {
        log.debug("Found header map: {}", parser.getHeaderMap());
        final TabularScanner testScanner = new TabularScanner(parser.iterator(), mockTypeStrategy,
                mockRangeStrategy, mockEnumStrategy);
        testScanner.scan(0);//from  w  ww.  ja  v  a  2  s.  c o m
        final List<DataType> guesses = testScanner.getTypeStrategies().stream().map(Heuristic::results)
                .collect(toList());
        assertEquals("Failed to find the correct column types!", expectedResults, guesses);
    }
}

From source file:com.hack23.cia.service.external.vdem.impl.VdemServiceImpl.java

/**
 * Gets the country question data.//from  w  w w.  j  a v  a 2  s .  c o m
 *
 * @return the country question data
 */
@Override
public List<CountryQuestionData> getCountryQuestionData() {
    final List<CountryQuestionData> list = new ArrayList<>();

    final List<Question> questions = getQuestions();

    try {
        final Reader in = new InputStreamReader(new URL(VDEM_DATA_DOWNLOAD_URL).openStream());

        final CSVParser parser = new CSVParser(in, CSVFormat.EXCEL.withHeader().withDelimiter(','));

        for (final CSVRecord record : parser) {
            final String countryName = record.get("country_name");
            final String countryId = record.get("country_id");
            final String countryTextId = record.get("country_text_id");
            final String year = record.get("year");
            final String gapStart = record.get("gapstart");
            final String gapEnd = record.get("gapend");
            final String codingEnd = record.get("codingend");
            final String cowCode = record.get("COWcode");

            final int currentSize = list.size();
            LOGGER.info("Loading vdem data for country:{} year {} ", countryName, year);

            for (final Question question : questions) {
                addQuestionDataToList(list, record, countryName, countryId, countryTextId, year, gapStart,
                        gapEnd, codingEnd, cowCode, question);
            }

            final int afterSize = list.size();
            LOGGER.info("Found vdem data for country:{} year:{} data points:{}", countryName, year,
                    afterSize - currentSize);

        }
        parser.close();

    } catch (final IOException e) {

        LOGGER.warn("Problem loading vdem data", e);
    }

    return list;
}

From source file:core.reporting.ImportFromFile.java

private void validateCSVFile() {
    Iterable<CSVRecord> inrlst = null;
    try {//from ww w.  ja v  a2  s  . co  m
        Reader in = new FileReader(csvInputFile);
        inrlst = (new CSVParser(in, CSVFormat.EXCEL.withHeader()).getRecords());
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }

    int err = validateRecord(inrlst);
    // error found, show errors in system log
    ServiceRequest sr = null;
    putClientProperty(TConstants.JTABLE_AUTO_RESIZE_MODE, JTable.AUTO_RESIZE_OFF);
    if (err > 0) {
        String wc = "t_sluserid = '" + Session.getUserName() + "' AND t_slflag = 'ie'";
        sr = new ServiceRequest(ServiceRequest.DB_QUERY, "t_system_log", wc);
        putClientProperty(TConstants.ICON_PARAMETERS, "0;;t_sltype");
        putClientProperty(TConstants.SHOW_COLUMNS, "t_slmessage");
        saveAction.setEnabled(false);

    } else {
        sr = new ServiceRequest(ServiceRequest.CLIENT_GENERATED_LIST, "", tempBuffer);
        sr.setParameter(ServiceResponse.RECORD_MODEL,
                ConnectionManager.getAccessTo(recordModel.getTableName()).getModel());
        putClientProperty(TConstants.ICON_PARAMETERS, "-1;aa");
        putClientProperty(TConstants.SHOW_COLUMNS, fileColumns);
        saveAction.setEnabled(true);
    }
    setServiceRequest(sr);

    /*
     * File f = (File) evt.getNewValue(); Record rcon =
     * ConnectionManager.getAccessTo("t_connections").exist("t_cnname = 'CsvJdbc'"); String u = (String)
     * rcon.getFieldValue("t_cnurl"); // rcon.setFieldValue("t_cnurl", u.replace("<filename>",
     * f.getAbsolutePath())); rcon.setFieldValue("t_cnurl", u.replace("<filename>", f.getParent()));
     * ConnectionManager.connect(rcon); String tn = "CsvJdbc." + f.getName().split("[.]")[0]; Record rm =
     * ConnectionManager.getAccessTo(tn).getModel(); ServiceRequest sr = new ServiceRequest(ServiceRequest.DB_QUERY,
     * tn, null); sr.setParameter(ServiceRequest.ORDER_BY, "ficha"); setServiceRequest(sr);
     */

}

From source file:com.ibm.util.merge.directive.provider.ProviderTag.java

/**
 * Reset the table, and if the Tag exists, add a row with the tag name/value
 * @param cf//from w w w  .j av a2 s .c  o m
 */
@Override
public void getData(MergeContext rtc) throws MergeException {
    reset();
    DataTable table = addNewTable();
    Template template = getDirective().getTemplate();
    String theTag = Template.wrap(tag);
    log.info("Getting Tag Data for " + tag);

    switch (condition) {
    case ProviderTag.CONDITION_EXISTS:
        if (!template.hasReplaceKey(theTag)) {
            log.info("Tag not found for Exists Condition");
            return;
        }
        break;
    case ProviderTag.CONDITION_BLANK:
        if (!template.hasReplaceKey(theTag) || template.hasReplaceValue(theTag)) {
            log.info("Tag not found or Data found for Blank Condition");
            return;
        }
        break;
    case ProviderTag.CONDITION_NONBLANK:
        if (!template.hasReplaceKey(theTag) || !template.hasReplaceValue(theTag)) {
            log.info("Tag or Empty Data found for Non-Blank Condition");
            return;
        }
        break;
    case ProviderTag.CONDITION_EQUALS:
        if (!template.hasReplaceKey(theTag) || !template.hasReplaceValue(theTag)
                || !template.getReplaceValue(theTag).equals(value)) {
            log.info("Tag not Equals or not found");
            return;
        }
        break;
    }

    // We have a match, so add data
    String data = template.getReplaceValue(Template.wrap(tag));
    log.info("Data Found: " + data);
    table.addCol(tag);
    if (isList()) {
        CSVParser parser;
        try {
            parser = new CSVParser(new StringReader(data), CSVFormat.EXCEL.withHeader());
            for (String colName : parser.getHeaderMap().keySet()) {
                ArrayList<String> row = table.addNewRow();
                row.add(colName);
            }
            parser.close();
        } catch (IOException e) {
            throw new MergeException(this, e, "CSV Parser Stringreader IO Exception", data);
        }
    } else {
        ArrayList<String> row = table.addNewRow();
        row.add(data);
    }
}

From source file:com.xceptance.xlt.common.tests.AbstractURLTestCase.java

/**
 * Loading of the data. There is a state variable used to indicate that we already did that.
 * /*from  w w w.  j  a  v a 2s .c  o m*/
 * @throws IOException
 */
@Before
public void loadData() throws IOException {
    login = getProperty("login", getProperty("com.xceptance.xlt.auth.userName"));
    password = getProperty("password", getProperty("com.xceptance.xlt.auth.password"));

    // load the data. Ideally we would offload the file searching to
    // XltProperties.getDataFile(String name)
    // or XltProperties.getDataFile(String name, String locale)
    // or XltProperties.getDataFile(String name, Locale locale)
    final String dataDirectory = XltProperties.getInstance().getProperty(
            XltConstants.XLT_PACKAGE_PATH + ".data.directory", "config" + File.separatorChar + "data");
    final File file = new File(dataDirectory,
            getProperty("filename", Session.getCurrent().getUserName() + ".csv"));

    BufferedReader br = null;
    boolean incorrectLines = false;

    try {
        br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));

        // permit # as comment, empty lines, set comma as separator, and activate the header
        final CSVFormat csvFormat = CSVFormat.RFC4180.toBuilder().withIgnoreEmptyLines(true)
                .withCommentStart('#').withHeader().withIgnoreSurroundingSpaces(true).build();
        final CSVParser parser = new CSVParser(br, csvFormat);
        final Iterator<CSVRecord> csvRecords = parser.iterator();

        // verify header fields to avoid problems with incorrect spelling or spaces
        final Map<String, Integer> headerMap = parser.getHeaderMap();

        for (final String headerField : headerMap.keySet()) {
            if (!CSVBasedURLAction.isPermittedHeaderField(headerField)) {
                Assert.fail(MessageFormat.format("Unsupported or misspelled header field: {0}", headerField));
            }
        }

        // go over all lines, this is a little odd, because we have to catch the iterator exception
        while (true) {
            try {
                final boolean hasNext = csvRecords.hasNext();
                if (!hasNext) {
                    break;
                }
            } catch (final Exception e) {
                // the plus 1 is meant to correct the increment missing because of the exception
                throw new RuntimeException(
                        MessageFormat.format("Line at {0} is invalid, because of <{1}>. Line is ignored.",
                                parser.getLineNumber() + 1, e.getMessage()));
            }

            final CSVRecord csvRecord = csvRecords.next();

            // only take ok lines
            if (csvRecord.isConsistent()) {
                // guard against data exceptions
                try {
                    // do we have an url?
                    if (csvRecord.get(CSVBasedURLAction.URL) != null) {
                        // take it
                        csvBasedActions.add(new CSVBasedURLAction(csvRecord, interpreter));
                    } else {
                        XltLogger.runTimeLogger.error(MessageFormat.format(
                                "Line at {0} does not contain any URL. Line is ignored: {1}",
                                parser.getLineNumber(), csvRecord));
                    }
                } catch (final Exception e) {
                    throw new RuntimeException(MessageFormat.format(
                            "Line at {0} is invalid, because of <{2}>. Line is ignored: {1}",
                            parser.getLineNumber(), csvRecord, e.getMessage()));
                }
            } else {
                XltLogger.runTimeLogger.error(MessageFormat.format(
                        "Line at {0} has not been correctly formatted. Line is ignored: {1}",
                        parser.getLineNumber(), csvRecord));
                incorrectLines = true;
            }
        }
    } finally {
        IOUtils.closeQuietly(br);
    }

    // stop if we have anything the is incorrect, avoid half running test cases
    if (incorrectLines) {
        throw new RuntimeException("Found incorrectly formatted lines. Stopping here.");
    }
}

From source file:com.marklogic.contentpump.CompressedDelimitedTextReader.java

@Override
public boolean nextKeyValue() throws IOException, InterruptedException {
    if (zipIn == null) {
        hasNext = false;/*w ww .  j av  a 2  s .  c o  m*/
        return false;
    }
    if (instream == null) {
        if (codec.equals(CompressionCodec.ZIP)) {
            return nextKeyValueInZip();
        } else if (codec.equals(CompressionCodec.GZIP)) {
            instream = new InputStreamReader(zipIn, encoding);
            parser = new CSVParser(instream, CSVParserFormatter.getFormat(delimiter, encapsulator, true, true));
            parserIterator = parser.iterator();
            return super.nextKeyValue();
        } else {
            throw new UnsupportedOperationException("Unsupported codec: " + codec.name());
        }
    } else {
        if (codec.equals(CompressionCodec.ZIP)) {
            if (super.nextKeyValue()) {
                // current delim txt has next
                return true;
            }
            return nextKeyValueInZip();
        } else if (codec.equals(CompressionCodec.GZIP)) {
            if (super.nextKeyValue()) {
                return true;
            }
            // move to next gzip file in this split
            if (iterator != null && iterator.hasNext()) {
                // close previous zip and streams
                close();
                initStream(iterator.next());
                if (encoding == null) {
                    instream = new InputStreamReader(zipIn);
                } else {
                    instream = new InputStreamReader(zipIn, encoding);
                }
                parser = new CSVParser(instream,
                        CSVParserFormatter.getFormat(delimiter, encapsulator, true, true));
                parserIterator = parser.iterator();
                return super.nextKeyValue();
            } else
                return false;
        } else {
            throw new UnsupportedOperationException("Unsupported codec: " + codec.name());
        }
    }
}

From source file:com.cotrino.langnet.GenerateVisualization.java

private double[] calculateSimilarity(File logFile) throws IOException {

    int i = 0;//from   ww  w  . ja va2s  .c om
    double total = 0.0;
    Reader reader = new FileReader(logFile);
    CSVParser parser = new CSVParser(reader, csvFormat);
    for (CSVRecord record : parser) {
        try {
            double similarity = Double.parseDouble(record.get("Similarity"));
            total += similarity;
            i++;
        } catch (NumberFormatException e) {
            logger.error("At " + logFile.getName() + ", failed line: " + record.toString());
        }
    }
    parser.close();
    reader.close();
    return new double[] { (total / i), i };

}