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

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

Introduction

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

Prototype

public static CSVParser parse(final URL url, final Charset charset, final CSVFormat format) throws IOException 

Source Link

Document

Creates a parser for the given URL.

Usage

From source file:de.tudarmstadt.ukp.experiments.dip.wp1.documents.Step1PrepareContainers.java

public static void main(String[] args) throws IOException {
    // queries with narratives in CSV
    File queries = new File(args[0]);
    File relevantInformationExamplesFile = new File(args[1]);

    Map<Integer, Map<Integer, List<String>>> relevantInformationMap = parseRelevantInformationFile(
            relevantInformationExamplesFile);

    // output dir
    File outputDir = new File(args[2]);
    if (!outputDir.exists()) {
        outputDir.mkdirs();/*from  www  .  jav  a  2  s.c o  m*/
    }

    // iterate over queries
    CSVParser csvParser = CSVParser.parse(queries, Charset.forName("utf-8"), CSVFormat.DEFAULT);
    for (CSVRecord record : csvParser) {
        // create new container, fill, and store
        QueryResultContainer container = new QueryResultContainer();
        container.qID = record.get(0);
        container.query = record.get(1);

        // Fill some dummy text first
        container.relevantInformationExamples.addAll(Collections.singletonList("ERROR. Information missing."));
        container.irrelevantInformationExamples
                .addAll(Collections.singletonList("ERROR. Information missing."));

        // and now fill it with existing information if available
        Integer queryID = Integer.valueOf(container.qID);
        if (relevantInformationMap.containsKey(queryID)) {
            if (relevantInformationMap.get(queryID).containsKey(0)) {
                container.irrelevantInformationExamples = new ArrayList<>(
                        relevantInformationMap.get(queryID).get(0));
            }

            if (relevantInformationMap.get(queryID).containsKey(1)) {
                container.relevantInformationExamples = new ArrayList<>(
                        relevantInformationMap.get(queryID).get(1));
            }
        }

        File outputFile = new File(outputDir, container.qID + ".xml");
        FileUtils.writeStringToFile(outputFile, container.toXML());
        System.out.println("Finished " + outputFile);
    }
}

From source file:ch.bfh.unicert.certimport.Main.java

public static void main(String[] args) throws IOException, InvalidNameException {

    logger.info("File read");

    File f = new File(csvPath);
    CSVParser parser = CSVParser.parse(f, Charset.forName("UTF-8"), CSVFormat.DEFAULT);

    for (CSVRecord record : parser) {
        createCertificate(record);//from  w w  w  .j  av a  2  s . c o  m

    }

}

From source file:io.mindmaps.migration.csv.Main.java

public static void main(String[] args) {

    String csvFileName = null;//w w w  .  jav  a2 s .  com
    String csvEntityType = null;
    String engineURL = null;
    String graphName = null;

    for (int i = 0; i < args.length; i++) {
        if ("-file".equals(args[i]))
            csvFileName = args[++i];
        else if ("-graph".equals(args[i]))
            graphName = args[++i];
        else if ("-engine".equals(args[i]))
            engineURL = args[++i];
        else if ("-as".equals(args[i])) {
            csvEntityType = args[++i];
        } else if ("csv".equals(args[0])) {
            continue;
        } else
            die("Unknown option " + args[i]);
    }

    if (csvFileName == null) {
        die("Please specify CSV file using the -csv option");
    }
    File csvFile = new File(csvFileName);
    if (!csvFile.exists()) {
        die("Cannot find file: " + csvFileName);
    }
    if (graphName == null) {
        die("Please provide the name of the graph using -graph");
    }
    if (csvEntityType == null) {
        csvEntityType = csvFile.getName().replaceAll("[^A-Za-z0-9]", "_");
    }

    System.out.println("Migrating " + csvFileName + " using MM Engine "
            + (engineURL == null ? "local" : engineURL) + " into graph " + graphName);

    // perform migration
    CSVSchemaMigrator schemaMigrator = new CSVSchemaMigrator();
    CSVDataMigrator dataMigrator = new CSVDataMigrator();

    //
    try {
        MindmapsGraph graph = engineURL == null ? MindmapsClient.getGraph(graphName)
                : MindmapsClient.getGraph(graphName, engineURL);

        Loader loader = engineURL == null ? new BlockingLoader(graphName)
                : new DistributedLoader(graphName, Lists.newArrayList(engineURL));

        CSVParser csvParser = CSVParser.parse(csvFile.toURI().toURL(), StandardCharsets.UTF_8,
                CSVFormat.DEFAULT.withHeader());

        schemaMigrator.graph(graph).configure(csvEntityType, csvParser).migrate(loader);

        System.out.println("Schema migration successful");

        dataMigrator.graph(graph).configure(csvEntityType, csvParser).migrate(loader);

        System.out.println("DataType migration successful");

    } catch (Throwable throwable) {
        throwable.printStackTrace(System.err);
    }

    System.exit(0);
}

From source file:edu.harvard.liblab.ecru.LoadCsvData.java

/**
 * @param args/* ww w .ja  va2s.  co m*/
 */
