private static String[] doParseDelimited(String delimited, CSVFormat format) { try (CSVParser parser = CSVParser.parse(delimited, format)) { Iterator<CSVRecord> records = parser.iterator(); return records.hasNext() ? StreamSupport.stream(records.next().spliterator(), false).toArray(String[]::new) : EMPTY_STRING; } catch (IOException e) { throw new IllegalStateException(e); // Can't happen } }
private void init() throws IOException { _parser = _format.parse(RecordReaderUtils.getFileReader(_dataFile)); _iterator = _parser.iterator(); }
Iterator<CSVRecord> iterator = csvParser.iterator();
Iterator<CSVRecord> iter = parsed.iterator(); ourLog.debug("Header map: {}", parsed.getHeaderMap());
private CsvIterator(CSVParser parser, CsvRecordConverter<T> converter) { this.parser = parser; this.iterator = parser.iterator(); this.converter = converter; }
@Test public void testParseCustomNullValues() throws IOException { final StringWriter sw = new StringWriter(); final CSVFormat format = CSVFormat.DEFAULT.withNullString("NULL"); try (final CSVPrinter printer = new CSVPrinter(sw, format)) { printer.printRecord("a", null, "b"); } final String csvString = sw.toString(); assertEquals("a,NULL,b" + recordSeparator, csvString); try (final CSVParser iterable = format.parse(new StringReader(csvString))) { final Iterator<CSVRecord> iterator = iterable.iterator(); final CSVRecord record = iterator.next(); assertEquals("a", record.get(0)); assertEquals(null, record.get(1)); assertEquals("b", record.get(2)); assertFalse(iterator.hasNext()); } }
final CSVFormat format = CSVFormat.DEFAULT .withIgnoreEmptyLines(true) .withDelimiter(','); CSVParser parser = CSVParser.parse(file, Charset.forName("UTF-8"), format); Iterator<CSVRecord> iterator = parser.iterator();
/** * Checks, if another record is available for the parser. * * @return true, if another record is available, otherwise false */ public final boolean hasNext() { if (this.iterator == null) { this.iterator = this.parser.iterator(); } return this.iterator.hasNext(); }
metadata, tikaConfig.getEncodingDetector()); CSVParser csvParser = new CSVParser(reader, CSVFormat.TDF)) { Iterator<CSVRecord> iterator = csvParser.iterator();
xhtml.startElement("table"); Iterator<CSVRecord> iterator = csvParser.iterator();
public final Record next() { if (this.iterator == null) { this.iterator = this.parser.iterator(); } return this.newRecord(this.iterator.next()); } }
/** * Constructs composite value with specified initial child values. * * @param parserSettings Settings for the CSV parser. */ public CsvReaderValue(CSVParserSettings parserSettings) { if (parserSettings == null) { throw new IllegalArgumentException("parserSettings cannot be null."); } this.evaluatedValues = new HashMap<>(); this.csvParser = createCSVParser(parserSettings); this.iterator = csvParser.iterator(); }
private static String[] doParseDelimited(String delimited, CSVFormat format) { try (CSVParser parser = CSVParser.parse(delimited, format)) { Iterator<CSVRecord> records = parser.iterator(); return records.hasNext() ? StreamSupport.stream(records.next().spliterator(), false).toArray(String[]::new) : EMPTY_STRING; } catch (IOException e) { throw new IllegalStateException(e); // Can't happen } }
private CSVParser(Reader input) { try { this.parser = CSVFormat.RFC4180.parse(input); this.iterator = parser.iterator() ; } catch (IOException e) { throw new CSVParseException("Failed to create the CSV parser: " + e.getMessage(), e); } }
private PeekingIterator<CSVRecord> getInternalCSVIter(final CSVParser parser) { final Iterator<CSVRecord> csvIter = parser.iterator(); if (atMost.isPresent()) { final int headerRows = hasHeader ? 1 : 0; final Iterator<CSVRecord> limitedIterator = Iterators.limit(csvIter, atMost.get() + headerRows + discardRows); return Iterators.peekingIterator(limitedIterator); } return Iterators.peekingIterator(csvIter); }
@Override public Map<String, Object> next() throws IOException { if (csvParser == null) { csvParser = CSVFormat.DEFAULT.withHeader().withDelimiter(delimiter).parse(inputStream); iterator = csvParser.iterator(); } if (!iterator.hasNext()) { return null; } CSVRecord csvRecord = iterator.next(); return new HashMap<String, Object>(csvRecord.toMap()); }
public Object unmarshal(Exchange exchange, InputStream inputStream) throws IOException { CSVParser parser = new CSVParser(new InputStreamReader(inputStream, IOHelper.getCharsetName(exchange)), format); try { return asList(parser.iterator(), converter); } finally { IOHelper.close(parser); } }
@Override public Map<String, String> next() throws IOException { if (csvParser == null) { csvParser = CSVFormat.DEFAULT.withHeader().withDelimiter(delimiter).parse(inputStream); iterator = csvParser.iterator(); } if (!iterator.hasNext()) { return null; } CSVRecord csvRecord = iterator.next(); return csvRecord.toMap(); }
public void loadFile(String fileName, Map<String, Object> propertiesMap, WorkerPool workers) throws Exception { log.info("Loading " + fileName); // long linesCount = BatchHelper.countLines(fileName); this.propertiesMap = propertiesMap; Reader in = new FileReader(fileName); Iterator<CSVRecord> iter = CSVFormat.EXCEL.withHeader().parse(in).iterator(); // long freeMemory = Runtime.getRuntime().freeMemory()/1024/1024; // TODO Calculate targetThreadCount using the free memory and number of threads // to execute // Max record count per thread startWorkers(iter, Config.getConfig().getWorkersTargetRecordCount(), workers); } }
@Override public void open(final Serializable checkpoint) throws Exception { final CSVFormat csvFormat = newFormat(); parser = csvFormat.parse(newReader()); iterator = new IteratorReader<CSVRecord>(parser.iterator()); mapperInstance = mapper == null ? new BeanLocator.LocatorInstance<CsvReaderMapper>( mapping != null ? new DefaultMapper(Thread.currentThread().getContextClassLoader().loadClass(mapping)) : NOOP_MAPPER, null) : BeanLocator.Finder.get(locator).newInstance(CsvReaderMapper.class, mapper); super.open(checkpoint); }