Example usage for org.apache.commons.csv CSVPrinter printRecord

List of usage examples for org.apache.commons.csv CSVPrinter printRecord

Introduction

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

Prototype

public void printRecord(final Object... values) throws IOException 

Source Link

Document

Prints the given values a single record of delimiter separated values followed by the record separator.

Usage

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

/**
 * @param args//from w ww  .  ja  va2s .c  o  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:com.act.lcms.db.analysis.StandardIonAnalysis.java

public static void main(String[] args) throws Exception {
    Options opts = new Options();
    for (Option.Builder b : OPTION_BUILDERS) {
        opts.addOption(b.build());/*w  ww  .j  a  va2 s  . co  m*/
    }

    CommandLine cl = null;
    try {
        CommandLineParser parser = new DefaultParser();
        cl = parser.parse(opts, args);
    } catch (ParseException e) {
        System.err.format("Argument parsing failed: %s\n", e.getMessage());
        HELP_FORMATTER.printHelp(LoadPlateCompositionIntoDB.class.getCanonicalName(), HELP_MESSAGE, opts, null,
                true);
        System.exit(1);
    }

    if (cl.hasOption("help")) {
        HELP_FORMATTER.printHelp(LoadPlateCompositionIntoDB.class.getCanonicalName(), HELP_MESSAGE, opts, null,
                true);
        return;
    }

    File lcmsDir = new File(cl.getOptionValue(OPTION_DIRECTORY));
    if (!lcmsDir.isDirectory()) {
        System.err.format("File at %s is not a directory\n", lcmsDir.getAbsolutePath());
        HELP_FORMATTER.printHelp(LoadPlateCompositionIntoDB.class.getCanonicalName(), HELP_MESSAGE, opts, null,
                true);
        System.exit(1);
    }

    try (DB db = DB.openDBFromCLI(cl)) {
        ScanFile.insertOrUpdateScanFilesInDirectory(db, lcmsDir);
        StandardIonAnalysis analysis = new StandardIonAnalysis();
        HashMap<Integer, Plate> plateCache = new HashMap<>();

        String plateBarcode = cl.getOptionValue(OPTION_STANDARD_PLATE_BARCODE);
        String inputChemicals = cl.getOptionValue(OPTION_STANDARD_CHEMICAL);
        String medium = cl.getOptionValue(OPTION_MEDIUM);

        // If standard chemical is specified, do standard LCMS ion selection analysis
        if (inputChemicals != null && !inputChemicals.equals("")) {
            String[] chemicals;
            if (!inputChemicals.contains(",")) {
                chemicals = new String[1];
                chemicals[0] = inputChemicals;
            } else {
                chemicals = inputChemicals.split(",");
            }

            String outAnalysis = cl.getOptionValue(OPTION_OUTPUT_PREFIX) + "." + CSV_FORMAT;
            String plottingDirectory = cl.getOptionValue(OPTION_PLOTTING_DIR);
            String[] headerStrings = { "Molecule", "Plate Bar Code", "LCMS Detection Results" };
            CSVPrinter printer = new CSVPrinter(new FileWriter(outAnalysis),
                    CSVFormat.DEFAULT.withHeader(headerStrings));

            for (String inputChemical : chemicals) {
                List<StandardWell> standardWells;

                Plate queryPlate = Plate.getPlateByBarcode(db,
                        cl.getOptionValue(OPTION_STANDARD_PLATE_BARCODE));
                if (plateBarcode != null && medium != null) {
                    standardWells = analysis.getStandardWellsForChemicalInSpecificPlateAndMedium(db,
                            inputChemical, queryPlate.getId(), medium);
                } else if (plateBarcode != null) {
                    standardWells = analysis.getStandardWellsForChemicalInSpecificPlate(db, inputChemical,
                            queryPlate.getId());
                } else {
                    standardWells = analysis.getStandardWellsForChemical(db, inputChemical);
                }

                if (standardWells.size() == 0) {
                    throw new RuntimeException("Found no LCMS wells for " + inputChemical);
                }

                // Sort in descending order of media where MeOH and Water related media are promoted to the top and
                // anything derived from yeast media are demoted.
                Collections.sort(standardWells, new Comparator<StandardWell>() {
                    @Override
                    public int compare(StandardWell o1, StandardWell o2) {
                        if (StandardWell.doesMediaContainYeastExtract(o1.getMedia())
                                && !StandardWell.doesMediaContainYeastExtract(o2.getMedia())) {
                            return 1;
                        } else {
                            return 0;
                        }
                    }
                });

                Map<StandardWell, StandardIonResult> wellToIonRanking = StandardIonAnalysis
                        .getBestMetlinIonsForChemical(inputChemical, lcmsDir, db, standardWells,
                                plottingDirectory);

                if (wellToIonRanking.size() != standardWells.size()
                        && !cl.hasOption(OPTION_OVERRIDE_NO_SCAN_FILE_FOUND)) {
                    throw new Exception("Could not find a scan file associated with one of the standard wells");
                }

                for (StandardWell well : wellToIonRanking.keySet()) {
                    LinkedHashMap<String, XZ> snrResults = wellToIonRanking.get(well).getAnalysisResults();

                    String snrRankingResults = "";
                    int numResultsToShow = 0;

                    Plate plateForWellToAnalyze = Plate.getPlateById(db, well.getPlateId());

                    for (Map.Entry<String, XZ> ionToSnrAndTime : snrResults.entrySet()) {
                        if (numResultsToShow > 3) {
                            break;
                        }

                        String ion = ionToSnrAndTime.getKey();
                        XZ snrAndTime = ionToSnrAndTime.getValue();

                        snrRankingResults += String.format(ion + " (%.2f SNR at %.2fs); ",
                                snrAndTime.getIntensity(), snrAndTime.getTime());
                        numResultsToShow++;
                    }

                    String[] resultSet = { inputChemical,
                            plateForWellToAnalyze.getBarcode() + " " + well.getCoordinatesString() + " "
                                    + well.getMedia() + " " + well.getConcentration(),
                            snrRankingResults };

                    printer.printRecord(resultSet);
                }
            }

            try {
                printer.flush();
                printer.close();
            } catch (IOException e) {
                System.err.println("Error while flushing/closing csv writer.");
                e.printStackTrace();
            }
        } else {
            // Get the set of chemicals that includes the construct and all it's intermediates
            Pair<ConstructEntry, List<ChemicalAssociatedWithPathway>> constructAndPathwayChems = analysis
                    .getChemicalsForConstruct(db, cl.getOptionValue(OPTION_CONSTRUCT));
            System.out.format("Construct: %s\n", constructAndPathwayChems.getLeft().getCompositionId());

            for (ChemicalAssociatedWithPathway pathwayChem : constructAndPathwayChems.getRight()) {
                System.out.format("  Pathway chem %s\n", pathwayChem.getChemical());

                // Get all the standard wells for the pathway chemicals. These wells contain only the
                // the chemical added with controlled solutions (ie no organism or other chemicals in the
                // solution)

                List<StandardWell> standardWells;

                if (plateBarcode != null) {
                    Plate queryPlate = Plate.getPlateByBarcode(db,
                            cl.getOptionValue(OPTION_STANDARD_PLATE_BARCODE));
                    standardWells = analysis.getStandardWellsForChemicalInSpecificPlate(db,
                            pathwayChem.getChemical(), queryPlate.getId());
                } else {
                    standardWells = analysis.getStandardWellsForChemical(db, pathwayChem.getChemical());
                }

                for (StandardWell wellToAnalyze : standardWells) {
                    List<StandardWell> negativeControls = analysis.getViableNegativeControlsForStandardWell(db,
                            wellToAnalyze);
                    Map<StandardWell, List<ScanFile>> allViableScanFiles = analysis
                            .getViableScanFilesForStandardWells(db, wellToAnalyze, negativeControls);

                    List<String> primaryStandardScanFileNames = new ArrayList<>();
                    for (ScanFile scanFile : allViableScanFiles.get(wellToAnalyze)) {
                        primaryStandardScanFileNames.add(scanFile.getFilename());
                    }
                    Plate plate = plateCache.get(wellToAnalyze.getPlateId());
                    if (plate == null) {
                        plate = Plate.getPlateById(db, wellToAnalyze.getPlateId());
                        plateCache.put(plate.getId(), plate);
                    }

                    System.out.format("    Standard well: %s @ %s, '%s'%s%s\n", plate.getBarcode(),
                            wellToAnalyze.getCoordinatesString(), wellToAnalyze.getChemical(),
                            wellToAnalyze.getMedia() == null ? ""
                                    : String.format(" in %s", wellToAnalyze.getMedia()),
                            wellToAnalyze.getConcentration() == null ? ""
                                    : String.format(" @ %s", wellToAnalyze.getConcentration()));
                    System.out.format("      Scan files: %s\n",
                            StringUtils.join(primaryStandardScanFileNames, ", "));

                    for (StandardWell negCtrlWell : negativeControls) {
                        plate = plateCache.get(negCtrlWell.getPlateId());
                        if (plate == null) {
                            plate = Plate.getPlateById(db, negCtrlWell.getPlateId());
                            plateCache.put(plate.getId(), plate);
                        }
                        List<String> negativeControlScanFileNames = new ArrayList<>();
                        for (ScanFile scanFile : allViableScanFiles.get(negCtrlWell)) {
                            negativeControlScanFileNames.add(scanFile.getFilename());
                        }

                        System.out.format("      Viable negative: %s @ %s, '%s'%s%s\n", plate.getBarcode(),
                                negCtrlWell.getCoordinatesString(), negCtrlWell.getChemical(),
                                negCtrlWell.getMedia() == null ? ""
                                        : String.format(" in %s", negCtrlWell.getMedia()),
                                negCtrlWell.getConcentration() == null ? ""
                                        : String.format(" @ %s", negCtrlWell.getConcentration()));
                        System.out.format("        Scan files: %s\n",
                                StringUtils.join(negativeControlScanFileNames, ", "));
                        // TODO: do something useful with the standard wells and their scan files, and then stop all the printing.
                    }
                }
            }
        }
    }
}

