List of usage examples for org.apache.commons.csv CSVRecord getRecordNumber
public long getRecordNumber()
From source file:com.mahisoft.elasticsearchprediction.engine.ElasticsearchGenericIndexEngine.java
private void loadData(File dataFile, Client client, String indexName, String mappingFilename) throws IOException { CSVParser parser = null;//from w w w .j av a 2 s.c o m PrintWriter mappingFileWriter = null; List<String> headers = new ArrayList<String>(); try { mappingFileWriter = new PrintWriter(mappingFilename, Constants.UTF8); parser = CSVParser.parse(dataFile, Charset.forName(Constants.UTF8), CSVFormat.RFC4180); for (CSVRecord csvRecord : parser) { if (csvRecord.getRecordNumber() == 1) { addHeaders(csvRecord, headers); continue; } if (csvRecord.getRecordNumber() == 2) { createIndex(client, indexName, mappingFileWriter, headers, csvRecord); } addValue(client, indexName, headers, csvRecord); } } finally { if (mappingFileWriter != null) mappingFileWriter.close(); if (parser != null) parser.close(); } LOGGER.info("Done!"); }
From source file:com.awesheet.managers.CSVManager.java
/** * Imports a Sheet from a CSV file in the specified path. * @param path a CSV File Path.//from w w w .ja v a 2 s .com * @return a new Sheet or null if parsing failed */ public Sheet importSheet(String path) { File csvData = new File(path); // Parse the CSV file. CSVParser parser; try { parser = CSVParser.parse(csvData, Charset.defaultCharset(), CSVFormat.RFC4180); } catch (IOException e) { return null; } // Create our new sheet. Sheet sheet = new Sheet("Imported Sheet"); // Populate its cells. for (CSVRecord record : parser) { for (int x = 0; x < record.size(); ++x) { sheet.setCellValue(x, (int) record.getRecordNumber() - 1, record.get(x), true); } } return sheet; }
From source file:functions.LoadCSVdata.java
public void LoadData() { // ArrayList<Resident> residents = new ArrayList<>(); try {//from w ww. j a v a 2 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:de.inren.service.banking.BankDataServiceImpl.java
@Override public void importTransactionCsv(byte[] bytes) throws IOException { Iterable<CSVRecord> records = getIngDibaCsvFormat().parse(createReader(bytes)); Account account = new Account(); for (CSVRecord record : records) { switch ((int) record.getRecordNumber()) { case 1: // Umsatzanzeige break; case 2: // Kunde account.setOwner(record.get(1).trim()); break; case 3: // Konto String[] vals = record.get(1).split(":"); account.setName(vals[0].trim()); account.setNumber(vals[1].trim()); account = validateAccount(account); break; case 4: // break; case 5: // Zeitraum break; case 6: // Saldo break; case 7: // Leer break; case 8: // berschrift break; default: // Eintrag Transaction transaction = new Transaction(); transaction.setAccountNumber(account.getNumber().trim()); transaction.setAccountingDate(getDate(record.get(0))); transaction.setValutaDate(getDate(record.get(1))); transaction.setPrincipal(record.get(2).trim()); transaction.setAccountingText(record.get(3).trim()); transaction.setPurpose(record.get(4).trim()); transaction.setAmount(getBigDecimal(record.get(5))); transaction.setTransactionCurrency(record.get(6).trim()); transaction.setBalance(getBigDecimal(record.get(7))); transaction.setBalanceCurrency(record.get(8).trim()); transaction.setHashCode(transaction.createHashCode()); Transaction oldTransaction = transactionRepository.findByHashCode(transaction.getHashCode()); // only save new transactions if (oldTransaction == null) { transactionRepository.save(transaction); }//w w w. j a v a 2 s. c om } } // Add the categories to the new (all) transactions. Should be // optimized. Iterable<Category> categories = categoryRepository.findAll(); for (Category category : categories) { applyCategoryToTransactions(category); } }
From source file:com.datascience.hadoop.CsvRecordReader.java
@Override public boolean next(LongWritable key, ListWritable<Text> value) throws IOException { value.clear();/* www .j av a 2s. com*/ try { if (iterator.hasNext()) { CSVRecord record = iterator.next(); position++; colLength = colLength == null ? record.size() : colLength; if ((!record.isConsistent() || record.size() != colLength) && strict) { String message = String.format("%s: %s", "inconsistent record at position", position); throw new CsvParseException(message); } key.set(record.getRecordNumber()); for (int i = 0; i < record.size(); i++) { String item = record.get(i); if (item == null) { value.add(null); } else { Text text = cache[i]; if (text == null) { text = new Text(); cache[i] = text; } text.set(item); value.add(text); } } //position = record.getCharacterPosition(); return true; } } catch (Exception e) { LOGGER.warn("failed to parse record at position: " + position); if (strict) { throw e; } else { return next(key, value); } } return false; }
From source file:com.archimatetool.csv.importer.CSVImporter.java
/** * @param csvRecord/*w w w .jav a 2s . co m*/ * @param fields * @return True if csvRecord matches a header with given fields */ private boolean isHeaderRecord(CSVRecord csvRecord, String[] fields) { if (csvRecord.getRecordNumber() != 1 && csvRecord.size() != fields.length) { return false; } for (int i = 0; i < fields.length; i++) { String field = fields[i]; if (!field.equalsIgnoreCase(csvRecord.get(i))) { return false; } } return true; }
From source file:frames.MainGUI.java
public void LoadRentToJtable(JTable t, File file) { try {/*from w w w .ja va2s.c o m*/ CSVParser parser = CSVParser.parse(file, Charset.forName("UTF-8"), CSVFormat.DEFAULT); //t.setModel(tm); DefaultTableModel model = new DefaultTableModel(); //model.setRowCount(0); for (CSVRecord c : parser) { if (c.getRecordNumber() == 1) { model.addColumn("??"); model.addColumn(""); model.addColumn("?"); model.addColumn("?"); model.addColumn(""); model.addColumn(""); t.setModel(model); model = (DefaultTableModel) t.getModel(); continue; } model.addRow(new Object[] { c.get(0), c.get(1), c.get(2), c.get(3), c.get(4), c.get(5) }); } } catch (Exception e) { System.out.println(e); } }
From source file:frames.MainGUI.java
public void LoadFeeDataToJTable(JTable t, File file) { try {/*from w w w .j ava2 s .com*/ CSVParser parser = CSVParser.parse(file, Charset.forName("UTF-8"), CSVFormat.DEFAULT); //t.setModel(tm); DefaultTableModel model = new DefaultTableModel(); //model.setRowCount(0); for (CSVRecord c : parser) { if (c.getRecordNumber() == 1) { model.addColumn(c.get(datatype.GlobalVariable.TYPE)); model.addColumn(c.get(datatype.GlobalVariable.AMOUNT)); model.addColumn(c.get(datatype.GlobalVariable.PAID_BY)); model.addColumn(c.get(datatype.GlobalVariable.PAYER)); t.setModel(model); model = (DefaultTableModel) t.getModel(); 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.xceptance.xlt.common.util.CSVBasedURLAction.java
/** * Constructor based upon read CSV data/*from ww w. java2 s . c o m*/ * * @param record * the record to process * @param interpreter * the bean shell interpreter to use * @throws UnsupportedEncodingException * @throws MalformedURLException */ public CSVBasedURLAction(final CSVRecord record, final ParamInterpreter interpreter) throws UnsupportedEncodingException, MalformedURLException { // no bean shell, so we do not do anything, satisfy final here this.interpreter = interpreter; // the header is record 1, so we have to subtract one, for autonaming this.name = StringUtils.defaultIfBlank(record.get(NAME), "Action-" + (record.getRecordNumber() - 1)); // take care of type String _type = StringUtils.defaultIfBlank(record.get(TYPE), TYPE_ACTION); if (!_type.equals(TYPE_ACTION) && !_type.equals(TYPE_STATIC) && !_type.equals(TYPE_XHR_ACTION)) { XltLogger.runTimeLogger.warn(MessageFormat.format("Unknown type '{0}' in line {1}, defaulting to 'A'", _type, record.getRecordNumber())); _type = TYPE_ACTION; } this.type = _type; // we need at least an url, stop here of not given this.urlString = record.get(URL); if (this.urlString == null) { throw new IllegalArgumentException(MessageFormat .format("No url given in record in line {0}. Need at least that.", record.getRecordNumber())); } this.url = interpreter == null ? new URL(this.urlString) : null; // take care of method String _method = StringUtils.defaultIfBlank(record.get(METHOD), GET); if (!_method.equals(GET) && !_method.equals(POST)) { XltLogger.runTimeLogger.warn(MessageFormat.format( "Unknown method '{0}' in line {1}, defaulting to 'GET'", _method, record.getRecordNumber())); _method = GET; } this.method = _method; // get the response code validator this.httpResponseCodeValidator = StringUtils.isNotBlank(record.get(RESPONSECODE)) ? new HttpResponseCodeValidator(Integer.parseInt(record.get(RESPONSECODE))) : HttpResponseCodeValidator.getInstance(); // compile pattern only, if no interpreter shall be used this.regexpString = StringUtils.isNotEmpty(record.get(REGEXP)) ? record.get(REGEXP) : null; if (interpreter == null) { this.regexp = StringUtils.isNotEmpty(regexpString) ? RegExUtils.getPattern(regexpString) : null; } else { this.regexp = null; } this.xPath = StringUtils.isNotBlank(record.get(XPATH)) ? record.get(XPATH) : null; this.text = StringUtils.isNotEmpty(record.get(TEXT)) ? record.get(TEXT) : null; this.encoded = StringUtils.isNotBlank(record.get(ENCODED)) ? Boolean.parseBoolean(record.get(ENCODED)) : false; // ok, get all the parameters for (int i = 1; i <= DYNAMIC_GETTER_COUNT; i++) { xpathGetterList.add(StringUtils.isNotBlank(record.get(XPATH_GETTER_PREFIX + i)) ? record.get(XPATH_GETTER_PREFIX + i) : null); regexpGetterList.add(StringUtils.isNotBlank(record.get(REGEXP_GETTER_PREFIX + i)) ? record.get(REGEXP_GETTER_PREFIX + i) : null); } // ok, this is the tricky part this.parameters = StringUtils.isNotBlank(record.get(PARAMETERS)) ? setupParameters(record.get(PARAMETERS)) : null; }
From source file:edu.isi.misd.scanner.network.modules.worker.processors.ptr.PrepToResearchProcessor.java
private PrepToResearchResponse analyzeFile(PrepToResearchRequest request, File analysisFile) throws Exception { PrepToResearchResponse response = new PrepToResearchResponse(); Integer requestedOmopConceptID = request.getOmopConceptID(); CSVFormat csvFormat = CSVFormat.newFormat(',').withHeader().withCommentMarker('#').withQuote('"'); CSVParser parser = CSVParser.parse(analysisFile, Charset.defaultCharset(), csvFormat); for (CSVRecord csvRecord : parser) { try {//from w w w. j a v a 2s . co m this.validateCSVRecord(csvRecord); // check the ID first, if no match continue Integer omopConceptID = Integer .parseInt(csvRecord.get(ExpectedColumnName.OMOP_CONCEPT_ID.toString())); if (!requestedOmopConceptID.equals(omopConceptID)) { continue; } // match found, create response output record if (log.isDebugEnabled()) { log.debug(String.format("Found a match for requested ID %s, record: %s", requestedOmopConceptID, csvRecord.toString())); } PrepToResearchRecord ptrRecord = new PrepToResearchRecord(); ptrRecord.setOmopConceptID(omopConceptID); ptrRecord.setOmopConceptName(csvRecord.get(ExpectedColumnName.OMOP_CONCEPT_NAME)); ptrRecord.setCategory(csvRecord.get(ExpectedColumnName.CATEGORY)); ptrRecord.setCategoryValue(csvRecord.get(ExpectedColumnName.CATEGORY_VALUE)); ptrRecord.setCountFemales(Integer.parseInt(csvRecord.get(ExpectedColumnName.COUNT_FEMALES))); ptrRecord.setCountMales(Integer.parseInt(csvRecord.get(ExpectedColumnName.COUNT_MALES))); ptrRecord.setCountTotal(Integer.parseInt(csvRecord.get(ExpectedColumnName.COUNT_TOTAL))); response.getPrepToResearchRecord().add(ptrRecord); } catch (Exception e) { String error = String.format( "An exception occured while processing row number %s with the following values %s: %s", csvRecord.getRecordNumber(), csvRecord.toString(), e.toString()); parser.close(); throw new RuntimeException(error); } } parser.close(); return response; }