List of usage examples for org.apache.commons.csv CSVParser CSVParser
public CSVParser(final Reader reader, final CSVFormat format) throws IOException
If you do not read all records from the given reader , you should call #close() on the parser, unless you close the reader .
From source file:com.github.jferard.pgloaderutils.sniffer.csv.CSVOptionalHeaderSniffer.java
@Override public void sniff(final InputStream inputStream, final int size) throws IOException { final Reader streamReader = new InputStreamReader(inputStream, this.charset); final CSVParser parser = new CSVParser(streamReader, this.csvFormat); try {//from ww w .ja va 2 s . co m final Iterator<CSVRecord> iterator = parser.iterator(); if (iterator.hasNext()) { final CSVRecord firstRowRecord = iterator.next(); final int firstRowSize = firstRowRecord.size(); final char[] firstRowSignature = this.rowSignaturesAnalyzer.getSignature(firstRowRecord, firstRowSize); if (this.containsAtLeastOneOnlyDigitsValue(firstRowSignature)) { this.header = null; } else { final char[] remainingRowsSignature = this.rowSignaturesAnalyzer .getRemainingRowsSignature(iterator, firstRowSize); if (this.containsAtLeastOneColumnWithLetterHeaderAndDigitValues(firstRowSignature, remainingRowsSignature, firstRowSize)) { // copy firstRow in header for (final String s : firstRowRecord) this.header.add(s); } } } else this.header = null; } finally { parser.close(); } }
From source file:br.edimarmanica.trinity.intrasitemapping.manual.OffsetToRule.java
public void execute() { readMappings();/*from w w w. j a v a 2 s .c o m*/ File dir = new File(Paths.PATH_TRINITY + site.getPath() + "/offset"); for (File offset : dir.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.endsWith(".csv"); } })) { try (Reader in = new FileReader(offset)) { try (CSVParser parser = new CSVParser(in, CSVFormat.EXCEL)) { for (CSVRecord record : parser) { String page = record.get(0); if (pages.contains(page)) { continue; } else { pages.add(page); } List<String> dataRecord = new ArrayList<>(); for (Attribute attr : site.getDomain().getAttributes()) { try { int group = mappings.get(offset.getName()).get(attr.getAttributeID()); if (group != -1) { dataRecord.add(record.get(group)); } else { dataRecord.add(""); } } catch (Exception ex) { dataRecord.add(""); } } print(page, dataRecord); } } } catch (FileNotFoundException ex) { Logger.getLogger(Mapping.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(Mapping.class.getName()).log(Level.SEVERE, null, ex); } } }
From source file:com.blackducksoftware.integration.hubdiff.HubDiffTest.java
@Test public void csvTest() throws IOException, IllegalArgumentException, EncryptionException, HubIntegrationException, JSONException { HubDiff hubDiff = new HubDiff(doc1, doc2); hubDiff.writeDiffAsCSV(actualFile);//from ww w.j a v a 2s . c om CSVParser expectedParser = new CSVParser(new FileReader(expectedFile), CSVFormat.EXCEL); CSVParser actualParser = new CSVParser(new FileReader(actualFile), CSVFormat.EXCEL); List<CSVRecord> expectedRecords = expectedParser.getRecords(); List<CSVRecord> actualRecords = actualParser.getRecords(); assertEquals(expectedRecords.size(), actualRecords.size()); for (int i = 0; i < expectedRecords.size(); i++) { String expected = expectedRecords.get(i).toString(); String actual = actualRecords.get(i).toString(); assertEquals(expected, actual); } expectedParser.close(); actualParser.close(); }
From source file:mtsar.resources.TaskResource.java
@POST @Consumes(MediaType.MULTIPART_FORM_DATA) public Response postTasksCSV(@Context UriInfo uriInfo, @FormDataParam("file") InputStream stream) throws IOException { try (final Reader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) { try (final CSVParser csv = new CSVParser(reader, TaskCSV.FORMAT)) { taskDAO.insert(TaskCSV.parse(stage, csv)); }//www. j a v a 2 s . c om } taskDAO.resetSequence(); return Response.seeOther(getTasksURI(uriInfo)).build(); }
From source file:com.xoriant.akka.mongodb.bulkimport.actor.FileReaderActor.java
private void readAndInsertCSV(String filePath) { FileReader fileReader = null; CSVParser csvFileParser = null;// ww w . j a v a 2 s . c o m // Create the CSVFormat object with the header mapping CSVFormat csvFileFormat = CSVFormat.EXCEL.withHeader(FILE_HEADER_MAPPING); try { fileReader = new FileReader(filePath); csvFileParser = new CSVParser(fileReader, csvFileFormat); List<CSVRecord> csvRecords = csvFileParser.getRecords(); CSVRecordBatchMsg csvRecordBatch = new CSVRecordBatchMsg(); boolean batchSent = false; // Skip the header row and start reading CSV records for (int i = 1; i < csvRecords.size(); i++) { CSVRecord record = csvRecords.get(i); BasicDBObject person = new BasicDBObject(); person.put(PERSON_GENDER, record.get(PERSON_GENDER)); person.put(PERSON_TITLE, record.get(PERSON_TITLE)); person.put(PERSON_NAMESET, record.get(PERSON_NAMESET)); person.put(PERSON_SURNAME, record.get(PERSON_SURNAME)); person.put(PERSON_CITY, record.get(PERSON_CITY)); person.put(PERSON_STATE, record.get(PERSON_STATE)); person.put(PERSON_ZIPCODE, record.get(PERSON_ZIPCODE)); csvRecordBatch.add(person); batchSent = false; if (i % batchSize == 0) { batchSentCounter++; csvRecordBatch.setBatchNo(batchSentCounter); mongoInsertionActor.tell(csvRecordBatch, getSelf()); csvRecordBatch = new CSVRecordBatchMsg(); batchSent = true; } } // Last batch maybe pending if there are less than batch size left over records. Sending last batch of such records explicitly if (!batchSent) { batchSentCounter++; csvRecordBatch.setBatchNo(batchSentCounter); mongoInsertionActor.tell(csvRecordBatch, getSelf()); } mongoInsertionActor.tell(new EndOfFileMsg(), getSelf()); System.out.println("FileReaderActor: EOF sent"); } catch (Exception e) { System.out.println("Error in CsvFileReader !!!" + e.getMessage()); } finally { try { fileReader.close(); csvFileParser.close(); } catch (IOException e) { System.out.println("Error while closing fileReader/csvFileParser : " + e.getMessage()); } } }
From source file:fi.vm.kapa.identification.proxy.utils.SessionHandlingUtils.java
@PostConstruct public void initSessionHandlingService() throws Exception { try {//from w w w . j a v a 2s.co m CSVParser csvParser = new CSVParser(new FileReader(new File(attributeMapFile)), CSVFormat.DEFAULT.withDelimiter(';').withCommentMarker('#')); CSVParser csvLegacyParser = new CSVParser(new FileReader(new File(legacyAttributeMapFile)), CSVFormat.DEFAULT.withDelimiter(';').withCommentMarker('#')); /* The attribute mapper files have the following syntax: * [SP-attribute-key];[External-attribute-mapper-key] */ csvParser.forEach(record -> attributeMap.put(record.get(0), record.get(1))); csvLegacyParser.forEach(record -> legacyAttributeMap.put(record.get(0), record.get(1))); } catch (Exception e) { logger.error("Error initializing CSV parser", e); } }
From source file:com.appspot.relaxe.tools.CSVInsertTask.java
public void run(Connection connection, Reader input, Table table) throws IOException, SQLException { if (connection == null) { throw new NullPointerException("'connection' must not be null"); }/*from w w w . ja v a 2s. com*/ if (input == null) { throw new NullPointerException("'input' must not be null"); } if (table == null) { throw new NullPointerException("'table' must not be null"); } boolean committed = false; try { connection.setAutoCommit(false); CSVStrategy cs = new CSVStrategy('\t', '"', CSVStrategy.COMMENTS_DISABLED, false, false, false); CSVParser p = new CSVParser(input, cs); // get header line String[] line = p.getLine(); // configure by using the column headers: ColumnMap cm = table.getColumnMap(); List<Identifier> names = new ArrayList<Identifier>(); List<Column> columnList = new ArrayList<Column>(); for (String n : line) { Column column = cm.get(n); if (column == null) { throw new IllegalArgumentException("column not found " + n); } columnList.add(column); names.add(column.getColumnName()); } if (names.isEmpty()) { throw new IllegalStateException("no column names available"); } ElementList<Identifier> nel = ElementList.newElementList(names); final int expectedColumnCount = line.length; // int recno = 0; PreparedStatement ps = null; InsertStatement ins = null; VarcharParameter[] params = new VarcharParameter[expectedColumnCount]; ValueAssignerFactory vaf = getImplementation().getValueAssignerFactory(); AssignmentVisitor pa = null; while ((line = p.getLine()) != null) { // recno++; final int cols = line.length; int lineno = p.getLineNumber(); if (cols != expectedColumnCount) { throw new IllegalStateException("unexpected column count: " + cols + " at line " + lineno); } if (ps == null) { List<RowValueConstructorElement> vl = new ArrayList<RowValueConstructorElement>(params.length); for (int i = 0; i < params.length; i++) { Column column = columnList.get(i); VarcharHolder h = parse(column, line[i]); VarcharParameter param = new VarcharParameter(column, h); params[i] = param; vl.add(param); } RowValueConstructor rvc = AbstractRowValueConstructor.of(vl); ins = new InsertStatement(table, nel, rvc); String q = ins.generate(); ps = connection.prepareStatement(q); pa = new AssignmentVisitor(vaf, ps); // System.err.println("lineno: " + lineno); // System.err.println("record: " + recno); // System.err.println("query: " + q); } else { pa.reset(); for (int i = 0; i < line.length; i++) { Column column = columnList.get(i); VarcharHolder h = parse(column, line[i]); VarcharParameter param = params[i]; param.setValue(h); } } ins.traverse(null, pa); ps.addBatch(); } int[] updateCounts = ps.executeBatch(); updated(updateCounts); connection.commit(); committed = true; } finally { if (!(committed)) { QueryHelper.doRollback(connection); } } }
From source file:com.itemanalysis.jmetrik.data.JmetrikFileImporterTest.java
@Test public void readJmetrikFileTest() { System.out.println("JmetrikFileImporterTest: Reading *.jmetrik file"); CSVParser parser = null;/*from ww w.jav a 2 s . com*/ Reader reader = null; try { File dataFile = FileUtils.toFile(this.getClass().getResource("/data/example-import-file.jmetrik")); reader = new InputStreamReader(new BOMInputStream(new FileInputStream(dataFile)), "UTF-8"); parser = new CSVParser(reader, CSVFormat.DEFAULT.withCommentMarker('#')); Iterator<CSVRecord> iter = parser.iterator(); CSVRecord temp = null; boolean readAttributes = false; boolean readData = false; int attCount = 0; while (iter.hasNext()) { temp = iter.next(); if ("VERSION".equals(temp.getComment())) { System.out.println("VERSION: " + temp.get(0)); } else if ("METADATA".equals(temp.getComment())) { System.out.println("CASES: " + temp.get(0)); } else if ("ATTRIBUTES".equals(temp.getComment())) { readAttributes = true; } else if ("DATA".equals(temp.getComment())) { readAttributes = false; readData = true; } if (readAttributes) { System.out.print("ATTRIBUTE-" + attCount + ": "); Iterator<String> innerIter = temp.iterator(); while (innerIter.hasNext()) { System.out.print(innerIter.next()); if (innerIter.hasNext()) { System.out.print(","); } } System.out.println(); attCount++; } if (readData) { Iterator<String> innerIter = temp.iterator(); while (innerIter.hasNext()) { System.out.print(innerIter.next()); if (innerIter.hasNext()) { System.out.print(","); } } System.out.println(); } } } catch (IOException ex) { ex.printStackTrace(); } finally { try { parser.close(); reader.close(); } catch (IOException ex) { ex.printStackTrace(); } } }
From source file:com.publictransitanalytics.scoregenerator.datalayer.directories.GTFSReadingServiceTypeCalendar.java
private void parseCalendarFile(final Reader calendarReader, final Multimap<LocalDate, String> serviceTypesMap) throws IOException { final CSVParser calendarParser = new CSVParser(calendarReader, CSVFormat.DEFAULT.withHeader()); final List<CSVRecord> calendarRecords = calendarParser.getRecords(); LocalDate earliestDate = null; LocalDate latestDate = null;/*from www .j av a2 s. co m*/ for (final CSVRecord record : calendarRecords) { final String serviceType = record.get("service_id"); final LocalDate start = LocalDate.parse(record.get("start_date"), DateTimeFormatter.BASIC_ISO_DATE); if (earliestDate == null || start.isBefore(earliestDate)) { earliestDate = start; } final LocalDate end = LocalDate.parse(record.get("end_date"), DateTimeFormatter.BASIC_ISO_DATE); if (latestDate == null || end.isAfter(latestDate)) { latestDate = end; } final EnumSet<DayOfWeek> daysOfWeek = EnumSet.noneOf(DayOfWeek.class); if (record.get("monday").equals("1")) { daysOfWeek.add(DayOfWeek.MONDAY); } if (record.get("tuesday").equals("1")) { daysOfWeek.add(DayOfWeek.TUESDAY); } if (record.get("wednesday").equals("1")) { daysOfWeek.add(DayOfWeek.WEDNESDAY); } if (record.get("thursday").equals("1")) { daysOfWeek.add(DayOfWeek.THURSDAY); } if (record.get("friday").equals("1")) { daysOfWeek.add(DayOfWeek.FRIDAY); } if (record.get("saturday").equals("1")) { daysOfWeek.add(DayOfWeek.SATURDAY); } if (record.get("sunday").equals("1")) { daysOfWeek.add(DayOfWeek.SUNDAY); } LocalDate targetDate = start; while (!targetDate.isAfter(end)) { if (daysOfWeek.contains(targetDate.getDayOfWeek())) { serviceTypesMap.put(targetDate, serviceType); } targetDate = targetDate.plusDays(1); } } }
From source file:br.edimarmanica.weir2.rule.filter.RulesFilter.java
public static List<String> loadFilteredRules(Site site) { List<String> remainingRules = new ArrayList<>(); try (Reader in = new FileReader(new File(Paths.PATH_WEIR_V2 + "/" + site.getPath() + "/filter.csv"))) { try (CSVParser parser = new CSVParser(in, CSVFormat.EXCEL.withHeader())) { for (CSVRecord record : parser) { //para cada value remainingRules.add(record.get("RULE")); }/*from ww w .j a v a2 s .c om*/ } } catch (FileNotFoundException ex) { Logger.getLogger(RulesDataTypeController.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(RulesDataTypeController.class.getName()).log(Level.SEVERE, null, ex); } return remainingRules; }