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:edu.isi.misd.scanner.network.modules.worker.processors.ptr.PrepToResearchProcessor.java

private PrepToResearchResponse analyzeFile(PrepToResearchRequest request, File analysisFile) throws Exception {
    PrepToResearchResponse response = new PrepToResearchResponse();
    Integer requestedOmopConceptID = request.getOmopConceptID();
    CSVFormat csvFormat = CSVFormat.newFormat(',').withHeader().withCommentMarker('#').withQuote('"');
    CSVParser parser = CSVParser.parse(analysisFile, Charset.defaultCharset(), csvFormat);
    for (CSVRecord csvRecord : parser) {
        try {//from   w ww.ja va2s . c om
            this.validateCSVRecord(csvRecord);

            // check the ID first, if no match continue
            Integer omopConceptID = Integer
                    .parseInt(csvRecord.get(ExpectedColumnName.OMOP_CONCEPT_ID.toString()));
            if (!requestedOmopConceptID.equals(omopConceptID)) {
                continue;
            }

            // match found, create response output record
            if (log.isDebugEnabled()) {
                log.debug(String.format("Found a match for requested ID %s, record: %s", requestedOmopConceptID,
                        csvRecord.toString()));
            }
            PrepToResearchRecord ptrRecord = new PrepToResearchRecord();
            ptrRecord.setOmopConceptID(omopConceptID);
            ptrRecord.setOmopConceptName(csvRecord.get(ExpectedColumnName.OMOP_CONCEPT_NAME));
            ptrRecord.setCategory(csvRecord.get(ExpectedColumnName.CATEGORY));
            ptrRecord.setCategoryValue(csvRecord.get(ExpectedColumnName.CATEGORY_VALUE));
            ptrRecord.setCountFemales(Integer.parseInt(csvRecord.get(ExpectedColumnName.COUNT_FEMALES)));
            ptrRecord.setCountMales(Integer.parseInt(csvRecord.get(ExpectedColumnName.COUNT_MALES)));
            ptrRecord.setCountTotal(Integer.parseInt(csvRecord.get(ExpectedColumnName.COUNT_TOTAL)));

            response.getPrepToResearchRecord().add(ptrRecord);
        } catch (Exception e) {
            String error = String.format(
                    "An exception occured while processing row number %s with the following values %s: %s",
                    csvRecord.getRecordNumber(), csvRecord.toString(), e.toString());
            parser.close();
            throw new RuntimeException(error);
        }
    }
    parser.close();
    return response;
}

From source file:com.paolodragone.util.io.datasets.tsv.TsvDataRecord.java

TsvDataRecord(CSVRecord record, DataColumn[] header) {
    if (header.length > Short.MAX_VALUE) {
        throw new IllegalStateException("Current header has too many columns.");
    }//from w w  w .j  a va2s.  c o  m

    ImmutableList.Builder<String> builder = ImmutableList.builder();
    for (short i = 0; i < header.length; i++) {
        DataColumn key = header[i];
        mapping.put(key.getColumnName(), i);
        builder.add(record.get(key.getColumnName()));
    }
    values = builder.build();
}