From source file:com.siemens.sw360.exporter.CSVExport.java

@NotNull
private static ByteArrayOutputStream getCSVOutputStream(Iterable<String> csvHeaderIterable,
        Iterable<Iterable<String>> inputIterable) throws IOException {
    final ByteArrayOutputStream outB = new ByteArrayOutputStream();
    try (Writer out = new BufferedWriter(new OutputStreamWriter(outB));) {
        CSVPrinter csvPrinter = new CSVPrinter(out, CommonUtils.sw360CsvFormat);
        csvPrinter.printRecord(csvHeaderIterable);
        csvPrinter.printRecords(inputIterable);
        csvPrinter.flush();//from  w w w .j a  va 2  s . c om
        csvPrinter.close();
    } catch (Exception e) {
        outB.close();
        throw e;
    }

    return outB;

}

From source file:com.streamsets.pipeline.lib.util.CsvUtil.java

public static String csvRecordToString(Record r, CSVFormat csvFormat) throws IOException {
    StringWriter stringWriter = new StringWriter();
    CSVPrinter csvPrinter = new CSVPrinter(stringWriter, csvFormat);
    csvPrinter.printRecord(CsvUtil.fieldToCsv(r.get()));
    csvPrinter.flush();// w w w  .  j a v a 2  s  . com
    csvPrinter.close();
    return stringWriter.toString();
}

