List of usage examples for org.apache.commons.csv CSVRecord get
public String get(final String name)
From source file:streaming.core.GenericTweetsProcessor.java
public static void main(String[] args) throws Exception { Reader in = new FileReader("data/us-election-aprial07.csv"); Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(in); for (CSVRecord record : records) { System.out.println(record.get(0) + " " + record.get(1)); }/* w w w . j a va 2s . co m*/ }
From source file:testes.CveCsvReader.java
public static void main(String[] args) { int contCandidate = 0, contEntry = 0, contReserved = 0, contReject = 0, contDisputed = 0, contEqual = 0; String last = ""; try {//from ww w .j a v a2 s .c om File csvData = new File("dataset/base_dados_cve.csv"); // File csvDataOut = new File("dataset/cve_out.csv"); // FileWriter outFile = new FileWriter(csvDataOut); // CSVPrinter csvPrinter = new CSVPrinter((Appendable) outFile, CSVFormat.RFC4180); CSVParser parser = CSVParser.parse(csvData, Charset.forName("ISO-8859-1"), CSVFormat.RFC4180); for (CSVRecord csvRecord : parser) { //System.out.println("Nmero de campos: " + csvRecord.size()); //System.out.println(csvRecord.get(0)); if (csvRecord.get(1).equals("Candidate")) { contCandidate++; } else if (csvRecord.get(1).equals("Entry")) { contEntry++; } if (csvRecord.get(2).startsWith("** RESERVED **")) { contReserved++; } else if (csvRecord.get(2).startsWith("** REJECT **")) { contReject++; } else if (csvRecord.get(2).startsWith("** DISPUTED **")) { contDisputed++; } else { if (last.equals(csvRecord.get(2))) { contEqual++; } else { // csvPrinter.printRecord(csvRecord); } last = csvRecord.get(2); } } System.out.println("Nmero de Registros: " + parser.getRecordNumber()); //csvPrinter.close(); } catch (IOException ex) { Logger.getLogger(CveCsvReader.class.getName()).log(Level.SEVERE, null, ex); } System.out.println("Nmero CANDIDATE: " + contCandidate); System.out.println("Nmero ENTRY: " + contEntry); System.out.println("Nmero REJECT: " + contReject); System.out.println("Nmero RESERVED: " + contReserved); System.out.println("Nmero DISPUTED: " + contDisputed); System.out.println("Nmero IGUAIS: " + contEqual); }
From source file:tr.edu.firat.ceng.aml.assignments.decisiontree.util.CSV2DatasetUtil.java
public Dataset convert(String resourceName) throws UnsupportedEncodingException, IOException { Reader reader = null;/*from www . j a v a2 s .co m*/ try { List<Property> properties = new ArrayList<Property>(); properties.add(new NumericPropertyImpl("sepal_length")); properties.add(new NumericPropertyImpl("sepal_width")); properties.add(new NumericPropertyImpl("petal_length")); properties.add(new NumericPropertyImpl("petal_width")); ClassProperty classProperty = new ClassPropertyImpl("class"); final URL url = getClass().getResource(resourceName); reader = new InputStreamReader(url.openStream(), "UTF-8"); CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT); for (CSVRecord record : parser) { for (int i = 0; i < properties.size(); i++) { Property get = properties.get(i); if (get instanceof NumericProperty) { NumericProperty numericProperty = (NumericProperty) get; numericProperty.getValues().add(new Double(record.get(i))); } } classProperty.getValues().add(record.get(properties.size())); } Dataset dataset = new DatasetImpl(classProperty); dataset.addProperties(properties); return dataset; } finally { try { if (reader != null) { reader.close(); } } catch (IOException ex) { System.out.println(ex); } } }
From source file:tr.edu.firat.ceng.aml.assignments.decisiontree.util.CSV2DatasetUtil.java
private List<String> getNominalProperty(CSVParser parser, int index) { List<String> values = new ArrayList<String>(); for (CSVRecord record : parser) { values.add(record.get(index)); }//from ww w . j av a 2s.c o m return values; }
From source file:tr.edu.firat.ceng.aml.assignments.decisiontree.util.CSV2DatasetUtil.java
private List<Number> getNumericProperty(CSVParser parser, int index) { List<Number> values = new ArrayList<Number>(); for (CSVRecord record : parser) { values.add(new Double(record.get(index))); }/*w ww . ja va2s. c om*/ return values; }
From source file:trainer.userinput.TrainingFileDB.java
public static UserInputTrainingRecord parseLine(String line) throws IOException { CSVParser lineParser = CSVParser.parse(line, TrainingFileDB.getCSVFormat()); List<CSVRecord> csvRecords = lineParser.getRecords(); UserInputTrainingRecord retVal = null; for (CSVRecord record : csvRecords) { retVal = new UserInputTrainingRecord(record.get(0), record.get(1)); }//from w ww.jav a 2 s . c o m return retVal; }
From source file:uk.bl.dpt.qa.ProcessIsolatedTika.java
/** * Parse an inputstream and populate a Metadata object * @param pInputStream stream to analyse * @param pMetadata metadata object to populate * @param pOutputStream output to write data to * @return true if processed ok, false if execution was terminated *//*from ww w . j av a 2 s .c o m*/ public boolean parse(final InputStream pInputStream, final Metadata pMetadata) { boolean ret = true; if (!gRunner.isRunning()) { gLogger.error("Tika-Server is not running"); return false; } final String TIKA_PATH = "/meta"; final String END_POINT = "http://" + TIKA_LOCAL_HOST + ":" + TIKA_SERVER_PORT; gLogger.trace("Server: " + END_POINT + TIKA_PATH); final String detectedType = pMetadata.get(Metadata.CONTENT_TYPE); FutureTask<Integer> task = new FutureTask<Integer>(new Callable<Integer>() { @Override public Integer call() throws Exception { gResponse = WebClient.create(END_POINT + TIKA_PATH).accept("text/csv") // give the parsers a hint .type(detectedType) // protect the stream from being closed .put(new CloseShieldInputStream(pInputStream)); return null; } }); Thread thread = new Thread(task); thread.start(); try { task.get(TIMEOUT_SECS * 1000, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { gLogger.info("InterruptedException: " + e); ret = false; restart(); } catch (ExecutionException e) { gLogger.info("ExecutionException: " + e); ret = false; restart(); } catch (TimeoutException e) { gLogger.info("TimeoutException: " + e); ret = false; restart(); } if (gResponse != null) { if (gResponse.getStatus() == Status.UNSUPPORTED_MEDIA_TYPE.getStatusCode()) { // the server may return HTTP 415 (unsupported) if it won't accept the mimetype // handle this issue here // add some text to the output // FIXME: maybe change mimetype for a more visible error? pMetadata.add("parseFailure415", "true"); gLogger.error("Parse Failure: HTTP 415 (format unsupported for parsing)"); } else { if (gResponse.getEntity() instanceof InputStream) { InputStream is = (InputStream) gResponse.getEntity(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); try { Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(reader); for (CSVRecord record : records) { pMetadata.add(record.get(0), record.get(1)); } } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); ret = false; } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } } gLogger.info("Metadata entries: " + pMetadata.names().length); return ret; }
From source file:uk.co.inetria.gce.GceUsageParser.java
public void parse() throws FileNotFoundException, IOException { for (String file : files) { try (BufferedReader reader = new BufferedReader(new FileReader(file), BUF_SIZE);) { Iterable<CSVRecord> records = CSVFormat.DEFAULT.withHeader().withSkipHeaderRecord().parse(reader); for (CSVRecord record : records) { String measurement = StringUtils.remove(record.get(1), MEASURE_PREFIX); this.measurementIds.add(measurement); if (measurement.contains(VM) || measurement.contains(CONTAINER_ENGINE_VM)) { this.numberOfVms++; this.vms.add(record.get(4)); }// ww w . j a v a 2 s.c o m Usage usage = this.usages.get(measurement); if (usage == null) { usage = new Usage(); this.usages.put(measurement, usage); } long value = Long.parseLong(record.get(2)); usage.raw += value; if (measurement.contains(VM) || measurement.contains(CONTAINER_ENGINE_VM)) { // hourly based billing long adjusted = value; if (adjusted < HOUR) { adjusted = HOUR; } else if (adjusted % HOUR > 0) { adjusted = (long) (HOUR * Math.ceil(adjusted / (double) HOUR)); } usage.adjusted += adjusted; } } } } System.out.println("Unique measurements"); for (String measureId : this.measurementIds) { System.out.println(measureId); } System.out.println("Total number of started VMs: " + this.numberOfVms); System.out.println("Total number of unique VMs: " + this.vms.size()); for (String vmId : this.vms) { System.out.println(vmId); } System.out.println("Aggregated usage"); System.out.println("MeasurementId,Quantity,Per-hour Quantity"); for (Entry<String, Usage> entry : this.usages.entrySet()) { Usage usage = entry.getValue(); System.out.println(entry.getKey() + ',' + usage.raw + ',' + usage.adjusted); } }
From source file:umich.ms.batmass.filesupport.files.types.mzrt.providers.MzrtFeaturesDataSource.java
@Override public MzrtFeatures load() throws DataLoadingException { Path path = Paths.get(this.getOriginURI()); MzrtFile file = new MzrtFile(path); file.load();/*from w w w .ja v a 2s .com*/ MzrtFeatures features = new MzrtFeatures(file); Map<String, Integer> header = file.getHeader(); List<CSVRecord> records = file.getRecords(); int[] idxs = file.getIndexesMzRtColorOpacity(); for (CSVRecord record : records) { String mzLo = record.get(idxs[0]); String mzHi = record.get(idxs[1]); String rtLo = record.get(idxs[2]); String rtHi = record.get(idxs[3]); double mlo, mhi, rlo, rhi; try { mlo = Double.parseDouble(mzLo); mhi = Double.parseDouble(mzHi); rlo = Double.parseDouble(rtLo); rhi = Double.parseDouble(rtHi); double mz = (mlo + mhi) / 2; MzrtBox box = new MzrtBox(mz, rlo, rhi, mlo, mhi); MzrtFeature mzrtFeature = new MzrtFeature(new MzrtBox[] { box }, record); if (idxs[4] >= 0) { String colorStr = record.get(idxs[4]); try { Color color = Color.decode(colorStr); mzrtFeature.setColor(color); } catch (NumberFormatException ex) { throw new DataLoadingException("Could not decode color string"); } } if (idxs[5] >= 0) { String opacityStr = record.get(idxs[5]); float opacity = Float.parseFloat(opacityStr); mzrtFeature.setOpacity(opacity); } features.add(mzrtFeature, 1, null); } catch (NumberFormatException ex) { throw new DataLoadingException(ex); } } return features; }