From source file:com.adobe.aem.demomachine.communities.SetupCommunities.java

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
        throws ServerException, IOException {

    PrintWriter out = response.getWriter();

    // Checking if we have a valid admin user
    ResourceResolver resourceResolver = request.getResourceResolver();
    String userId = resourceResolver.getUserID();
    if (userId == null || !userId.equals("admin")) {
        out.println("Permission denied: admin user requested to access this feature");
        return;/*from   w  ww  .j  ava 2  s.c o m*/
    }

    // Checking if we have valid configuration parameters
    String csvPath = (String) request.getParameter("contentPath");
    if (csvPath == null) {
        csvPath = "";
    }

    // Checking the version of GraniteUI to be loaded
    String coralVersion = "3";
    Resource resCoral = resourceResolver.getResource("/etc/clientlibs/granite/coralui3.js");
    if (resCoral == null)
        coralVersion = "2";

    response.setContentType("text/html");
    out.println("<html><head>");
    out.println("<link rel=\"stylesheet\" href=\"/etc/clientlibs/granite/coralui" + coralVersion
            + ".css\" type=\"text/css\">");
    out.println("<script type=\"text/javascript\" src=\"/etc/clientlibs/granite/typekit.js\"></script>");
    out.println("<script type=\"text/javascript\" src=\"/etc/clientlibs/granite/jquery.js\"></script>");
    out.println("<script type=\"text/javascript\" src=\"/etc/clientlibs/granite/utils.js\"></script>");
    out.println("<script type=\"text/javascript\" src=\"/etc/clientlibs/granite/moment.js\"></script>");
    out.println("<script type=\"text/javascript\" src=\"/etc/clientlibs/granite/coralui" + coralVersion
            + ".js\"></script>");
    out.println("</head><body class=\"coral--light u-coral-clearFix\" style=\"margin:40px\">");

    // Checking if the page is loaded in a frame (e.g. authoring environment)
    out.println(
            "<script language=\"JavaScript\">if(window.frameElement){window.top.location=window.location.href;}</script>");

    out.println("<a name=\"top\"/>");
    out.println("<div><h1>AEM Communities - Demo Setup</h1>");
    out.println(
            "<form action=\"/bin/CreateCommunities\" method=\"GET\" class=\"coral-Form coral-Form--vertical\" style=\"width:700px\">");
    out.println("<section class=\"coral-Form-fieldset\">");
    out.println(
            "<span>All the fun takes place on the Publish instance with AEM - please ensure yours is available at the following coordinates</spanl>");
    out.println("<label class=\"coral-Form-fieldlabel\">Path to configuration files</label>");
    out.println("<input is=\"coral-textfield\" name=\"contentPath\" type=\"text\" value=\"" + csvPath
            + "\" class=\"coral-Form-field coral-Textfield\">");
    out.println("<label class=\"coral-Form-fieldlabel\">Author instance</label>");
    out.println("<div class=\"coral-Form--aligned\">");
    // Checking if the default host and port are reachable for the author server
    String hostname_author = "localhost";
    String port_author = "4502";
    if (!Hostname.isReachable(hostname_author, port_author)) {
        hostname_author = "";
        port_author = "";
    }
    out.println("<input is=\"coral-textfield\" name=\"hostname_author\" type=\"text\" value=\""
            + hostname_author + "\" class=\"coral-Textfield\">");
    out.println("<input is=\"coral-textfield\" name=\"port_author\" type=\"text\" value=\"" + port_author
            + "\" class=\"coral-Textfield\">");
    out.println("</div>");
    out.println("<label class=\"coral-Form-fieldlabel\">Publish instance</label>");
    // Checking if the default host and port are reachable for the publish server
    String hostname_publish = "localhost";
    String port_publish = "4503";
    if (!Hostname.isReachable(hostname_publish, port_publish)) {
        hostname_publish = "";
        port_publish = "";
        out.println("<coral-alert>");
        out.println("<coral-alert-header>WARNING</coral-alert-header>");
        out.println(
                "<coral-alert-content>Using an AEM Publish instance is strongly recommended. If not using a Publish instance, all UGC will be posted against the Author instance, which might fail if the demo members are not granted appropriate permissions on Author.</coral-alert-content>");
        out.println("</coral-alert>");
    }
    out.println("<div class=\"coral-Form--aligned\">");

    out.println("<input is=\"coral-textfield\" name=\"hostname\" type=\"text\" value=\"" + hostname_publish
            + "\" class=\"coral-Textfield\">");
    out.println("<input is=\"coral-textfield\" name=\"port\" type=\"text\" value=\"" + port_publish
            + "\" class=\"coral-Textfield\">");

    out.println("</div>");
    out.println("<label class=\"coral-Form-fieldlabel\">Admin password</label>");
    out.println(
            "<input is=\"coral-textfield\" name=\"password\" type=\"text\" value=\"admin\" class=\"coral-Form-field coral-Textfield\">");
    out.println("<label class=\"coral-Form-fieldlabel\">Please select from the following options</label>");

    // Getting the list of .csv configuration files for this content path
    int intOptions = 0;
    Resource resConfigFiles = resourceResolver.getResource(csvPath);
    if (!csvPath.equals("") && resConfigFiles != null) {
        ArrayList<String[]> configOptions = new ArrayList<String[]>();
        for (Resource resConfigFile : resConfigFiles.getChildren()) {
            if (resConfigFile != null && resConfigFile.getName().endsWith(".csv")) {
                String[] resConfigSettings = resConfigFile.getName().split("-");
                configOptions.add(resConfigSettings);
            }

        }
        Collections.sort(configOptions, new Comparator<String[]>() {
            public int compare(String[] strings, String[] otherStrings) {
                return strings[0].compareTo(otherStrings[0]);
            }
        });
        for (String[] configOption : configOptions) {

            // Loading title and description
            String title = configOption[2];
            String description = configOption[0] + "-" + configOption[1] + "-" + configOption[2];
            Resource resConfigFile = resourceResolver.getResource(csvPath + "/" + description + "/jcr:content");
            if (resConfigFile != null) {

                InputStream stream = resConfigFile.adaptTo(InputStream.class);
                Reader inConfigFile = new InputStreamReader(stream);
                Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(inConfigFile);
                for (CSVRecord record : records) {
                    String rDescription = "# Description: ";
                    if (record.get(0).startsWith(rDescription))
                        description = record.get(0).replace(rDescription, "").trim() + " (" + description + ")";
                    String rTitle = "# Title: ";
                    if (record.get(0).startsWith(rTitle))
                        title = record.get(0).replace(rTitle, "").trim();
                }
                intOptions++;

                try {
                    inConfigFile.close();
                    stream.close();
                } catch (IOException ioex) {
                    //omitted.
                }

            }

            printCheckbox(out, "setup-" + configOption[0], title, description);

        }
    }

    if (intOptions > 0) {

        out.println("<div class=\"coral-Form-fieldwrapper coral-Form-fieldwrapper--alignRight\">");
        out.println("<button class=\"coral-Form-field coral-Button coral-Button--primary\">Submit</button>");
        out.println("</div>");

    } else {

        out.println("<p>No configuration file to process</p>");

    }

    String returnURL = (String) request.getParameter("returnURL");
    if (returnURL != null) {
        out.println("<input type=\"hidden\" name=\"returnURL\" value=\"" + returnURL + "\">");
    }

    out.println("</section></form>");
    out.println("</body></html>");

}