From source file:ddf.catalog.transformer.csv.common.CsvTransformer.java

private static void printHeaders(final CSVPrinter csvPrinter, final Iterator iterator) {
    try {//from   w  w  w.  j  a v  a2  s.c  o  m
        csvPrinter.printRecord(() -> iterator);
    } catch (IOException ioe) {
        LOGGER.debug("Failed to print the CSV header data.", ioe);
    }
}

From source file:ddf.catalog.transformer.csv.common.CsvTransformer.java

private static void printMetacardData(final CSVPrinter csvPrinter, final Iterator iterator, Metacard metacard) {
    try {//w  w w  .j a va2s .c o  m
        csvPrinter.printRecord(() -> iterator);
    } catch (IOException ioe) {
        LOGGER.debug("Failed to print the CSV data for metacard with id: {}", metacard.getId(), ioe);
    }
}

From source file:de.tudarmstadt.ukp.experiments.argumentation.sequence.significance.SignificanceMain.java

/**
 * Prints table to output string as CSV// www. j  a  va  2s  .  co m
 *
 * @param out   output
 * @param <T>   value type
 * @param table table
 * @throws IOException
 */
public static <T> String tableToCsv(Table<String, String, Boolean> table) throws IOException {
    StringWriter sw = new StringWriter();
    CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);

    List<String> firstRow = new ArrayList<>();
    firstRow.add(" ");
    firstRow.addAll(table.columnKeySet());
    printer.printRecord(firstRow);

    for (String rowKey : table.rowKeySet()) {
        printer.print(rowKey);
        for (String columnKey : table.columnKeySet()) {
            printer.print(table.get(rowKey, columnKey));
        }
        printer.println();
    }

    printer.close();

    return sw.toString();
}

