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:fi.vm.kapa.identification.proxy.utils.SessionHandlingUtils.java
@PostConstruct public void initSessionHandlingService() throws Exception { try {/*from w w w. ja va 2 s .c o 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.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 w w w .j av a2 s . c o 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:edu.emory.mathcs.nlp.zzz.CSVSentiment.java
public void toTXT(String inputFile) throws Exception { CSVParser parser = new CSVParser(IOUtils.createBufferedReader(inputFile), CSVFormat.DEFAULT); PrintStream fout = IOUtils.createBufferedPrintStream(inputFile + ".txt"); List<CSVRecord> records = parser.getRecords(); CSVRecord record;/*from ww w.j av a 2 s. c o m*/ System.out.println(inputFile); for (int i = 0; i < records.size(); i++) { if (i == 0) continue; record = records.get(i); fout.println(record.get(6)); } fout.close(); parser.close(); }
From source file:com.ibm.watson.developer_cloud.professor_languo.pipeline.evaluation.ResultWriter.java
@Override public void initialize(Properties properties) { // Retrieve necessary properties String resultsFilePath = properties.getProperty(ConfigurationConstants.PIPELINE_RESULTS_TSV_FILE_PATH); this.format = PipelineResultsTsvFileFormats .valueOf(properties.getProperty(ConfigurationConstants.PIPELINE_RESULTS_TSV_FILE_FORMAT, PipelineResultsTsvFileFormats.DEFAULT.toString())); // Make sure file path was actually specified if (resultsFilePath == null) throw new RuntimeException(MessageFormat.format(Messages.getString("RetrieveAndRank.MISSING_PROPERTY"), //$NON-NLS-1$ ConfigurationConstants.PIPELINE_RESULTS_TSV_FILE_FORMAT)); // Open a FileWriter, using CSV or TSV format depending on desired // output format try {/*from w w w . j a v a 2 s . co m*/ writer = (this.format == PipelineResultsTsvFileFormats.COMPETITION) ? new CSVPrinter(new FileWriter(resultsFilePath), CSVFormat.DEFAULT) : new CSVPrinter(new FileWriter(resultsFilePath), CSVFormat.TDF.withHeader(getHeaders(this.format))); } catch (IOException e) { throw new RuntimeException(new PipelineException(e)); } }
From source file:dk.dma.ais.packet.AisPacketCSVOutputSink.java
@Override public void process(OutputStream stream, AisPacket packet, long count) throws IOException { csvLock.lock();/*from w ww.j av a 2s . co m*/ try { if (csv == null) { csv = new CSVPrinter(new OutputStreamWriter(stream), CSVFormat.DEFAULT.withHeader(columns)); } AisMessage m = packet.tryGetAisMessage(); if (m != null) { List fields = Lists.newArrayList(); for (String column : columns) { switch (column) { case "timestamp": addTimestamp(fields, packet); break; case "timestamp_pretty": addTimestampPretty(fields, packet); break; case "msgid": addMsgId(fields, m); break; case "mmsi": addMmsi(fields, m); break; case "posacc": addPositionAccuracy(fields, m); break; case "lat": addLatitude(fields, m); break; case "lon": addLongitude(fields, m); break; case "targetType": addTargetType(fields, m); break; case "sog": addSog(fields, m); break; case "cog": addCog(fields, m); break; case "trueHeading": addTrueHeading(fields, m); break; case "draught": addDraught(fields, m); break; case "name": addName(fields, m); break; case "dimBow": addDimBow(fields, m); break; case "dimPort": addDimPort(fields, m); break; case "dimStarboard": addDimStarboard(fields, m); break; case "dimStern": addDimStern(fields, m); break; case "shipTypeCargoTypeCode": addShipTypeCargoTypeCode(fields, m); break; case "shipType": addShipType(fields, m); break; case "shipCargo": addShipCargo(fields, m); break; case "callsign": addCallsign(fields, m); break; case "imo": addImo(fields, m); break; case "destination": addDestination(fields, m); break; case "eta": addEta(fields, m); break; default: LOG.warn("Unknown column: " + column); fields.add(NULL_INDICATOR); } } csv.printRecord(fields); } } finally { csvLock.unlock(); } }
From source file:com.adobe.acs.commons.exporters.impl.users.UserExportServletTest.java
@Test public void testWithGroupDirectFilter() throws Exception { // Build parameters JsonObject params = buildParameterObject("direct", "users"); Map<String, Object> parameters = new HashMap<>(); parameters.put("params", params); context.request().setParameterMap(parameters); servlet.doGet(context.request(), context.response()); assertEquals(context.response().getStatus(), 200); String output = context.response().getOutputAsString(); CSVParser parser = CSVParser.parse(output, CSVFormat.DEFAULT.withHeader()); assertAllUsersPresent(parser.getRecords(), "alice", "bob"); }
From source file:com.streamsets.pipeline.lib.parser.delimited.TestDelimitedCharDataParser.java
@Test public void testParseNoHeaderWithListMap() 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.NO_HEADER, -1, CsvRecordType.LIST_MAP); Assert.assertEquals("0", parser.getOffset()); Record record = parser.parse();//from w ww . j a v a 2 s . c om Assert.assertNotNull(record); Assert.assertEquals("id::0", record.getHeader().getSourceId()); Assert.assertEquals("A", record.get().getValueAsListMap().get("0").getValueAsString()); Assert.assertEquals("B", record.get().getValueAsListMap().get("1").getValueAsString()); Assert.assertEquals("4", parser.getOffset()); record = parser.parse(); Assert.assertNotNull(record); Assert.assertEquals("id::4", record.getHeader().getSourceId()); Assert.assertEquals("a", record.get().getValueAsListMap().get("0").getValueAsString()); Assert.assertEquals("b", record.get().getValueAsListMap().get("1").getValueAsString()); Assert.assertEquals("7", parser.getOffset()); record = parser.parse(); Assert.assertNull(record); Assert.assertEquals("-1", parser.getOffset()); parser.close(); }
From source file:com.github.woonsan.jackrabbit.migration.datastore.batch.MigrationJobExecutionReporter.java
public void printJobExecutionSummary(PrintWriter out) { int totalCount = executionStates.getRecordCount(); int processedRecordCount = 0; int readSuccessCount = 0; int writeSuccessCount = 0; DataIdentifier identifier;/*from ww w . java2 s. c o m*/ MigrationRecordExecutionStates recordStates; for (Map.Entry<DataIdentifier, MigrationRecordExecutionStates> entry : executionStates .getExecutionStatesMap().entrySet()) { ++processedRecordCount; identifier = entry.getKey(); recordStates = entry.getValue(); if (recordStates.isReadSucceeded()) { ++readSuccessCount; } if (recordStates.isWriteSucceeded()) { ++writeSuccessCount; } } out.println( "==============================================================================================================="); out.println("Execution Summary:"); out.println( "---------------------------------------------------------------------------------------------------------------"); out.printf( "Total: %d, Processed: %d, Read Success: %d, Read Fail: %d, Write Success: %d, Write Fail: %d, Duration: %dms", totalCount, processedRecordCount, readSuccessCount, (processedRecordCount - readSuccessCount), writeSuccessCount, (processedRecordCount - writeSuccessCount), executionStates.getStoppedTimeMillis() - executionStates.getStartedTimeMillis()); out.println(); out.println( "---------------------------------------------------------------------------------------------------------------"); out.println("Details (in CSV format):"); out.println( "---------------------------------------------------------------------------------------------------------------"); CSVPrinter csvPrinter = null; try { csvPrinter = CSVFormat.DEFAULT.withHeader("SEQ", "ID", "READ", "WRITE", "SIZE", "ERROR").print(out); int seq = 0; for (Map.Entry<DataIdentifier, MigrationRecordExecutionStates> entry : executionStates .getExecutionStatesMap().entrySet()) { identifier = entry.getKey(); recordStates = entry.getValue(); csvPrinter.printRecord(++seq, identifier, recordStates.isReadSucceeded(), recordStates.isWriteSucceeded(), recordStates.getWriteSize(), StringUtils.isNotEmpty(recordStates.getReadError()) ? recordStates.getReadError() : recordStates.getWriteError()); } } catch (IOException e) { log.error("Error while printing result in CSV."); } out.println( "==============================================================================================================="); }
From source file:eu.fthevenet.binjr.data.codec.CsvDecoder.java
/** * Returns the columns headers of the CSV file. * * @param in an input stream for the CSV file. * @return the columns headers of the CSV file. * @throws IOException in the event of an I/O error. * @throws DecodingDataFromAdapterException if an error occurred while decoding the CSV file. *//*from w ww .j av a2 s . c o m*/ public List<String> getDataColumnHeaders(InputStream in) throws IOException, DecodingDataFromAdapterException { try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, encoding))) { CSVFormat csvFormat = CSVFormat.DEFAULT.withAllowMissingColumnNames(false).withDelimiter(delimiter); Iterable<CSVRecord> records = csvFormat.parse(reader); return this.parseColumnHeaders(records.iterator().next()); } }
From source file:com.ge.research.semtk.load.DataCleaner.java
/** * Constructor that takes the cleaning spec as JSON * @param dataset the dataset to clean//w ww . j av a 2s . co m * @param cleanedFilePathStr the file to write cleaned data to * @param cleanSpecJson the cleaning spec in JSON format, e.g. {"LOWERCASE":["child_names","has_pool"],"SPLIT":{"pet_names":"##","child_names":"~"},"REMOVE_NULLS":"true"} */ public DataCleaner(Dataset dataset, String cleanedFilePathStr, JSONObject cleanSpecJson) throws Exception { this.dataset = dataset; this.headers = dataset.getColumnNamesinOrder(); this.writer = new BufferedWriter(new FileWriter(cleanedFilePathStr)); this.csvPrinter = new CSVPrinter(this.writer, CSVFormat.DEFAULT); // add the specs for cleaning parseCleanSpecJson(cleanSpecJson); // write the headers this.csvPrinter.printRecord(this.headers); }