List of usage examples for org.apache.commons.csv CSVParser getRecords
public List<CSVRecord> getRecords() throws IOException
From source file:com.stratio.decision.executables.DataFlowFromCsvMain.java
public static void main(String[] args) throws IOException, NumberFormatException, InterruptedException { if (args.length < 4) { log.info(// ww w.j a v a2 s . com "Usage: \n param 1 - path to file \n param 2 - stream name to send the data \n param 3 - time in ms to wait to send each data \n param 4 - broker list"); } else { Producer<String, String> producer = new Producer<String, String>(createProducerConfig(args[3])); Gson gson = new Gson(); Reader in = new FileReader(args[0]); CSVParser parser = CSVFormat.DEFAULT.parse(in); List<String> columnNames = new ArrayList<>(); for (CSVRecord csvRecord : parser.getRecords()) { if (columnNames.size() == 0) { Iterator<String> iterator = csvRecord.iterator(); while (iterator.hasNext()) { columnNames.add(iterator.next()); } } else { StratioStreamingMessage message = new StratioStreamingMessage(); message.setOperation(STREAM_OPERATIONS.MANIPULATION.INSERT.toLowerCase()); message.setStreamName(args[1]); message.setTimestamp(System.currentTimeMillis()); message.setSession_id(String.valueOf(System.currentTimeMillis())); message.setRequest_id(String.valueOf(System.currentTimeMillis())); message.setRequest("dummy request"); List<ColumnNameTypeValue> sensorData = new ArrayList<>(); for (int i = 0; i < columnNames.size(); i++) { // Workaround Object value = null; try { value = Double.valueOf(csvRecord.get(i)); } catch (NumberFormatException e) { value = csvRecord.get(i); } sensorData.add(new ColumnNameTypeValue(columnNames.get(i), null, value)); } message.setColumns(sensorData); String json = gson.toJson(message); log.info("Sending data: {}", json); producer.send(new KeyedMessage<String, String>(InternalTopic.TOPIC_DATA.getTopicName(), STREAM_OPERATIONS.MANIPULATION.INSERT, json)); log.info("Sleeping {} ms...", args[2]); Thread.sleep(Long.valueOf(args[2])); } } log.info("Program completed."); } }
From source file:ca.craigthomas.visualclassifier.dataset.DataSetReader.java
/** * Read from a CSV file, and return the samples as a list of doubles. * //from ww w . j a va 2 s . co m * @param filename the name of the file to read from * @return the list of samples from the file * @throws IOException */ public static List<List<Double>> readCSVFile(String filename) throws IOException { File file = new File(filename); String fileContents = FileUtils.readFileToString(file); Reader reader = new StringReader(fileContents); CSVFormat format = CSVFormat.EXCEL; CSVParser parser = new CSVParser(reader, format); List<CSVRecord> records = parser.getRecords(); List<List<Double>> inputs = new ArrayList<List<Double>>(); for (CSVRecord record : records) { List<Double> inputLine = new ArrayList<Double>(); for (int index = 0; index < record.size(); index++) { String value = record.get(index); inputLine.add(Double.parseDouble(value)); } inputs.add(inputLine); } parser.close(); return inputs; }
From source file:com.siemens.sw360.datahandler.common.ImportCSV.java
/** * reads a CSV file and returns its content as a list of CSVRecord * * @param in// w ww .j a v a 2s.c o m * @return list of records */ public static List<CSVRecord> readAsCSVRecords(InputStream in) { List<CSVRecord> records = null; try (Reader reader = new InputStreamReader(in)) { CSVParser parser = new CSVParser(reader, CommonUtils.sw360CsvFormat); records = parser.getRecords(); records.remove(0); // Remove header } catch (IOException e) { log.error("Error parsing CSV File!", e); } // To avoid returning null above if (records == null) records = Collections.emptyList(); return records; }
From source file:edu.caltech.ipac.firefly.server.util.DsvToDataGroup.java
public static DataGroup parse(File inf, CSVFormat format) throws IOException { BufferedReader reader = new BufferedReader(new FileReader(inf), IpacTableUtil.FILE_IO_BUFFER_SIZE); List<DataType> columns = new ArrayList<DataType>(); CSVParser parser = new CSVParser(reader, format); List<CSVRecord> records = parser.getRecords(); if (records != null && records.size() > 0) { // parse the column info CSVRecord cols = records.get(0); for (Iterator<String> itr = cols.iterator(); itr.hasNext();) { String s = itr.next(); if (!StringUtils.isEmpty(s)) { columns.add(new DataType(s, null)); // unknown type }//w ww . j a v a 2s.c om } DataGroup dg = new DataGroup(null, columns); // parse the data for (int i = 1; i < records.size(); i++) { DataObject row = parseRow(dg, records.get(i)); if (row != null) { dg.add(row); } } dg.shrinkToFitData(); return dg; } return null; }
From source file:com.ctt.persistance.StorageFile.java
public static void saveTransactions(byte[] file) throws IOException { try {/*from ww w. j av a2 s . c o m*/ List<String> list = new ArrayList<String>(); CSVParser csv; csv = CSVParser.parse(new String(file), CSVFormat.DEFAULT); List<CSVRecord> it = csv.getRecords(); Transaction obj; int i = 0; for (i = 0; i < it.size(); i++) { if (i != 0) { obj = new Transaction((CSVRecord) it.get(i)); if (obj.validateCanonical() && obj.isRepayment()) { list.add(obj.repaymentString()); StorageFile.saveTransaction(obj); } } } FileUtils.writeLines(new File(Main.prop.getProperty("path_repayments")), list, "\n", true); } catch (Exception e) { Main.appendLog("Error: SAVE TRANSACTIONS: " + e.getMessage()); } }
From source file:com.publictransitanalytics.scoregenerator.datalayer.directories.GTFSReadingRouteDetailsDirectory.java
private static void parseRoutesFile(final Store<RouteIdKey, RouteDetails> store, final Reader routeReader) throws InterruptedException, IOException { final CSVParser routeParser = new CSVParser(routeReader, CSVFormat.DEFAULT.withHeader()); final List<CSVRecord> routeRecords = routeParser.getRecords(); for (final CSVRecord record : routeRecords) { String routeId = record.get("route_id"); String routeShortName = record.get("route_short_name"); String routeLongName = record.get("route_long_name"); populateRouteDetail(routeId, routeShortName, routeLongName, store); }/*from ww w . ja v a 2s . c om*/ }
From source file:ca.liquidlabs.android.speedtestvisualizer.util.CsvDataParser.java
/** * Parses CSV data/*from w w w .j a va 2 s . c om*/ * * @param csvHeader Header items for csv records * @param csvData * @return */ public static List<SpeedTestRecord> parseCsvData(String csvHeader, String csvData) { Reader in = new StringReader(getCsvData(csvHeader, csvData)); try { CSVParser parser = new CSVParser(in, CSVFormat.DEFAULT.toBuilder().withHeader().build()); // get the parsed records List<CSVRecord> list = parser.getRecords(); // create a list to convert data into SpeedTestRecord model List<SpeedTestRecord> speedTestRecords = new ArrayList<SpeedTestRecord>(); for (CSVRecord csvRecord : list) { speedTestRecords.add(new SpeedTestRecord(csvRecord)); } return speedTestRecords; } catch (IOException e) { Tracer.error(LOG_TAG, e); } // when no data, send empty list return Collections.emptyList(); }
From source file:com.sojw.TableNamesFinderExecutor.java
public static List<CSVRecord> getCSVFileContents(final String filePath) throws IOException { final Reader in = new BufferedReader(new FileReader(filePath)); final CSVParser parser = new CSVParser(in, CSVFormat.DEFAULT); List<CSVRecord> fileContentList = parser.getRecords(); return fileContentList; }
From source file:com.bigtester.ate.tcg.controller.TrainingFileDB.java
/** * Parses the line./*from w ww . ja va2 s . c o m*/ * * @param line * the line * @return the user input training record * @throws IOException * Signals that an I/O exception has occurred. */ public static UserInputTrainingRecord parseLine(String line) throws IOException { CSVParser lineParser = CSVParser.parse(line, TrainingFileDB.getCSVFormat()); List<CSVRecord> csvRecords = lineParser.getRecords(); UserInputTrainingRecord retVal = null; // NOPMD for (CSVRecord record : csvRecords) { if (null != record) { String temp = record.get(0); String temp2 = record.get(1); if (null == temp) temp = ""; if (null == temp2) temp2 = ""; retVal = new UserInputTrainingRecord(temp, // NOPMD temp2); } } if (null == retVal) throw new IOException(); return retVal; }
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. ja v a 2 s .c o m*/ 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; }