From source file:com.ibm.watson.developer_cloud.professor_languo.pipeline.QuestionSetManagerTest.java

private void subset_tsv_files_are_properly_created() throws PipelineException {
    // Confirm that TSV files of the same format as the duplicate thread TSV
    // file are created
    // for each of the subsets that are generated by the QuestionSetManager
    for (File file : Arrays.asList(trainFile, testFile, validateFile)) {
        assertTrue("File " + file.getName() + " is missing", file.exists());

        QuestionAnswerSet set;/*  w  ww.j a  v  a  2s. c  o m*/
        if (file == trainFile)
            set = this.questionSetManager.getTrainingSet();
        else if (file == testFile)
            set = this.questionSetManager.getTestSet();
        else
            set = this.questionSetManager.getValidationSet();

        // Iterate through each line of the subset TSV file and verify that
        // the records
        // it contains are in fact in the corresponding QuestionAnswerSet
        try (CSVParser parser = CSVFormat.TDF.withHeader().parse(new FileReader(file))) {
            for (CSVRecord record : parser.getRecords()) {
                assertTrue("Subset TSV file has erroneous QID",
                        set.getQuestionIds().contains(record.get(CorpusBuilder.TSV_COL_HEADER_THREAD_ID)));
            }
        } catch (IOException e) {
            throw new PipelineException(e);
        }
    }
}

From source file:biz.ganttproject.impex.csv.CsvImportTest.java

public void testSkipLinesWithEmptyMandatoryFields() throws IOException {
    String header = "A, B, C";
    String data1 = "a1,,c1";
    String data2 = "a2,b2,c2";
    String data3 = ",b3,c3";
    final AtomicBoolean wasCalled = new AtomicBoolean(false);
    RecordGroup recordGroup = new RecordGroup("ABC", ImmutableSet.<String>of("A", "B", "C"),
            ImmutableSet.<String>of("A", "B")) {
        @Override/*from  w w  w .  j a v  a2  s  .  c  o  m*/
        protected boolean doProcess(CSVRecord record) {
            if (!super.doProcess(record)) {
                return false;
            }
            if (!hasMandatoryFields(record)) {
                return false;
            }
            wasCalled.set(true);
            assertEquals("a2", record.get("A"));
            assertEquals("b2", record.get("B"));
            return true;
        }
    };
    GanttCSVOpen importer = new GanttCSVOpen(createSupplier(Joiner.on('\n').join(header, data1, data2, data3)),
            recordGroup);
    importer.load();
    assertTrue(wasCalled.get());
    assertEquals(2, importer.getSkippedLineCount());
}

