List of usage examples for org.apache.commons.csv CSVFormat DEFAULT
CSVFormat DEFAULT
To view the source code for org.apache.commons.csv CSVFormat DEFAULT.
Click Source Link
From source file:com.publictransitanalytics.scoregenerator.datalayer.directories.GTFSReadingStopDetailsDirectory.java
public GTFSReadingStopDetailsDirectory(final Store<StopIdKey, StopDetails> stopDetailsStore, final Reader stopDetailsReader) throws IOException, InterruptedException { this.stopDetailsStore = stopDetailsStore; try {//from ww w . ja v a 2 s . co m if (stopDetailsStore.isEmpty()) { log.info("Building stop details directory."); final CSVParser parser = new CSVParser(stopDetailsReader, CSVFormat.DEFAULT.withHeader()); final List<CSVRecord> stopDetailsRecords = parser.getRecords(); for (CSVRecord record : stopDetailsRecords) { final String latitude = record.get("stop_lat"); final String longitude = record.get("stop_lon"); final String stopId = record.get("stop_id"); final StopDetails stopDetails = new StopDetails(stopId, record.get("stop_name"), new Coordinate(latitude, longitude)); stopDetailsStore.put(new StopIdKey(stopId), stopDetails); } } } catch (final BitvantageStoreException e) { throw new ScoreGeneratorFatalException(e); } }
From source file:io.ecarf.core.cloud.task.processor.reason.phase2.ReasonUtils.java
/** * //w w w .ja v a 2 s .c o m * @param file * @param writer * @param compressed * @return * @throws IOException */ public static int reason(String inFile, String outFile, boolean compressed, Map<Long, Set<Triple>> schemaTerms, Set<Long> productiveTerms) throws IOException { log.info("Reasoning for file: " + inFile + ", memory usage: " + Utils.getMemoryUsageInGB() + "GB"); int inferredTriples = 0; // loop through the instance triples probably stored in a file and generate all the triples matching the schema triples set try (BufferedReader reader = getQueryResultsReader(inFile, compressed); PrintWriter writer = new PrintWriter(new BufferedOutputStream( new GZIPOutputStream(new FileOutputStream(outFile), Constants.GZIP_BUF_SIZE), Constants.GZIP_BUF_SIZE));) { Iterable<CSVRecord> records; if (compressed) { // ignore first row subject,predicate,object records = CSVFormat.DEFAULT.withHeader().withSkipHeaderRecord().parse(reader); } else { records = CSVFormat.DEFAULT.parse(reader); } Long term; for (CSVRecord record : records) { ETriple instanceTriple = ETriple.fromCSV(record.values()); // TODO review for OWL ruleset if (SchemaURIType.RDF_TYPE.id == instanceTriple.getPredicate()) { term = instanceTriple.getObject(); // object } else { term = instanceTriple.getPredicate(); // predicate } Set<Triple> schemaTriples = schemaTerms.get(term); if ((schemaTriples != null) && !schemaTriples.isEmpty()) { productiveTerms.add(term); for (Triple schemaTriple : schemaTriples) { Rule rule = GenericRule.getRule(schemaTriple); Triple inferredTriple = rule.head(schemaTriple, instanceTriple); if (inferredTriple != null) { writer.println(inferredTriple.toCsv()); inferredTriples++; } } } } } return inferredTriples; }
From source file:com.streamsets.pipeline.lib.parser.delimited.TestDelimitedCharDataParser.java
@Test public void testParseNoHeader() throws Exception { OverrunReader reader = new OverrunReader(new StringReader("A,B\na,b"), 1000, true, false); DataParser parser = new DelimitedCharDataParser(getContext(), "id", reader, 0, 0, CSVFormat.DEFAULT, CsvHeader.NO_HEADER, -1, CsvRecordType.LIST); Assert.assertEquals("0", parser.getOffset()); Record record = parser.parse();// w ww. ja v a 2s . c o m Assert.assertNotNull(record); Assert.assertEquals("id::0", record.getHeader().getSourceId()); Assert.assertEquals("A", record.get().getValueAsList().get(0).getValueAsMap().get("value").getValueAsString()); Assert.assertFalse(record.has("[0]/header")); Assert.assertEquals("B", record.get().getValueAsList().get(1).getValueAsMap().get("value").getValueAsString()); Assert.assertFalse(record.has("[1]/header")); Assert.assertEquals("4", parser.getOffset()); record = parser.parse(); Assert.assertNotNull(record); Assert.assertEquals("id::4", record.getHeader().getSourceId()); Assert.assertEquals("a", record.get().getValueAsList().get(0).getValueAsMap().get("value").getValueAsString()); Assert.assertFalse(record.has("[0]/header")); Assert.assertEquals("b", record.get().getValueAsList().get(1).getValueAsMap().get("value").getValueAsString()); Assert.assertFalse(record.has("[1]/header")); Assert.assertEquals("7", parser.getOffset()); record = parser.parse(); Assert.assertNull(record); Assert.assertEquals("-1", parser.getOffset()); parser.close(); }
From source file:functions.LoadCSVdata.java
public void LoadData() { // ArrayList<Resident> residents = new ArrayList<>(); try {//from w w w . j a va2 s . co m // READ FEE TABLE file = new File(PATH + FEE_FILE); System.out.println(file.toPath()); parser = CSVParser.parse(file, Charset.forName("UTF-8"), CSVFormat.DEFAULT); Fee new_fee; Resident new_res; for (CSVRecord c : parser) { // skip first record if (c.getRecordNumber() == 1) continue; System.out.println(c.get(datatype.GlobalVariable.TYPE) + ", " + c.get(datatype.GlobalVariable.AMOUNT) + ", " + c.get(datatype.GlobalVariable.PAID_BY) + ", " + c.get(datatype.GlobalVariable.PAYER)); String tmp = c.get(datatype.GlobalVariable.PAYER); //String payers; String[] payers = tmp.split(";"); System.out.println(payers.length); new_fee = new Fee(); new_fee.name = c.get(datatype.GlobalVariable.TYPE); new_fee.amount = Double.valueOf(c.get(datatype.GlobalVariable.AMOUNT)); new_fee.number_of_payer = payers.length; System.out.println("new fee: " + new_fee.name); //datatype.GlobalVariable.FEES int res_index; // add payer for (int i = 0; i < payers.length; i++) { res_index = -1; for (Resident r : datatype.GlobalVariable.RESIDENTS) { if (r.name.equals(payers[i])) { res_index = datatype.GlobalVariable.RESIDENTS.indexOf(r); break; } } System.out.println(res_index); // new resident found if (res_index == -1) { new_res = new Resident(); // System.out.println(payers[i]); new_res.name = payers[i]; datatype.GlobalVariable.RESIDENTS.add(new_res); res_index = datatype.GlobalVariable.RESIDENTS.indexOf(new_res); } // insert payer's fee // if(datatype.GlobalVariable.RESIDENTS.get(res_index).extra_fee.size()>=1) // System.out.println(datatype.GlobalVariable.RESIDENTS.get(res_index).extra_fee.get(datatype.GlobalVariable.RESIDENTS.get(res_index).extra_fee.size()-1).name); datatype.GlobalVariable.RESIDENTS.get(res_index).extra_fee.add(new_fee); // System.out.println(datatype.GlobalVariable.RESIDENTS.get(res_index).extra_fee.get(datatype.GlobalVariable.RESIDENTS.get(res_index).extra_fee.size()-1).name); } // add paid by res_index = -1; for (Resident r : datatype.GlobalVariable.RESIDENTS) { if (r.name.equals(c.get(datatype.GlobalVariable.PAID_BY))) { res_index = datatype.GlobalVariable.RESIDENTS.indexOf(r); break; } } // new resident found if (res_index == -1) { new_res = new Resident(); new_res.name = c.get(datatype.GlobalVariable.PAID_BY); datatype.GlobalVariable.RESIDENTS.add(new_res); res_index = datatype.GlobalVariable.RESIDENTS.indexOf(new_res); } // insert paid datatype.GlobalVariable.RESIDENTS.get(res_index).paid.add(new_fee); } file = new File(PATH + RESIDENTS_FILE); // READ RESIDENT TABLE parser = CSVParser.parse(file, Charset.forName("UTF-8"), CSVFormat.DEFAULT); for (CSVRecord c : parser) { // skip first record if (c.getRecordNumber() == 1) continue; System.out .println(c.get(datatype.GlobalVariable.NAME) + ", " + c.get(datatype.GlobalVariable.RENT)); int res_index = -1; for (Resident r : datatype.GlobalVariable.RESIDENTS) { if (r.name.equals(c.get(datatype.GlobalVariable.NAME))) { res_index = datatype.GlobalVariable.RESIDENTS.indexOf(r); break; } } datatype.GlobalVariable.RESIDENTS.get(res_index).basic_rent = Integer .parseInt(c.get(datatype.GlobalVariable.RENT)); } // for(int i=0;i<datatype.GlobalVariable.RESIDENTS.size();i++){ // System.out.println(datatype.GlobalVariable.RESIDENTS.get(i).name); // for(Fee f:datatype.GlobalVariable.RESIDENTS.get(i).extra_fee){ // System.out.println(f.name); // } // } } catch (Exception e) { System.out.println(e); } //return residents; }
From source file:edu.harvard.hms.dbmi.bd2k.irct.ws.rs.resultconverter.CSVTabularDataConverter.java
@Override public StreamingOutput createStream(final Result result) { StreamingOutput stream = new StreamingOutput() { @Override/*from w ww . j a v a 2 s. c o m*/ public void write(OutputStream outputStream) throws IOException, WebApplicationException { ResultSet rs = null; CSVPrinter printer = null; try { rs = (ResultSet) result.getData(); rs.load(result.getResultSetLocation()); printer = new CSVPrinter(new OutputStreamWriter(outputStream), CSVFormat.DEFAULT); String[] columnHeaders = new String[rs.getColumnSize()]; for (int i = 0; i < rs.getColumnSize(); i++) { columnHeaders[i] = rs.getColumn(i).getName(); } printer.printRecord((Object[]) columnHeaders); rs.beforeFirst(); while (rs.next()) { String[] row = new String[rs.getColumnSize()]; for (int i = 0; i < rs.getColumnSize(); i++) { row[i] = rs.getString(i); } printer.printRecord((Object[]) row); } printer.flush(); } catch (ResultSetException | PersistableException e) { log.info("Error creating CSV Stream: " + e.getMessage()); } finally { if (printer != null) { printer.close(); } if (rs != null && !rs.isClosed()) { try { rs.close(); } catch (ResultSetException e) { e.printStackTrace(); } } if (outputStream != null) { outputStream.close(); } } } }; return stream; }
From source file:de.tudarmstadt.ukp.dkpro.argumentation.sequence.report.TokenLevelBatchCrossValidationReport.java
protected void reportOnlyMacroFM() throws IOException { File aggregatedCSVFile = new File( getContext().getStorageLocation(Constants.TEST_TASK_OUTPUT_KEY, StorageService.AccessMode.READONLY), TokenLevelEvaluationReport.TOKEN_LEVEL_PREDICTIONS_CSV); // load the CSV CSVParser csvParser = new CSVParser(new FileReader(aggregatedCSVFile), CSVFormat.DEFAULT.withCommentMarker('#')); // compute confusion matrix ConfusionMatrix cm = new ConfusionMatrix(); // and add the all rows for (CSVRecord csvRecord : csvParser) { // first item is the gold label String gold = csvRecord.get(0); // second item is the predicted label String predicted = csvRecord.get(1); cm.increaseValue(gold, predicted); }/*from w w w . j a va2 s .co m*/ File evaluationFile = new File(getContext().getStorageLocation(Constants.TEST_TASK_OUTPUT_KEY, StorageService.AccessMode.READWRITE), RESULT_SUMMARY); ReportTools.printFMeasuresToFile(cm, evaluationFile); }
From source file:mSearch.filmlisten.WriteFilmlistJson.java
public void filmlisteSchreibenJson(String datei, ListeFilme listeFilme) { ZipOutputStream zipOutputStream = null; XZOutputStream xZOutputStream = null; JsonGenerator jg = null;//from w w w . ja v a 2s . c o m FileWriter fileWriter = null; CSVPrinter csvFilePrinter = null; try { Log.sysLog("Filme schreiben (" + listeFilme.size() + " Filme) :"); File file = new File(datei); File dir = new File(file.getParent()); if (!dir.exists()) { if (!dir.mkdirs()) { Log.errorLog(915236478, "Kann den Pfad nicht anlegen: " + dir.toString()); } } Log.sysLog(" --> Start Schreiben nach: " + datei); CSVFormat csvFileFormat = CSVFormat.DEFAULT.withDelimiter(';').withQuote('\'') .withRecordSeparator("\n"); fileWriter = new FileWriter(datei); csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat); // Infos der Felder in der Filmliste csvFilePrinter.printRecord(DatenFilm.COLUMN_NAMES); //Filme schreiben DatenFilm datenFilm; Iterator<DatenFilm> iterator = listeFilme.iterator(); while (iterator.hasNext()) { datenFilm = iterator.next(); datenFilm.arr[DatenFilm.FILM_NEU] = Boolean.toString(datenFilm.isNew()); // damit wirs beim nchsten Programmstart noch wissen List<String> filmRecord = new ArrayList<String>(); for (int i = 0; i < DatenFilm.JSON_NAMES.length; ++i) { int m = DatenFilm.JSON_NAMES[i]; filmRecord.add(datenFilm.arr[m].replace("\n", "").replace("\r", "")); } csvFilePrinter.printRecord(filmRecord); } Log.sysLog(" --> geschrieben!"); } catch (Exception ex) { Log.errorLog(846930145, ex, "nach: " + datei); } finally { try { fileWriter.flush(); fileWriter.close(); csvFilePrinter.close(); } catch (Exception e) { Log.errorLog(732101201, e, "close stream: " + datei); } } }
From source file:com.thinkbiganalytics.discovery.parsers.csv.CSVAutoDetect.java
/** * Parses a sample file to allow schema specification when creating a new feed. * * @param sampleText the sample text/*from www . j a va 2 s. c om*/ * @return A configured parser * @throws IOException If there is an error parsing the sample file */ public CSVFormat detectCSVFormat(String sampleText, boolean headerRow, String seperatorStr) throws IOException { CSVFormat format = CSVFormat.DEFAULT.withAllowMissingColumnNames(); Character separatorChar = null; if (StringUtils.isNotBlank(seperatorStr)) { separatorChar = seperatorStr.charAt(0); } try (BufferedReader br = new BufferedReader(new StringReader(sampleText))) { List<LineStats> lineStats = generateStats(br, separatorChar); Character quote = guessQuote(lineStats); Character delim = guessDelimiter(lineStats, sampleText, quote, headerRow); if (delim == null) { throw new IOException("Unrecognized format"); } format = format.withDelimiter(delim); format = format.withQuoteMode(QuoteMode.MINIMAL).withQuote(quote); } return format; }
From source file:io.v.android.apps.reader.UserActionLogger.java
public void initPrinters() { File dir = Utils.getLogDirectory(); Log.i(TAG, "User action logs are saved at: " + dir.getAbsolutePath()); String startTime = Utils.getTimeString(); File touchLogFile = new File(dir, String.format("reader-%s-touch-%s.log", mDeviceId, startTime)); File navigationLogFile = new File(dir, String.format("reader-%s-navigation-%s.log", mDeviceId, startTime)); try {//from www. j a va 2s. c o m mTouchPrinter = CSVFormat.DEFAULT.withHeader("ACTION", "TIMESTAMP").print(new FileWriter(touchLogFile)); mNavigationPrinter = CSVFormat.DEFAULT.withHeader("ACTION", "VALUE", "TIMESTAMP") .print(new FileWriter(navigationLogFile)); } catch (IOException e) { handleException(e); try { close(); } catch (IOException e2) { // Nothing to do here. } } }
From source file:javalibs.CSVExtractor.java
/** * Read a CSV file and return a list of records representing each row in the CSV * NOTE: This does not handle anything but plain CSV files with default formatting * @param csvPath The path to the CSV file * @return The list of CSVRecord objects *///from w w w. j a v a2s .com public static List<CSVRecord> getCSVRecords(String csvPath) { CSVParser parser = null; List<CSVRecord> records = null; try { parser = new CSVParser(Files.newBufferedReader(Paths.get(csvPath)), CSVFormat.DEFAULT.withHeader().withIgnoreHeaderCase().withTrim()); records = parser.getRecords(); } catch (IOException e) { TSL.get().exception(e); } return records; }