Example usage for org.apache.commons.csv CSVRecord get

List of usage examples for org.apache.commons.csv CSVRecord get

Introduction

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

Prototype

public String get(final String name) 

Source Link

Document

Returns a value by name.

Usage

From source file:com.team3637.service.TagServiceMySQLImpl.java

@Override
public void importCSV(String inputFile) {
    try {//  ww w  . j a  va2 s. c o m
        String csvData = new String(Files.readAllBytes(FileSystems.getDefault().getPath(inputFile)));
        csvData = csvData.replaceAll("\\r", "");
        CSVParser parser = CSVParser.parse(csvData, CSVFormat.DEFAULT.withRecordSeparator("\n"));
        for (CSVRecord record : parser) {
            Tag tag = new Tag();
            tag.setId(Integer.parseInt(record.get(0)));
            tag.setTag(record.get(1));
            tag.setType(record.get(2));
            tag.setCounter(record.get(3));
            tag.setInTable(record.get(4).equals("1") || record.get(4).toLowerCase().equals("true"));
            tag.setRequiesEval(record.get(5).equals("1") || record.get(5).toLowerCase().equals("true"));
            tag.setExpression(record.get(6).replace("\n", ""));
            if (checkForTag(tag))
                update(tag);
            else
                create(tag);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:edu.clemson.lph.utils.CSVParserWrapper.java

public CSVParserWrapper(CSVParser pIn) throws IOException {
    if (pIn == null)
        return;/*from w  w  w .  j a v a 2 s  .  com*/
    try {
        for (CSVRecord r : pIn.getRecords()) {
            List<String> aRow = new ArrayList<String>();
            for (int i = 0; i < r.size(); i++) {
                String sField = r.get(i);
                aRow.add(sField);
            }
            aRows.add(aRow);
        }
        iRows = aRows.size();
        iCurrent = 1;
    } finally {
        pIn.close();
    }
}

From source file:io.ecarf.core.cloud.task.processor.reason.phase0.DoReasonTask3.java

/**
 * //from w  w  w . jav  a2 s  .c o m
 * @param term
 * @param select
 * @param schemaTriples
 * @param rows
 * @param table
 * @param writer
 * @return
 * @throws IOException
 */
private int inferAndSaveTriplesToFile(Term term, List<String> select, Set<Triple> schemaTriples,
        BigInteger rows, String table, PrintWriter writer) throws IOException {

    int inferredTriples = 0;
    int failedTriples = 0;

    // loop through the instance triples probably stored in a file and generate all the triples matching the schema triples set
    try (BufferedReader r = new BufferedReader(new FileReader(term.getFilename()), Constants.GZIP_BUF_SIZE)) {

        Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(r);

        // records will contain lots of duplicates
        Set<String> inferredAlready = new HashSet<String>();

        try {

            for (CSVRecord record : records) {

                String values = ((select.size() == 1) ? record.get(0) : StringUtils.join(record.values(), ','));

                if (!inferredAlready.contains(values)) {
                    inferredAlready.add(values);

                    NTriple instanceTriple = new NTriple();

                    if (select.size() == 1) {
                        instanceTriple.set(select.get(0), record.get(0));
                    } else {

                        instanceTriple.set(select, record.values());
                    }

                    for (Triple schemaTriple : schemaTriples) {
                        Rule rule = GenericRule.getRule(schemaTriple);
                        Triple inferredTriple = rule.head(schemaTriple, instanceTriple);
                        writer.println(inferredTriple.toCsv());
                        inferredTriples++;
                    }

                    // this is just to avoid any memory issues
                    if (inferredAlready.size() > MAX_CACHE) {
                        inferredAlready.clear();
                        log.info("Cleared cache of inferred terms");
                    }
                }

            }
        } catch (Exception e) {
            log.error("Failed to parse selected terms", e);
            failedTriples++;
        }
    }

    //inferredFiles.add(inferredTriplesFile);
    log.info("\nSelect Triples: " + rows + ", Inferred: " + inferredTriples + ", Triples for term: " + term
            + ", Failed Triples: " + failedTriples);

    return inferredTriples;
}

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 a2s. co  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.ibm.watson.developer_cloud.professor_languo.pipeline.QuestionSetManager.java

/**
 * @param record - A single {@link CSVRecord} from the duplicate thread TSV file
 * @return A string array representing the data in each column of the record
 *///  ww w.  jav  a  2  s . c o m
private String[] convertRecordToArray(CSVRecord record) {
    String[] recordArray = new String[CorpusBuilder.getTsvColumnHeaders().length];
    for (int i = 0; i < CorpusBuilder.getTsvColumnHeaders().length; i++)
        recordArray[i] = record.get(i);
    return recordArray;
}

From source file:io.ecarf.core.cloud.task.processor.reason.phase0.DoReasonTask4.java

/**
 * //from w  w w . j av a  2  s  .c o m
 * @param term
 * @param select
 * @param schemaTriples
 * @param rows
 * @param table
 * @param writer
 * @return
 * @throws IOException
 */
private int inferAndSaveTriplesToFile(Term term, List<String> select, Set<Triple> schemaTriples,
        BigInteger rows, String table, PrintWriter writer) throws IOException {

    int inferredTriples = 0;
    int failedTriples = 0;

    // loop through the instance triples probably stored in a file and generate all the triples matching the schema triples set
    try (BufferedReader r = new BufferedReader(new FileReader(term.getFilename()), Constants.GZIP_BUF_SIZE)) {

        Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(r);

        // records will contain lots of duplicates
        Set<String> inferredAlready = new HashSet<String>();

        try {

            for (CSVRecord record : records) {

                String values = ((select.size() == 1) ? record.get(0) : StringUtils.join(record.values(), ','));

                if (!inferredAlready.contains(values)) {
                    inferredAlready.add(values);

                    NTriple instanceTriple = new NTriple();

                    if (select.size() == 1) {
                        instanceTriple.set(select.get(0), record.get(0));
                    } else {

                        instanceTriple.set(select, record.values());
                    }

                    for (Triple schemaTriple : schemaTriples) {
                        Rule rule = GenericRule.getRule(schemaTriple);
                        Triple inferredTriple = rule.head(schemaTriple, instanceTriple);
                        writer.println(inferredTriple.toCsv());
                        inferredTriples++;
                    }

                    // this is just to avoid any memory issues
                    if (inferredAlready.size() > MAX_CACHE) {
                        inferredAlready.clear();
                        log.info("Cleared cache of inferred terms");
                    }
                } else {
                    this.duplicates++;
                }

            }
        } catch (Exception e) {
            log.error("Failed to parse selected terms", e);
            failedTriples++;
        }
    }

    //inferredFiles.add(inferredTriplesFile);
    log.info("\nSelect Triples: " + rows + ", Inferred: " + inferredTriples + ", Triples for term: " + term
            + ", Failed Triples: " + failedTriples);

    return inferredTriples;
}

From source file:com.github.jferard.pgloaderutils.loader.CSVRecordCleanerExample.java

@Override
public Iterable<String> cleanRecord(final CSVRecord record) {
    return new Iterable<String>() {
        @Override/*ww w  .  j ava2 s .c  o m*/
        public Iterator<String> iterator() {
            return new Iterator<String>() {
                private int i = 0;

                @Override
                public boolean hasNext() {
                    return this.i < record.size();
                }

                @Override
                public String next() {
                    String s = record.get(this.i);
                    if (this.i == 11 || this.i == 12 || this.i == 16) // numbers
                        s = s.replaceAll(",", "."); // from continental to US

                    this.i++;
                    return s;
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    };
}

From source file:javalibs.CSVDataNormalizer.java

public void normalize() {
    BufferedWriter bw = null;//  w w w.  java  2  s . c  o m
    CSVPrinter printer = null;

    try {
        bw = Files.newBufferedWriter(Paths.get(this.savePath));
        printer = new CSVPrinter(bw, CSVFormat.DEFAULT.withHeader(this.headersInOrder));
    } catch (IOException e) {
        log_.die(e);
    }

    for (CSVRecord rec : this.allRecords) {
        List<String> writerCells = new ArrayList<>();
        for (int i = 0; i < this.numCols; ++i) {
            String colName = this.colNumToName.get(i);
            if (columnsToNormalize.contains(colName)) {
                double curVal = NumUtils.getDoubleFromStr(rec.get(colName));
                Pair<Double, Double> maxMin = this.colsToMaxMinPairs.get(colName);
                double normal = NumUtils.normalizeBetweenZeroOne(maxMin.right(), maxMin.left(), curVal);
                if (normal > 1.0) {
                    log_.warn("Normalized value greater than 1.0: " + normal + " from curVal: " + curVal
                            + " setting normal to 1.");
                    normal = 1.0;
                } else if (normal < 0.0) {
                    log_.warn("Normalized value less than 0.0: " + normal + " from curVal : " + curVal
                            + " setting normal to 0.");
                    normal = 0.0;
                }

                writerCells.add(Double.toString(normal));
            } else
                writerCells.add(rec.get(i));
        }
        try {
            printer.printRecord(writerCells.toArray());
        } catch (IOException e) {
            log_.die(e);
        }
    }
    try {
        printer.flush();
    } catch (IOException e) {
        log_.die(e);
    }
}

From source file:com.chargebee.Application.DateFormat.java

public void formatter(CSVParser parser, CSVPrinter printer, JSONObject jobj) throws Exception {

    String date_format = jobj.getString("date_format");
    String column_header = jobj.getString("column_header");
    boolean isHeader = true;

    for (CSVRecord record : parser) {
        String[] s = MethodBank.toArray(record);
        if (isHeader) {
            columnIndex = MethodBank.indexFinder(s, column_header);
            MethodBank.print(printer, s);
            isHeader = false;/*from w  w  w  .  j a v  a 2s . c  om*/
            continue;
        }

        String dateToPrint = "";
        SimpleDateFormat simpleDateFormatInput = new SimpleDateFormat(date_format);
        SimpleDateFormat simpleDateFormatOutput = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

        if (record.get(columnIndex) != null && !record.get(columnIndex).equals("")) {
            Date date = simpleDateFormatInput.parse(record.get(columnIndex));
            dateToPrint = simpleDateFormatOutput.format(date);
            s[columnIndex] = dateToPrint;
        }

        MethodBank.print(printer, s);

    }
}

From source file:br.edimarmanica.trinity.intrasitemapping.auto.MergeOffsets.java

private void executeOffset(int indexOffset) {
    File dir = new File(Paths.PATH_TRINITY + site.getPath() + "/offset");
    try (Reader in = new FileReader(dir.getAbsoluteFile() + "/result_" + indexOffset + ".csv")) {
        List<List<String>> lines = new ArrayList<>();
        try (CSVParser parser = new CSVParser(in, CSVFormat.EXCEL)) {

            int indexRegistro = 0;
            for (CSVRecord record : parser) {
                if (indexOffset != 0 && indexRegistro < Extract.NR_SHARED_PAGES) { //seno vai extrair repetido
                    indexRegistro++;//from   w w  w  .ja  v a2  s  . co  m
                    continue;
                }
                List<String> line = new ArrayList<>();
                for (int nrRegra = 0; nrRegra < record.size(); nrRegra++) {
                    try {
                        line.add(Preprocessing.filter(record.get(nrRegra)));
                    } catch (InvalidValue ex) {
                        line.add("");
                    }
                }
                lines.add(line);
                indexRegistro++;
            }

            print(indexOffset, lines);
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(MergeOffsets.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(MergeOffsets.class.getName()).log(Level.SEVERE, null, ex);
    }

}