public static void main(String[] args) {
    if (args.length > 7 | args.length == 0 || !args[0].equals("-f") || !args[2].equals("-u")
            || !args[4].equals("-i")) {
        System.err.println(USAGE);
        System.exit(1);
    }
    String filename = args[1].trim();
    url = args[3].trim();
    needsPrefix = !args[5].equals("unique");
    isVerbose = (args.length == 7 && args[6].equals("-v"));
    System.out.println("Loading data from " + filename + " " + (needsPrefix ? "IDs will be prefixed " : " "));
    long start = System.currentTimeMillis();
    boolean isReading = false;
    CSVPrinter printer = null;

    CSVFormat format = CSVFormat.EXCEL.withHeader().withDelimiter(',').withAllowMissingColumnNames(true);
    CSVParser parser;
    try {
        if (isVerbose) {
            printer = new CSVPrinter(System.err, format.withDelimiter('|'));
        }
        parser = CSVParser.parse(new File(filename), Charset.forName("UTF-8"), format);

        solrSrvr = SingletonSolrServer.getSolrServer(url);
        for (CSVRecord record : parser) {
            numRecs++;
            HashMap<String, String> recMap = new HashMap<String, String>();
            for (String field : FIELDS) {
                String value = null;
                try {
                    value = record.get(field);
                } catch (IllegalArgumentException e) {
                    if (e.getMessage().indexOf("expected one of") == -1) {
                        e.printStackTrace();
                        System.exit(1);
                    }
                }
                value = value == null ? "" : value.trim();
                recMap.put(field, value);
            }
            String id = recMap.get("ID");
            if (id.isEmpty()) {
                if (isVerbose) {
                    System.err.println("Record missing ID: ");
                    printer.printRecord(record);
                }
            } else {
                String type = recMap.get("Type");
                SolrDocument sdoc = getDocFromSolr(recMap.get("ID"));
                try {
                    if (type.toLowerCase().equals("course")) {
                        processCourse(recMap, sdoc);
                        isReading = false;
                    } else {
                        if (!isReading) {
                            addUpdateCommit(); // just in case the preceeding course(s) are related
                        }
                        processReading(recMap, sdoc);
                        isReading = true;
                    }
                } catch (Exception e) {
                    if (isVerbose) {
                        System.err.println("Record # " + numRecs + " not used:\n\t" + e.getMessage());
                    }
                    errRecs++;
                }
            }
            if (beans.size() > 20) {
                addUpdateCommit();
            }
        }
        parser.close();
        if (beans.size() > 0 || docUpdates.size() > 0) {
            addUpdateCommit();
        }
    } catch (FileNotFoundException e) {
        System.err.println(filename + " not found");
        System.exit(1);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
    long end = System.currentTimeMillis();
    long courseTime = (end - start) / (long) 1000;
    try {
        solrSrvr.optimize();
    } catch (SolrServerException e) {
        e.printStackTrace();
        System.exit(1);
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }
    System.out.println(numRecs + " records found, of which " + errRecs + " had a problem; time: " + courseTime
            + " seconds " + ((courseTime > 60) ? ("(" + (courseTime / (long) 60) + " minutes)") : ""));
    System.exit(0);
}

From source file:moacscoper.Parser.java

private List<CSVRecord> parse(String path) throws IOException {
    return CSVParser.parse(new File(path), StandardCharsets.UTF_8, CSVFormat.TDF).getRecords();
}

From source file:gradingfun.GradeParser.java

public GradeParser(File file) {
    try {// w w w.j av  a 2  s. co m
        this.parser = CSVParser.parse(file, Charset.defaultCharset(), CSVFormat.RFC4180);
    } catch (IOException ex) {
        Logger.getLogger(GradeParser.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.univocity.articles.csvcomparison.parser.CommonsCsvParser.java

@Override
public void processRows(File input) throws Exception {
    CSVFormat format = CSVFormat.RFC4180;
    CSVParser parser = CSVParser.parse(input, getEncoding(), format);
    for (CSVRecord record : parser) {
        process(record);/*  ww  w.  j  av  a 2  s  . c om*/
    }
}

From source file:functions.LoadCSVdata.java

public void LoadFeeDataToJTable(JTable t, String path) {
    try {//from  w  ww  . j  a  v  a 2 s  .  c o m
        file = new File(path);
        parser = CSVParser.parse(file, Charset.forName("UTF-8"), CSVFormat.DEFAULT);
        DefaultTableModel model = (DefaultTableModel) t.getModel();
        model.setRowCount(0);
        for (CSVRecord c : parser) {
            if (c.getRecordNumber() == 1)
                continue;
            model.addRow(
                    new Object[] { c.get(datatype.GlobalVariable.TYPE), c.get(datatype.GlobalVariable.AMOUNT),
                            c.get(datatype.GlobalVariable.PAID_BY), c.get(datatype.GlobalVariable.PAYER) });
        }
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:com.univocity.articles.csvcomparison.parser.CommonsCsvParser.java

@Override
public List<String[]> parseRows(File input) throws Exception {
    CSVFormat format = CSVFormat.RFC4180;
    CSVParser parser = CSVParser.parse(input, getEncoding(), format);

    List<String[]> rows = new ArrayList<String[]>();

    for (CSVRecord record : parser) {
        String[] row = new String[record.size()];
        for (int i = 0; i < row.length; i++) {
            row[i] = record.get(i);//from  w w w.j a v a2 s. co m
        }
        rows.add(row);
    }
    return rows;
}

From source file:com.ibm.g11n.pipeline.example.CSVFilter.java

@Override
public LanguageBundle parse(InputStream inStream, FilterOptions options)
        throws IOException, ResourceFilterException {
    LanguageBundleBuilder bundleBuilder = new LanguageBundleBuilder(true);
    CSVParser parser = CSVParser.parse(inStream, StandardCharsets.UTF_8,
            CSVFormat.RFC4180.withHeader("key", "value").withSkipHeaderRecord(true));
    for (CSVRecord record : parser) {
        String key = record.get(0);
        String value = record.get(1);
        bundleBuilder.addResourceString(key, value);
    }//  w  w w  .j  a  v a  2 s. c  o  m
    return bundleBuilder.build();
}