From source file:com.fbartnitzek.tasteemall.data.csv.CsvFileWriter.java

public static String writeFile(String[] headers, List<List<String>> entries, File file) {

    FileWriter fileWriter = null;
    CSVPrinter csvFilePrinter = null;
    String msg = null;//from w ww . j a va  2 s  .co  m
    try {
        fileWriter = new FileWriter(file); //initialize FileWriter object
        csvFilePrinter = new CSVPrinter(fileWriter, CSV_FORMAT_RFC4180); //initialize CSVPrinter object
        csvFilePrinter.printRecord(Arrays.asList(headers)); //Create CSV file header

        //Write a new student object list to the CSV file
        for (List<String> dataEntry : entries) {
            csvFilePrinter.printRecord(dataEntry);
        }

    } catch (Exception e) {
        e.printStackTrace();
        msg = "Error in CsvFileWriter !!!";
    } finally {
        try {
            if (fileWriter != null) {
                fileWriter.flush();
                fileWriter.close();
            }
            if (csvFilePrinter != null) {
                csvFilePrinter.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
            msg = "Error while flushing/closing fileWriter/csvPrinter !!!";

        }
    }
    return msg;
}

From source file:de.tudarmstadt.ukp.experiments.argumentation.sequence.evaluation.helpers.FinalTableExtractor.java

public static <T> String tableToCsv(Table<String, String, T> table) throws IOException {
    StringWriter sw = new StringWriter();
    CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);

    List<String> firstRow = new ArrayList<>();
    firstRow.add(" ");
    firstRow.addAll(table.columnKeySet());
    printer.printRecord(firstRow);

    for (String rowKey : table.rowKeySet()) {
        printer.print(rowKey);/*from   www  . ja  va 2 s .  c  o m*/
        for (String columnKey : table.columnKeySet()) {
            printer.print(table.get(rowKey, columnKey));
        }
        printer.println();
    }

    printer.close();

    return sw.toString();
}

From source file:com.streamsets.pipeline.kafka.common.KafkaTestUtil.java

public static List<KeyedMessage<String, String>> produceCsvMessages(String topic, String partition,
        CSVFormat csvFormat, File csvFile) throws IOException {
    List<KeyedMessage<String, String>> messages = new ArrayList<>();
    String line;/*from   w w w  .  j a va2  s .c om*/
    BufferedReader bufferedReader = new BufferedReader(
            new FileReader(KafkaTestUtil.class.getClassLoader().getResource("testKafkaTarget.csv").getFile()));
    while ((line = bufferedReader.readLine()) != null) {
        String[] strings = line.split(",");
        StringWriter stringWriter = new StringWriter();
        CSVPrinter csvPrinter = new CSVPrinter(stringWriter, csvFormat);
        csvPrinter.printRecord(strings);
        csvPrinter.flush();
        csvPrinter.close();
        messages.add(new KeyedMessage<>(topic, partition, stringWriter.toString()));
    }
    return messages;
}