From source file:co.cask.hydrator.transforms.CSVParser2.java

private StructuredRecord createStructuredRecord(CSVRecord record) {
    StructuredRecord.Builder builder = StructuredRecord.builder(outSchema);
    int i = 0;/* w  ww  .  j a  v a  2s.  c om*/
    for (Field field : fields) {
        builder.set(field.getName(), TypeConvertors.get(record.get(i), field.getSchema().getType()));
        ++i;
    }
    return builder.build();
}

From source file:com.ge.research.semtk.load.dataset.CSVDataset.java

@Override
/**//from  www. j  a  v a 2 s .  c  om
 * Read the next set of rows from the CSV file
 */
public ArrayList<ArrayList<String>> getNextRecords(int numRecords) throws Exception {
    if (headers == null) {
        throw new Exception("Dataset headers are not available");
    }
    ArrayList<ArrayList<String>> rows = new ArrayList<ArrayList<String>>();
    CSVRecord record;

    for (int i = 0; i < numRecords; i++) { // read the specified number of records
        try {
            ArrayList<String> currRow = new ArrayList<String>();
            record = this.recordIterator.next();
            if (record.size() == 1 && record.get(0).trim().isEmpty()) {
                System.out.println("Empty CSV row, continuing...");
                continue; // this is an empty line, skip it
            }

            for (int j = 0; j < headers.length; j++) {
                // add the next entry to the list. 
                try {
                    currRow.add(record.get(headers[j]));
                } catch (Exception eee) {
                    System.out.println("exception getting data for header");
                }
            }
            rows.add(currRow);
        } catch (NoSuchElementException e) {
            //            System.out.println("ran into an exception for a missing element when getting records.... out of rows.");
            break; // got to the end of the file
        }
    }

    // what is the count of Rows we want to return?
    //      System.out.println("number of CSV rows returned this run: " + rows.size());

    return rows;
}

From source file:com.wx3.galacdecks.Bootstrap.java

private void importCards(GameDatastore datastore, String path) throws IOException {
    Reader reader = new FileReader(path);
    CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader());
    int count = 0;
    for (CSVRecord record : parser) {
        String id = record.get("id");
        String name = record.get("name");
        String description = record.get("description");
        String flavor = record.get("flavor");
        String unitPrefab = record.get("unitPrefab");
        String summonEffect = record.get("summonEffect");
        String projectile = record.get("projectile");
        String portrait = record.get("portrait");
        Set<EntityTag> tags = new HashSet<EntityTag>();
        for (EntityTag tag : EntityTag.values()) {
            if (record.isSet(tag.toString())) {
                if (record.get(tag).equals("Y")) {
                    tags.add(tag);/*from w  w  w .  ja v a  2s.  co m*/
                }
            }
        }

        String[] types = record.get("types").split(",");
        for (String type : types) {
            if (type.length() > 0) {
                EntityTag tag = EntityTag.valueOf(type);
                tags.add(tag);
            }
        }

        Map<EntityStat, Integer> stats = new HashMap<EntityStat, Integer>();
        for (EntityStat stat : EntityStat.values()) {
            String statName = stat.toString();
            if (record.isSet(statName)) {
                stats.put(stat, parseIntOrZero(record.get(statName)));
            }
        }

        List<EntityRule> rules = new ArrayList<EntityRule>();
        String ruleField = record.get("rules");
        String[] ruleIds = ruleField.split(",");
        for (String ruleId : ruleIds) {
            if (ruleId.length() > 0) {
                if (!ruleCache.containsKey(ruleId)) {
                    throw new RuntimeException("Unable to find rule '" + ruleId + "'");
                }
                EntityRule rule = ruleCache.get(ruleId);
                rules.add(rule);
            }
        }

        ValidatorScript playValidator = null;
        String playValidatorId = record.get("playValidator");
        if (playValidatorId != null && !playValidatorId.isEmpty()) {
            if (!playValidatorCache.containsKey(playValidatorId)) {
                throw new RuntimeException("Unknown validator '" + playValidatorId + "'");
            }
            playValidator = playValidatorCache.get(playValidatorId);
        }

        ValidatorScript discardValidator = null;
        String discardValidatorId = record.get("discardValidator");
        if (discardValidatorId != null && !discardValidatorId.isEmpty()) {
            if (!discardValidatorCache.containsKey(discardValidatorId)) {
                throw new RuntimeException("Unknown validator '" + discardValidatorId + "'");
            }
            discardValidator = discardValidatorCache.get(discardValidatorId);
        }

        Set<AiHint> aiHints = new HashSet<>();
        String hintField = record.get("aiHints");
        String[] hintIds = hintField.split(",");
        for (String hintId : hintIds) {
            if (hintId.length() > 0) {
                if (!aiHintCache.containsKey(hintId)) {
                    throw new RuntimeException("Unable to find AI Hint '" + hintId + "'");
                }
                AiHint hint = aiHintCache.get(hintId);
                aiHints.add(hint);
            }
        }

        EntityPrototype card = EntityPrototype.createPrototype(id, name, description, flavor, unitPrefab,
                summonEffect, projectile, portrait, tags, rules, playValidator, discardValidator, stats,
                aiHints);
        datastore.createPrototype(card);
        if (cardCache.containsKey(card.getId())) {
            throw new RuntimeException("Duplicate card id: " + card.getId());
        }
        cardCache.put(card.getId(), card);
        ++count;
    }
    logger.info("Imported " + count + " cards");
    parser.close();
}

