Java Code Examples for org.apache.commons.csv.CSVFormat#DEFAULT
The following examples show how to use
org.apache.commons.csv.CSVFormat#DEFAULT .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DataExportController.java From MyBox with Apache License 2.0 | 8 votes |
protected boolean writeInternalCSV(File file, String sql) { try ( Connection conn = DriverManager.getConnection(protocol + dbHome() + login); CSVPrinter printer = new CSVPrinter(new FileWriter(file, Charset.forName("utf-8")), CSVFormat.DEFAULT)) { String filename = file.getAbsolutePath(); conn.setReadOnly(true); writeInternalCSVHeader(printer); int count = 0; try ( ResultSet results = conn.createStatement().executeQuery(sql)) { while (results.next()) { if (cancelled) { updateLogs(message("Cancelled") + " " + filename); return false; } writeInternalCSV(conn, printer, results); count++; if (verboseCheck.isSelected() && (count % 50 == 0)) { updateLogs(message("Exported") + " " + count + ": " + filename); } } } return true; } catch (Exception e) { updateLogs(e.toString()); return false; } }
Example 2
Source File: CoordinateUtils.java From TomboloDigitalConnector with MIT License | 6 votes |
public static Map<String, LatLong> postcodeToLatLong(String datasetProvider, DownloadUtils downloadUtils) throws Exception { Map<String, LatLong> postcodeToCoordMap = new HashMap<>(); InputStreamReader postcodeIsr = new InputStreamReader(downloadUtils.fetchInputStream(new URL(POSTCODE_TO_COORDINATE_URL), datasetProvider, ".csv")); CSVParser csvFileParser = new CSVParser(postcodeIsr, CSVFormat.DEFAULT); Iterator<CSVRecord> iter = csvFileParser.getRecords().iterator(); CSVRecord header = iter.next(); while (iter.hasNext()) { CSVRecord record = iter.next(); postcodeToCoordMap.put(record.get(1), new LatLong(record.get(2), record.get(3))); } return postcodeToCoordMap; }
Example 3
Source File: FrameworkUtils.java From data-polygamy with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * String Parsing */ public static String[] splitStr(String val, Integer len) throws IOException { String[] input; try { CSVParser parser = new CSVParser(new StringReader(val), CSVFormat.DEFAULT); CSVRecord record = parser.getRecords().get(0); input = new String[len]; Iterator<String> valuesIt = record.iterator(); int i = 0; while (valuesIt.hasNext()) { input[i] = valuesIt.next().trim(); i++; } parser.close(); } catch (ArrayIndexOutOfBoundsException e) { input = val.split(",", len); for (int i = 0; i < input.length; i++) input[i] = input[i].trim(); } return input; }
Example 4
Source File: CSVQueryOutputFormat.java From geowave with Apache License 2.0 | 6 votes |
@Override public void output(final ResultSet results) { try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outputFile), StringUtils.getGeoWaveCharset())) { try (CSVPrinter printer = new CSVPrinter(writer, CSVFormat.DEFAULT)) { final String[] header = new String[results.columnCount()]; for (int i = 0; i < results.columnCount(); i++) { header[i] = results.columnName(i); } printer.printRecord((Object[]) header); while (results.hasNext()) { final Result result = results.next(); final Object[] values = new Object[results.columnCount()]; for (int i = 0; i < results.columnCount(); i++) { values[i] = result.columnValue(i); } printer.printRecord(values); } } } catch (IOException e) { throw new RuntimeException("Error writing CSV: " + e.getMessage(), e); } }
Example 5
Source File: CSVUtils.java From nifi with Apache License 2.0 | 6 votes |
public static CSVFormat createCSVFormat(final PropertyContext context, final Map<String, String> variables) { final String formatName = context.getProperty(CSV_FORMAT).getValue(); if (formatName.equalsIgnoreCase(CUSTOM.getValue())) { return buildCustomFormat(context, variables); } if (formatName.equalsIgnoreCase(RFC_4180.getValue())) { return CSVFormat.RFC4180; } else if (formatName.equalsIgnoreCase(EXCEL.getValue())) { return CSVFormat.EXCEL; } else if (formatName.equalsIgnoreCase(TDF.getValue())) { return CSVFormat.TDF; } else if (formatName.equalsIgnoreCase(MYSQL.getValue())) { return CSVFormat.MYSQL; } else if (formatName.equalsIgnoreCase(INFORMIX_UNLOAD.getValue())) { return CSVFormat.INFORMIX_UNLOAD; } else if (formatName.equalsIgnoreCase(INFORMIX_UNLOAD_CSV.getValue())) { return CSVFormat.INFORMIX_UNLOAD_CSV; } else { return CSVFormat.DEFAULT; } }
Example 6
Source File: EpidemicReport.java From MyBox with Apache License 2.0 | 5 votes |
public static void writeInternalCSV(File file, List<EpidemicReport> reports) { try ( CSVPrinter printer = new CSVPrinter(new FileWriter(file, Charset.forName("utf-8")), CSVFormat.DEFAULT)) { writeInternalCSVHeader(printer); for (EpidemicReport report : reports) { writeInternalCSV(printer, report); } } catch (Exception e) { logger.debug(e.toString()); } }
Example 7
Source File: GPCsvExportTest.java From ganttproject with GNU General Public License v3.0 | 5 votes |
public void testResourceAssignments() throws Exception { HumanResourceManager hrManager = new HumanResourceManager(null, new CustomColumnsManager()); TaskManager taskManager = getTaskManager(); CSVOptions csvOptions = enableOnly(TaskDefaultColumn.ID.getStub().getID(), TaskDefaultColumn.RESOURCES.getStub().getID()); Task task1 = createTask(); Task task2 = createTask(); Task task3 = createTask(); HumanResource alice = hrManager.create("Alice", 1); HumanResource bob = hrManager.create("Bob", 2); task1.getAssignmentCollection().addAssignment(alice).setLoad(100f); task2.getAssignmentCollection().addAssignment(alice).setLoad(45.457f); task2.getAssignmentCollection().addAssignment(bob); GanttCSVExport exporter = new GanttCSVExport(taskManager, hrManager, new RoleManagerImpl(), csvOptions); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try (SpreadsheetWriter writer = new CsvWriterImpl(outputStream, CSVFormat.DEFAULT)) { exporter.save(writer); } String[] lines = new String(outputStream.toByteArray(), Charsets.UTF_8.name()).split("\\n"); assertEquals(9, lines.length); assertEquals("tableColID,resources,Assignments", lines[0].trim()); assertEquals("0,Alice,1:100.00", lines[1].trim()); assertEquals("1,Alice;Bob,1:45.46;2:0.00", lines[2].trim()); assertEquals("2,,", lines[3].trim()); }
Example 8
Source File: CQL2CSV.java From cqlkit with Apache License 2.0 | 5 votes |
@Override protected void head(ColumnDefinitions columnDefinitions, PrintStream out) { definitions = columnDefinitions.asList().toArray(new ColumnDefinitions.Definition[]{}); csvFormat = CSVFormat.DEFAULT; // Print the header if (!commandLine.hasOption("H")) { List<String> list = columnDefinitions.asList().stream() .map(col -> col.getName()) .collect(Collectors.toList()); if (commandLine.hasOption("l")) { list.add(0, "linenumber"); } if (commandLine.hasOption("queryKeys")) { list.add("tokenPercentage"); } try { CSVPrinter print = csvFormat .withHeader(list.toArray(new String[]{})) .print(out); print.flush(); } catch (IOException e) { throw new RuntimeException(e); } } }
Example 9
Source File: GeographyCode.java From MyBox with Apache License 2.0 | 5 votes |
public static void writeInternalCSV(File file, List<GeographyCode> codes, boolean writeHeader) { try ( CSVPrinter printer = new CSVPrinter(new FileWriter(file, Charset.forName("utf-8")), CSVFormat.DEFAULT)) { if (writeHeader) { writeInternalCSVHeader(printer); } for (GeographyCode code : codes) { writeInternalCSV(printer, code); } } catch (Exception e) { logger.debug(e.toString()); } }
Example 10
Source File: FrameworkUtils.java From data-polygamy with BSD 3-Clause "New" or "Revised" License | 5 votes |
public static String[] splitStr(String val) throws IOException { CSVParser parser = new CSVParser(new StringReader(val), CSVFormat.DEFAULT); CSVRecord record = parser.getRecords().get(0); Iterator<String> valuesIt = record.iterator(); String[] input = new String[record.size()]; int i = 0; while (valuesIt.hasNext()) { input[i] = valuesIt.next(); i++; } parser.close(); return input; }
Example 11
Source File: CommandLineInterface.java From utah-parser with Apache License 2.0 | 5 votes |
/** * Creates a list of headers, prints the headers, then prints the records for each header. * If a record doesn't exist for a header, a blank space is put in its place * * @param parser * @param target * @throws IOException */ void printToCSV(Parser parser, PrintStream target) throws IOException { Map<String, String> curr = parser.next(); List<Map<String, String>> mapList = new ArrayList<Map<String, String>>(); Set<String> CSV_HEADERS = new TreeSet<>(); // Get headers and create list while (curr != null) { mapList.add(curr); for (String str : curr.keySet()) { CSV_HEADERS.add(str); } curr = parser.next(); } CSVFormat csvFormat = CSVFormat.DEFAULT; CSVPrinter printer = new CSVPrinter(target, csvFormat); printer.printRecord(CSV_HEADERS); // Cycle through array of maps printing value for each header for (Map<String, String> map : mapList) { List<String> values = new ArrayList<String>(); for (String header : CSV_HEADERS) { String value = StringUtils.trimToEmpty(map.get(header)); values.add(value); } printer.printRecord(values.toArray()); } }
Example 12
Source File: CsvFileCreator.java From vividus with Apache License 2.0 | 4 votes |
public CsvFileCreator(File outputDirectory) { this(CSVFormat.DEFAULT, outputDirectory); }
Example 13
Source File: FormattedRowSetResource.java From macrobase with Apache License 2.0 | 4 votes |
@POST @Consumes(MediaType.APPLICATION_JSON) public FormattedRowSetResponse getRowsFormatted(RowSetRequest request) { FormattedRowSetResponse response = new FormattedRowSetResponse(); try { conf.set(MacroBaseConf.DB_URL, request.pgUrl); conf.set(MacroBaseConf.BASE_QUERY, request.baseQuery); HashMap<String, String> preds = new HashMap<>(); request.columnValues.stream().forEach(a -> preds.put(a.column, a.value)); if(request.returnType == RETURNTYPE.SQL) { response.response = ((SQLIngester) getLoader()).getRowsSql(request.baseQuery, preds, request.limit, request.offset)+";"; return response; } RowSet r = getLoader().getRows(request.baseQuery, preds, request.limit, request.offset); if (request.returnType == RETURNTYPE.JSON) { response.response = new ObjectMapper().writeValueAsString(r); } else { assert (request.returnType == RETURNTYPE.CSV); StringWriter sw = new StringWriter(); CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT); if(r.getRows().isEmpty()) { printer.printRecord(preds.keySet()); } else { printer.printRecord(r.getRows().get(0).getColumnValues().stream().map(a -> a.getColumn()).toArray()); for (RowSet.Row row : r.getRows()) { printer.printRecord(row.getColumnValues().stream().map(a -> a.getValue()).toArray()); } } response.response = sw.toString(); } } catch (Exception e) { log.error("An error occurred while processing a request:", e); response.errorMessage = ExceptionUtils.getStackTrace(e); } return response; }
Example 14
Source File: CsvWriter.java From SciGraph with Apache License 2.0 | 4 votes |
@Override CSVPrinter getCsvPrinter(Writer writer) throws IOException { return new CSVPrinter(writer, CSVFormat.DEFAULT); }
Example 15
Source File: FileIndexerPipeline.java From dataflow-opinion-analysis with Apache License 2.0 | 4 votes |
@ProcessElement public void processElement(ProcessContext c) { ContentIndexSummary summary = c.element(); if (summary.sentiments == null) return; try { StringWriter stringWriter = new StringWriter(); CSVPrinter csvPrinter = new CSVPrinter(stringWriter,CSVFormat.DEFAULT); for (int i=0; i < summary.sentiments.length; i++) { ArrayList<String> linefields = new ArrayList<String>(); addField(linefields,"RecordID",summary.doc.collectionItemId); ArrayList<String> sttags = new ArrayList<>(); if (summary.sentiments[i].tags != null) for (int j=0; j < summary.sentiments[i].tags.length; j++) sttags.add(summary.sentiments[i].tags[j].tag); addField(linefields,"Tags",sttags.toString()); // will write as [a,b,c] addField(linefields,"SentimentHash", summary.sentiments[i].sentimentHash); addField(linefields,"Text", summary.sentiments[i].text); addField(linefields,"LabelledPositions", summary.sentiments[i].labelledPositions); addField(linefields,"AnnotatedText", summary.sentiments[i].annotatedText); addField(linefields,"AnnotatedHtml", summary.sentiments[i].annotatedHtmlText); addField(linefields,"SentimentTotalScore", summary.sentiments[i].sentimentTotalScore); addField(linefields,"DominantValence", summary.sentiments[i].dominantValence.ordinal()); addField(linefields,"StAcceptance", summary.sentiments[i].stAcceptance); addField(linefields,"StAnger", summary.sentiments[i].stAnger); addField(linefields,"StAnticipation", summary.sentiments[i].stAnticipation); addField(linefields,"StAmbiguous", summary.sentiments[i].stAmbiguous); addField(linefields,"StDisgust", summary.sentiments[i].stDisgust); addField(linefields,"StFear", summary.sentiments[i].stFear); addField(linefields,"StGuilt", summary.sentiments[i].stGuilt); addField(linefields,"StInterest", summary.sentiments[i].stInterest); addField(linefields,"StJoy", summary.sentiments[i].stJoy); addField(linefields,"StSadness", summary.sentiments[i].stSadness); addField(linefields,"StShame", summary.sentiments[i].stShame); addField(linefields,"StSurprise", summary.sentiments[i].stSurprise); addField(linefields,"StPositive", summary.sentiments[i].stPositive); addField(linefields,"StNegative", summary.sentiments[i].stNegative); addField(linefields,"StSentiment", summary.sentiments[i].stSentiment); addField(linefields,"StProfane", summary.sentiments[i].stProfane); addField(linefields,"StUnsafe", summary.sentiments[i].stUnsafe); ArrayList<String> signalsarray = new ArrayList<>(); if (summary.sentiments[i].signals != null) for (int j=0; j < summary.sentiments[i].signals.length; j++) signalsarray.add(summary.sentiments[i].signals[j]); addField(linefields,"Signals",signalsarray.toString()); csvPrinter.printRecord(linefields); String output = stringWriter.toString().trim(); // need to trim, because printRecord will add the record separator, as will the TextIO.write method at the end of the pipeline csvPrinter.flush(); // will also flush the stringWriter c.output(output); } csvPrinter.close(); } catch (IOException e) { LOG.warn(e.getMessage()); } }
Example 16
Source File: CsvExtractor.java From link-move with Apache License 2.0 | 4 votes |
public CsvExtractor(StreamConnector connector, RowAttribute[] rowHeader, Charset charset) { this(connector, rowHeader, charset, CSVFormat.DEFAULT); }
Example 17
Source File: QuickStart.java From java-samples with Apache License 2.0 | 4 votes |
private static CSVPrinter getCSVPrinter(String fileName) throws IOException { return new CSVPrinter(new FileWriter(fileName), CSVFormat.DEFAULT); }
Example 18
Source File: ReadCSVStep.java From pipeline-utility-steps-plugin with MIT License | 4 votes |
@DataBoundConstructor public ReadCSVStep() { this.format = CSVFormat.DEFAULT; }
Example 19
Source File: CSVFormatFactory.java From incubator-batchee with Apache License 2.0 | 4 votes |
static CSVFormat newFormat(final String format, final String delimiter, final String quoteCharacter, final String quoteMode, final String commentMarker, final String escapeCharacter, final String ignoreSurroundingSpaces, final String ignoreEmptyLines, final String recordSeparator, final String nullString, final String headerComments, final String header, final String skipHeaderRecord, final String allowMissingColumnNames, final String readHeaders) { //CHECKSTYLE:ON CSVFormat out = format == null ? CSVFormat.DEFAULT : CSVFormat.valueOf(format); if (delimiter != null) { out = out.withDelimiter(delimiter.charAt(0)); } if (quoteCharacter != null) { out = out.withQuote(quoteCharacter.charAt(0)); } if (quoteMode != null) { out = out.withQuoteMode(QuoteMode.valueOf(quoteMode)); } if (commentMarker != null) { out = out.withCommentMarker(commentMarker.charAt(0)); } if (escapeCharacter != null) { out = out.withEscape(escapeCharacter.charAt(0)); } if (ignoreSurroundingSpaces != null) { out = out.withIgnoreSurroundingSpaces(Boolean.parseBoolean(ignoreSurroundingSpaces)); } if (ignoreEmptyLines != null) { out = out.withIgnoreEmptyLines(Boolean.parseBoolean(ignoreEmptyLines)); } if (recordSeparator != null) { if ("\\n".equals(recordSeparator)) { out = out.withRecordSeparator('\n'); } else if ("\\r\\n".equals(recordSeparator)) { out = out.withRecordSeparator("\r\n"); } else { out = out.withRecordSeparator(recordSeparator); } } if (nullString != null) { out = out.withNullString(nullString); } if (headerComments != null && !headerComments.trim().isEmpty()) { out = out.withHeaderComments(headerComments.split(" *, *")); } if (Boolean.parseBoolean(readHeaders)) { out = out.withHeader(); } if (header != null && !header.trim().isEmpty()) { try { // headers can have CSV header names so parse it there final Iterator<CSVRecord> iterator = out.withHeader(new String[0]).parse(new StringReader(header + '\n' + header)).iterator(); final CSVRecord record = iterator.next(); final List<String> list = new ArrayList<String>(record.size()); for (final String h : record) { list.add(h); } out = out.withHeader(list.toArray(new String[record.size()])); } catch (final IOException e) { // can't occur actually out = out.withHeader(header.split(" *, *")); } } if (skipHeaderRecord != null) { out = out.withSkipHeaderRecord(Boolean.parseBoolean(skipHeaderRecord)); } if (allowMissingColumnNames != null) { out = out.withAllowMissingColumnNames(Boolean.parseBoolean(allowMissingColumnNames)); } return out; }
Example 20
Source File: ArffConverter.java From jstarcraft-ai with Apache License 2.0 | 4 votes |
public ArffConverter(Collection<QualityAttribute> qualityAttributes, Collection<QuantityAttribute> quantityAttributes) { super(CSVFormat.DEFAULT, qualityAttributes, quantityAttributes); }