List of usage examples for org.apache.commons.csv CSVFormat DEFAULT
CSVFormat DEFAULT
To view the source code for org.apache.commons.csv CSVFormat DEFAULT.
Click Source Link
From source file:com.esri.geoevent.datastore.GeoEventDataStoreProxy.java
public GeoEventDataStoreProxy() { try (InputStream is = GeoEventDataStoreProxy.class.getResourceAsStream("/arcgisservers.properties")) { Properties props = new Properties(); props.load(is);//from w w w . ja va 2s. com StringReader csvServers = new StringReader(props.getProperty("servers", "")); Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(csvServers); ServerInfo currInfo; String currServerName = "<not initialized>"; String username, password; int port; Iterator<CSVRecord> iterator = records.iterator(); if (iterator.hasNext()) { CSVRecord record = iterator.next(); int size = record.size(); for (int i = 0; i < size; i++) { try { currInfo = new ServerInfo(); currServerName = record.get(i); currInfo.url = new URL(props.getProperty(currServerName + ".url", "")); port = (currInfo.url.getPort() == -1) ? getDefaultPortForScheme(currInfo.url.getProtocol()) : currInfo.url.getPort(); currInfo.authscope = new AuthScope(currInfo.url.getHost(), port); username = props.getProperty(currServerName + ".username", ""); password = props.getProperty(currServerName + ".password", ""); if (!StringUtils.isEmpty(username)) { username = username.replace('\\', '/'); String encryptedPassword = Crypto.doEncrypt(password); currInfo.credentials = new UsernameEncryptedPasswordCredentials(username, encryptedPassword); currInfo.ntCredentials = new NTCredentialsEncryptedPassword( username + ":" + encryptedPassword); } currInfo.httpContext = createContextForServer(currInfo); String tokenUrlKey = currServerName + ".tokenUrl"; String tokenUrl = props.getProperty(tokenUrlKey); if (tokenUrl != null) { currInfo.tokenUrl = new URL(tokenUrl); } username = props.getProperty(currServerName + ".gisTierUsername", ""); if (!StringUtils.isEmpty(username)) { password = props.getProperty(currServerName + ".gisTierPassword", ""); currInfo.gisTierUsername = username; currInfo.gisTierEncryptedPassword = Crypto.doEncrypt(password); } currInfo.name = currServerName; serverInfos.put(currServerName, currInfo); } catch (Throwable t) { LOG.log(Level.ALL, "Failed to parse properties for server " + currServerName, t); } } } } catch (Throwable t) { LOG.log(Level.SEVERE, "Unable to initialize. Will not be able to proxy any requests.", t); } }
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.publictransitanalytics.scoregenerator.datalayer.directories.GTFSReadingStopTimesDirectory.java
private void parseStopTimesFile(final ImmutableSetMultimap<String, FrequencyRecord> frequencyRecordMap, final Reader stopTimesReader) throws FileNotFoundException, IOException, InterruptedException { final CSVParser parser = new CSVParser(stopTimesReader, CSVFormat.DEFAULT.withHeader()); final SortedSetMultimap<String, RawTripStop> rawTripMap = TreeMultimap.create(Comparator.naturalOrder(), (stop1, stop2) -> Integer.compare(stop1.getSequence(), stop2.getSequence())); final Iterator<CSVRecord> stopTimesIter = parser.iterator(); while (stopTimesIter.hasNext()) { final CSVRecord record = stopTimesIter.next(); final String rawTripId = record.get("trip_id"); final int stopSequence = Integer.valueOf(record.get("stop_sequence")); final String stopId = record.get("stop_id"); final String arrivalTimeString = record.get("arrival_time"); final TransitTime arrivalTime = (arrivalTimeString == null) ? null : TransitTime.parse(arrivalTimeString); final String departureTimeString = record.get("departure_time"); final TransitTime departureTime = (departureTimeString == null) ? null : TransitTime.parse(arrivalTimeString); if (frequencyRecordMap.containsKey(rawTripId)) { final RawTripStop rawTripStop = new RawTripStop(arrivalTime, departureTime, stopId, rawTripId, stopSequence);/*from w w w . j av a 2 s . com*/ rawTripMap.put(rawTripId, rawTripStop); } else { final TripId tripId = new TripId(rawTripId); final TripStop tripStop = new TripStop(arrivalTime, stopId, tripId, stopSequence); try { final TripIdKey tripIdKey = new TripIdKey(rawTripId); tripsStore.put(tripIdKey, tripId); tripSequenceStore.put(new TripSequenceKey(tripIdKey, arrivalTime, stopSequence), tripStop); stopTimesStore.put(StopTimeKey.getWriteKey(stopId, arrivalTime), tripStop); } catch (final BitvantageStoreException e) { throw new ScoreGeneratorFatalException(e); } } } for (final String rawTripId : rawTripMap.keySet()) { final ImmutableSet<FrequencyRecord> frequencyRecords = frequencyRecordMap.get(rawTripId); for (final FrequencyRecord frequencyRecord : frequencyRecords) { TransitTime recurringTime = frequencyRecord.getStartTime(); while (recurringTime.isBefore(frequencyRecord.getEndTime())) { final TransitTime baseArrivalTime = rawTripMap.get(rawTripId).first().getArrivalTime(); final TripId tripId = new TripId(rawTripId, recurringTime.toString()); for (final RawTripStop rawTripStop : rawTripMap.get(rawTripId)) { final TransitTime arrivalTime = recurringTime .plus(TransitTime.durationBetween(baseArrivalTime, rawTripStop.getArrivalTime())); final int stopSequence = rawTripStop.getSequence(); final String stopId = rawTripStop.getStopId(); final TripStop tripStop = new TripStop(arrivalTime, stopId, tripId, stopSequence); final TripIdKey tripIdKey = new TripIdKey(tripId.getRawTripId(), tripId.getQualifier()); try { tripsStore.put(tripIdKey, tripId); tripSequenceStore.put(new TripSequenceKey(tripIdKey, arrivalTime, stopSequence), tripStop); stopTimesStore.put(StopTimeKey.getWriteKey(stopId, arrivalTime), tripStop); } catch (final BitvantageStoreException e) { throw new ScoreGeneratorFatalException(e); } } recurringTime = recurringTime.plus(frequencyRecord.getInterval()); } } } }
From source file:eu.fthevenet.binjr.data.codec.CsvDecoder.java
/** * Decodes data from the provided stream and invoke the provided {@link Consumer} for each decoded record. * * @param in the {@link InputStream} for the CSV file * @param headers a list of the headers to keep from decoded records * @param mapToResult the function to invoke for reach decoded record * @throws IOException in the event of an I/O error. * @throws DecodingDataFromAdapterException if an error occurred while decoding the CSV file. *//*from w w w .j a v a2s . c o m*/ public void decode(InputStream in, List<String> headers, Consumer<DataSample<T>> mapToResult) throws IOException, DecodingDataFromAdapterException { try (Profiler ignored = Profiler.start("Building time series from csv data", logger::trace)) { try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, encoding))) { CSVFormat csvFormat = CSVFormat.DEFAULT.withAllowMissingColumnNames(false).withFirstRecordAsHeader() .withSkipHeaderRecord().withDelimiter(delimiter); Iterable<CSVRecord> records = csvFormat.parse(reader); for (CSVRecord csvRecord : records) { ZonedDateTime timeStamp = dateParser.apply(csvRecord.get(0)); DataSample<T> tRecord = new DataSample<>(timeStamp); for (String h : headers) { tRecord.getCells().put(h, numberParser.apply(csvRecord.get(h))); } mapToResult.accept(tRecord); } } } }
From source file:com.itemanalysis.jmetrik.file.JmetrikFileReader.java
/** * Opens a connection by instantiating the reader and CSVParser. It extracts the * header into the VariableAttributeMap and moves the cursor to the first row of data. * /*from ww w . jav a2 s . c o m*/ * @throws IOException */ public void openConnection() throws IOException { //opens file and advances cursor to beginning of data dataReader = new InputStreamReader(new BOMInputStream(Files.newInputStream(f)), "UTF-8"); dataParser = new CSVParser(dataReader, CSVFormat.DEFAULT.withCommentMarker('#')); dataIterator = dataParser.iterator(); setVariableAttributes(dataIterator); //Get Column names String[] colNames = new String[variableAttributes.size()]; int index = 0; for (VariableName v : variableAttributes.keySet()) { colNames[index] = v.toString(); index++; } dataReader.close(); dataParser.close(); //Advance iterator to first data record (A little inefficient because loops over header a second time) //This inefficiency is because CSVReader only allows the header to be set in the constructor. dataReader = new InputStreamReader(new BOMInputStream(Files.newInputStream(f)), "UTF-8"); dataParser = new CSVParser(dataReader, CSVFormat.DEFAULT.withCommentMarker('#').withHeader(colNames)); dataIterator = dataParser.iterator(); advanceToFirstDataRecord(); }
From source file:com.raceup.fsae.test.TesterGui.java
/** * Parses data file, builds a Test/*ww w . j a v a 2 s . c o m*/ * @param pathToDataFile path to data csv file */ private void parseDataFileAndCreateTestOrFail(String pathToDataFile) { ArrayList<Question> questions = new ArrayList<>(); CSVRecord[] rows = null; try { CSVParser parser = CSVFormat.DEFAULT.parse(new FileReader(pathToDataFile)); rows = parser.getRecords().toArray(new CSVRecord[parser.getRecords().size()]); } catch (Exception e) { System.err.println(e.toString()); } for (CSVRecord row : rows) { // each row represent a question ArrayList<Answer> answers = new ArrayList<>(); // list of answers if (row.size() > 1) { for (int i = 1; i < row.size(); i++) { if (row.get(i).length() > 0) { answers.add(new Answer(row.get(i))); } } Answer correctAnswer = answers.get(0); // the correct // answer is always the first one String questionText = row.get(0); questions.add( new Question(questionText, answers.toArray(new Answer[answers.size()]), correctAnswer)); // add to list of questions } } test = new Test(questions.toArray(new Question[questions.size()])); }
From source file:de.tudarmstadt.ukp.experiments.argumentation.sequence.evaluation.helpers.FinalTableExtractor.java
public static <T> String tableToCsv(Table<String, String, T> table) throws IOException { StringWriter sw = new StringWriter(); CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT); List<String> firstRow = new ArrayList<>(); firstRow.add(" "); firstRow.addAll(table.columnKeySet()); printer.printRecord(firstRow);/*from w ww . j ava 2 s . c o m*/ for (String rowKey : table.rowKeySet()) { printer.print(rowKey); for (String columnKey : table.columnKeySet()) { printer.print(table.get(rowKey, columnKey)); } printer.println(); } printer.close(); return sw.toString(); }
From source file:com.me.jvmi.Main.java
public static Collection<InputRecord> parseInput(Path csv) throws IOException { Map<String, InputRecord> records = new HashMap<>(); CSVParser parser = CSVParser.parse(csv.toFile(), Charset.forName("UTF-8"), CSVFormat.DEFAULT.withHeader()); for (CSVRecord record : parser) { InputRecord input = new InputRecord(record); records.put(input.getId(), input); }//from w ww.j a v a 2 s . c o m for (InputRecord record : records.values()) { if (record.isPackage()) { for (String id : record.packageIds) { if (!records.containsKey(id)) { throw new IllegalStateException("Could not find product for package id: " + id); } record.addItem(records.get(id)); } } } return records.values(); }
From source file:com.streamsets.pipeline.lib.parser.delimited.TestDelimitedCharDataParser.java
@Test public void testParseWithHeader() throws Exception { OverrunReader reader = new OverrunReader(new StringReader("A,B\na,b"), 1000, true, false); DataParser parser = new DelimitedCharDataParser(getContext(), "id", reader, 0, 0, CSVFormat.DEFAULT, CsvHeader.WITH_HEADER, -1, CsvRecordType.LIST); Assert.assertEquals("4", parser.getOffset()); Record record = parser.parse();//from w ww. ja va2 s .com Assert.assertNotNull(record); Assert.assertEquals("id::4", record.getHeader().getSourceId()); Assert.assertEquals("a", record.get().getValueAsList().get(0).getValueAsMap().get("value").getValueAsString()); Assert.assertEquals("A", record.get().getValueAsList().get(0).getValueAsMap().get("header").getValueAsString()); Assert.assertEquals("b", record.get().getValueAsList().get(1).getValueAsMap().get("value").getValueAsString()); Assert.assertEquals("B", record.get().getValueAsList().get(1).getValueAsMap().get("header").getValueAsString()); Assert.assertEquals("7", parser.getOffset()); record = parser.parse(); Assert.assertNull(record); Assert.assertEquals("-1", parser.getOffset()); parser.close(); }
From source file:com.advdb.footballclub.FootBallClub.java
private void createPlayer(Session session) { Transaction transaction = null;//from w w w. ja v a2s. co m try { System.out.println("start createPlayer."); transaction = session.beginTransaction(); Reader in = new FileReader("/Users/apichart/Documents/DW_opponent/DimPlayer-Table 1.csv"); Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(in); for (CSVRecord record : records) { String name = record.get(2); String nationality = record.get(3); String value = record.get(4); String height = record.get(6); String weight = record.get(7); DimPlayer d = new DimPlayer(name, nationality, Double.valueOf(value), Double.valueOf(height), Double.valueOf(weight)); session.save(d); } in.close(); session.flush(); session.clear(); transaction.commit(); System.out.println("finish createPlayer."); } catch (HibernateException e) { if (transaction != null) { transaction.rollback(); } } catch (FileNotFoundException ex) { Logger.getLogger(FootBallClub.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(FootBallClub.class.getName()).log(Level.SEVERE, null, ex); } }