List of usage examples for org.apache.commons.csv CSVPrinter CSVPrinter
public CSVPrinter(final Appendable out, final CSVFormat format) throws IOException
From source file:com.amazonaws.services.dynamodbv2.online.index.integration.tests.ViolationCorrectionTest.java
/** * Iterates through detection output file: first leave updates blank based on missing updates per key. * Once it has reached the missing update number, it removes the expected gsi values as per the specified 'missingGsiExpectedHashValues'. * Note that once blank number is reached, it also starts adding updates. * It then iterates over the rows again and adds values for Yes/No/Invalid in the delete column. * It returns all error records, if present. If not, it returns all records. *//*w w w . ja v a2 s .c o m*/ private static List<List<String>> createCorrectionFile(final String detectionFile, final String correctionFile, final String gsiHashKeyName, final String gsiHashKeyType, final String gsiRangeKeyName, final String gsiRangeKeyType, final Map<String, String> tableHashToNewGsiHashValueMap, final Map<String, String> tableHashToNewGsiRangeValueMap, final int missingUpdatesPerKey, final int missingGsiExpectedHashValues, final int invalidValuesForDelete, final int numOfYesForDelete, final int numOfNoForDelete) throws IOException { List<List<String>> errorRecords = null; List<List<String>> allRecords = null; BufferedReader br = null; BufferedWriter bw = null; CSVParser parser = null; CSVPrinter csvPrinter = null; try { br = new BufferedReader(new FileReader(new File(detectionFile))); bw = new BufferedWriter(new FileWriter(new File(correctionFile))); parser = new CSVParser(br, TestUtils.csvFormat); csvPrinter = new CSVPrinter(bw, TestUtils.csvFormat); List<CSVRecord> detectorRecords = parser.getRecords(); int hashMissingUpdates = 0; int rangeMissingUpdates = 0; int missingGsiExpectedHashValuesCurrent = 0; // Print Header Map<String, Integer> header = parser.getHeaderMap(); csvPrinter.printRecord(header.keySet()); allRecords = new ArrayList<List<String>>(); for (CSVRecord csvRecord : detectorRecords) { List<String> newRecord = new ArrayList<String>(); String tableHashKeyRecorded = csvRecord.get(ViolationRecord.TABLE_HASH_KEY); String hashKeyViolationType = null; if (gsiHashKeyName != null) { hashKeyViolationType = csvRecord.get(ViolationRecord.GSI_HASH_KEY_VIOLATION_TYPE); } String rangeKeyViolationType = null; if (gsiRangeKeyName != null) { rangeKeyViolationType = csvRecord.get(ViolationRecord.GSI_RANGE_KEY_VIOLATION_TYPE); } for (int i = 0; i < csvRecord.size(); i++) { newRecord.add(i, csvRecord.get(i)); } String newGsiVal = null; if (hashKeyViolationType != null && (hashKeyViolationType.equals("Size Violation") || hashKeyViolationType.equals("Type Violation"))) { if (hashMissingUpdates < missingUpdatesPerKey) { allRecords.add(newRecord); hashMissingUpdates++; continue; } //Remove expected hash Values if (missingGsiExpectedHashValuesCurrent < missingGsiExpectedHashValues) { newRecord.remove((int) header.get(ViolationRecord.GSI_HASH_KEY)); newRecord.add(header.get(ViolationRecord.GSI_HASH_KEY), ""); missingGsiExpectedHashValuesCurrent++; } newRecord.remove((int) header.get(ViolationRecord.GSI_HASH_KEY_UPDATE_VALUE)); newGsiVal = getNewValue(gsiHashKeyType, 4 /*length*/); newRecord.add(header.get(ViolationRecord.GSI_HASH_KEY_UPDATE_VALUE), newGsiVal); tableHashToNewGsiHashValueMap.put(tableHashKeyRecorded, newGsiVal); } if (rangeKeyViolationType != null && (rangeKeyViolationType.equals("Size Violation") || rangeKeyViolationType.equals("Type Violation"))) { if (rangeMissingUpdates < missingUpdatesPerKey) { allRecords.add(newRecord); rangeMissingUpdates++; continue; } newRecord.remove(header.get(ViolationRecord.GSI_RANGE_KEY_UPDATE_VALUE)); newGsiVal = getNewValue(gsiRangeKeyType, 4 /*length*/); newRecord.add(header.get(ViolationRecord.GSI_RANGE_KEY_UPDATE_VALUE), newGsiVal); tableHashToNewGsiRangeValueMap.put(tableHashKeyRecorded, newGsiVal); } allRecords.add(newRecord); } // Add 'Y' or 'N' for delete column if (numOfNoForDelete > 0 || numOfYesForDelete > 0 || invalidValuesForDelete > 0) { errorRecords = new ArrayList<List<String>>(); int numOfYesAdded = 0; int numOfNoAdded = 0; int numOfInvalids = 0; for (List<String> record : allRecords) { if (numOfInvalids < invalidValuesForDelete) { record.remove(header.get(ViolationRecord.GSI_CORRECTION_DELETE_BLANK)); record.add(header.get(ViolationRecord.GSI_CORRECTION_DELETE_BLANK), "xx"); numOfInvalids++; errorRecords.add(record); continue; } if (numOfYesAdded < numOfYesForDelete) { record.remove(header.get(ViolationRecord.GSI_CORRECTION_DELETE_BLANK)); record.add(header.get(ViolationRecord.GSI_CORRECTION_DELETE_BLANK), "Y"); numOfYesAdded++; continue; } if (numOfNoAdded < numOfNoForDelete) { record.remove(header.get(ViolationRecord.GSI_CORRECTION_DELETE_BLANK)); record.add(header.get(ViolationRecord.GSI_CORRECTION_DELETE_BLANK), "N"); numOfNoAdded++; continue; } } } // Add all records to file csvPrinter.printRecords(allRecords); } finally { br.close(); bw.close(); parser.close(); csvPrinter.close(); } if (errorRecords != null) return errorRecords; else return allRecords; }
From source file:GUI.MainWindow.java
/** * Writes a row of data to a CSV file. For use with exporting CVE data * mainly./*from w w w . j a v a2 s . c o m*/ * * @param file * @param data * @param recordSeparator * @throws Exception */ public void writeCSVLine(File file, String[] data) throws Exception { FileWriter writer = new FileWriter(file, true); CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(System.getProperty("line.separator")); CSVPrinter csvFilePrinter = new CSVPrinter(writer, csvFileFormat); csvFilePrinter.printRecord(data); writer.flush(); writer.close(); csvFilePrinter.close(); }
From source file:nl.mpi.tg.eg.frinex.rest.CsvController.java
@RequestMapping(value = "/aggregate", method = RequestMethod.GET) @ResponseBody// w ww . java2s. c o m public void getAggregate(HttpServletResponse response) throws IOException, CsvExportException { response.setContentType("application/text"); response.addHeader("Content-Disposition", "attachment; filename=\"aggregate.csv\""); response.addHeader("Content-Transfer-Encoding", "text"); CSVPrinter printer = new CSVPrinter(response.getWriter(), CSVFormat.DEFAULT); final ParticipantCsvExporter participantCsvExporter = new ParticipantCsvExporter(); participantCsvExporter.appendAggregateCsvHeader(printer); ArrayList<String> insertedUserIds = new ArrayList<>(); for (Participant participant : participantRepository.findAllByOrderBySubmitDateDesc()) { if (!insertedUserIds.contains(participant.getUserId())) { // here we are relying on the last user data submission being the most complete because that data is only added to in the experiment GUI participantCsvExporter.appendAggregateCsvRow(printer, participant, tagRepository.findDistinctUserIdEventTagTagValueEventMsTageDateByUserIdOrderByTagDateAsc( participant.getUserId())); insertedUserIds.add(participant.getUserId()); } } printer.close(); // response.getOutputStream().flush(); }
From source file:nl.mpi.tg.eg.frinex.rest.CsvController.java
@RequestMapping(value = "/groupdatacsv", method = RequestMethod.GET) @ResponseBody/*from w ww. java 2s . c o m*/ public void getGroupData(HttpServletResponse response) throws IOException, CsvExportException { // response.setContentType("application/text"); // response.addHeader("Content-Disposition", "attachment; filename=\"groupdata.csv\""); // response.addHeader("Content-Transfer-Encoding", "text"); CSVPrinter printer = new CSVPrinter(response.getWriter(), CSVFormat.DEFAULT); List<String> headerList = new ArrayList(); headerList.add("Event Date"); headerList.add("Screen Name"); headerList.add("Group Name"); headerList.add("Member Codes"); headerList.add("Communication Channels"); headerList.add("Sender Code"); headerList.add("Respondent Code"); headerList.add("Index"); final StimuliTagExpander stimuliTagExpander = new StimuliTagExpander(); for (String columnTag : stimuliTagExpander.getTagColumns()) { headerList.add("Target-" + columnTag); } headerList.add("Target"); for (String columnTag : stimuliTagExpander.getTagColumns()) { headerList.add("Response-" + columnTag); } headerList.add("Response"); for (int distractorIndex : stimuliTagExpander.getDistractorColumns()) { for (String columnTag : stimuliTagExpander.getTagColumns()) { headerList.add("Distractor-" + (distractorIndex + 1) + "-" + columnTag); } headerList.add("Distractor-" + (distractorIndex + 1)); } headerList.add("Message"); headerList.add("ms"); printer.printRecord(headerList); for (GroupData groupData : groupDataRepository.findAll()) { List<Object> rowList = new ArrayList(); rowList.add(groupData.getEventDate()); rowList.add(groupData.getScreenName()); rowList.add(groupData.getGroupName()); rowList.add(groupData.getAllMemberCodes()); rowList.add(groupData.getGroupCommunicationChannels()); rowList.add(groupData.getSenderMemberCode()); rowList.add(groupData.getRespondentMemberCode()); rowList.add(groupData.getStimulusIndex()); for (String tagColumn : stimuliTagExpander.getTagColumns(groupData.getStimulusId(), ":")) { rowList.add(tagColumn); } rowList.add(groupData.getStimulusId()); for (String tagColumn : stimuliTagExpander.getTagColumns(groupData.getResponseStimulusId(), ":")) { rowList.add(tagColumn); } rowList.add(groupData.getResponseStimulusId()); for (String tagColumn : stimuliTagExpander.getDistractorTagColumns(groupData.getStimulusOptionIds(), ":")) { rowList.add(tagColumn); } rowList.add(groupData.getMessageString()); rowList.add(groupData.getEventMs()); printer.printRecord(rowList); } printer.close(); }
From source file:nl.mpi.tg.eg.frinex.rest.CsvController.java
private byte[] getParticipantsCsv() throws IOException { final StringBuilder stringBuilder = new StringBuilder(); CSVPrinter printer = new CSVPrinter(stringBuilder, CSVFormat.DEFAULT); final ParticipantCsvExporter participantCsvExporter = new ParticipantCsvExporter(); participantCsvExporter.appendCsvHeader(printer); ArrayList<String> insertedUserIds = new ArrayList<>(); for (Participant participant : participantRepository.findAllByOrderBySubmitDateDesc()) { if (!insertedUserIds.contains(participant.getUserId())) { // here we are relying on the last user data submission being the most complete because that data is only added to in the experiment GUI participantCsvExporter.appendCsvRow(printer, participant); insertedUserIds.add(participant.getUserId()); }//from www . ja v a2s . c om } printer.close(); return stringBuilder.toString().getBytes(); }
From source file:nl.mpi.tg.eg.frinex.rest.CsvController.java
private byte[] getScreenDataCsv() throws IOException { final StringBuilder stringBuilder = new StringBuilder(); CSVPrinter printer = new CSVPrinter(stringBuilder, CSVFormat.DEFAULT); printer.printRecord("UserId", "ScreenName", "ViewDate"); for (ScreenData screenData : screenDataRepository.findAllDistinctRecords()) { printer.printRecord(screenData.getUserId(), screenData.getScreenName(), screenData.getViewDate()); }//from ww w. j av a 2 s . c o m printer.close(); return stringBuilder.toString().getBytes(); }
From source file:nl.mpi.tg.eg.frinex.rest.CsvController.java
private byte[] getTimeStampDataCsv() throws IOException { final StringBuilder stringBuilder = new StringBuilder(); CSVPrinter printer = new CSVPrinter(stringBuilder, CSVFormat.DEFAULT); printer.printRecord("UserId", "EventTag", "EventMs", "TagDate"); for (TimeStamp timeStamp : timeStampRepository.findAllDistinctRecords()) { printer.printRecord(timeStamp.getUserId(), timeStamp.getEventTag(), timeStamp.getEventMs(), timeStamp.getTagDate()); }/*from ww w . ja va 2 s. c om*/ printer.close(); return stringBuilder.toString().getBytes(); }
From source file:nl.mpi.tg.eg.frinex.rest.CsvController.java
private byte[] getTagDataCsv() throws IOException { final StringBuilder stringBuilder = new StringBuilder(); CSVPrinter printer = new CSVPrinter(stringBuilder, CSVFormat.DEFAULT); printer.printRecord("UserId", "EventTag", "TagValue", "EventMs", "TagDate"); for (TagData tagData : tagRepository.findAllDistinctRecords()) { printer.printRecord(tagData.getUserId(), tagData.getEventTag(), tagData.getTagValue(), tagData.getEventMs(), tagData.getTagDate()); }// ww w .j ava 2 s . c o m printer.close(); return stringBuilder.toString().getBytes(); }
From source file:nl.mpi.tg.eg.frinex.rest.CsvController.java
private byte[] getTagPairDataCsv() throws IOException { final StringBuilder stringBuilder = new StringBuilder(); CSVPrinter printer = new CSVPrinter(stringBuilder, CSVFormat.DEFAULT); printer.printRecord("UserId", "EventTag", "TagValue1", "TagValue2", "EventMs", "TagDate"); for (TagPairData tagPairData : tagPairRepository.findAllDistinctRecords()) { printer.printRecord(tagPairData.getUserId(), tagPairData.getEventTag(), tagPairData.getTagValue1(), tagPairData.getTagValue2(), tagPairData.getEventMs(), tagPairData.getTagDate()); }/*from w ww . j a v a 2 s .c o m*/ printer.close(); return stringBuilder.toString().getBytes(); }
From source file:norbert.mynemo.dataimport.fileformat.MynemoRating.java
/** * Creates a printer where a rating can be printed. The given file must not exist. * * @param filepath the filepath where the printer will write the data. * @return a new printer//ww w . j ava 2 s .c om */ public static CSVPrinter createPrinter(String filepath) throws IOException { checkArgument(filepath != null, "The filepath must be not null."); checkArgument(!new File(filepath).exists(), "The file must not exist."); return new CSVPrinter(new BufferedWriter(new FileWriter(filepath)), CSV_FORMAT); }