From source file:javalibs.CSVExtractor.java

/**
 * Writes the CSV to the path specified in the c'tor. Returns the absolute path to
 * the output CSV file/*from w  ww .  ja v  a2  s . co  m*/
 * @return The absolute path to the output CSV file
 */
public String writeCSV() {
    BufferedWriter bw = null;
    CSVPrinter printer = null;

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

    log_.require(bw != null, "BufferedWriter cannot be null");
    log_.require(printer != null, "CSVPrinter cannot be null");

    for (CSVRecord rec : this.inRecords) {
        List<String> writerCells = new ArrayList<>();
        for (String col : this.headersInOrder) {
            if (!this.extractionCols.contains(col))
                continue;

            String colVal = null;
            try {
                colVal = rec.get(col);
            } catch (IllegalArgumentException e) {
                log_.err("Could not find column: " + col);
                log_.die(e);
            }
            writerCells.add(colVal);
        }
        try {
            printer.printRecord(writerCells.toArray());
        } catch (IOException e) {
            log_.die(e);
        }
    }
    try {
        printer.flush();
    } catch (IOException e) {
        log_.die(e);
    }

    return new File(this.outCSV).getAbsolutePath();
}

From source file:com.itemanalysis.jmetrik.data.JmetrikFileImporterTest.java

@Test
public void readJmetrikFileTest() {
    System.out.println("JmetrikFileImporterTest: Reading *.jmetrik file");
    CSVParser parser = null;//from  ww w  .  j  av a 2s  .co m
    Reader reader = null;

    try {
        File dataFile = FileUtils.toFile(this.getClass().getResource("/data/example-import-file.jmetrik"));
        reader = new InputStreamReader(new BOMInputStream(new FileInputStream(dataFile)), "UTF-8");

        parser = new CSVParser(reader, CSVFormat.DEFAULT.withCommentMarker('#'));
        Iterator<CSVRecord> iter = parser.iterator();
        CSVRecord temp = null;

        boolean readAttributes = false;
        boolean readData = false;
        int attCount = 0;

        while (iter.hasNext()) {
            temp = iter.next();

            if ("VERSION".equals(temp.getComment())) {
                System.out.println("VERSION: " + temp.get(0));

            } else if ("METADATA".equals(temp.getComment())) {
                System.out.println("CASES: " + temp.get(0));
            } else if ("ATTRIBUTES".equals(temp.getComment())) {
                readAttributes = true;
            } else if ("DATA".equals(temp.getComment())) {
                readAttributes = false;
                readData = true;
            }

            if (readAttributes) {
                System.out.print("ATTRIBUTE-" + attCount + ": ");
                Iterator<String> innerIter = temp.iterator();
                while (innerIter.hasNext()) {
                    System.out.print(innerIter.next());
                    if (innerIter.hasNext()) {
                        System.out.print(",");
                    }
                }
                System.out.println();
                attCount++;
            }

            if (readData) {
                Iterator<String> innerIter = temp.iterator();
                while (innerIter.hasNext()) {
                    System.out.print(innerIter.next());
                    if (innerIter.hasNext()) {
                        System.out.print(",");
                    }
                }
                System.out.println();
            }

        }

    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        try {
            parser.close();
            reader.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}