List of usage examples for org.apache.commons.csv CSVPrinter print
public void print(final Object value) throws IOException
From source file:org.apache.beam.sdk.extensions.sql.impl.schema.text.BeamTextCSVTableTest.java
/** * Helper that writes the given lines (adding a newline in between) to a stream, then closes the * stream.// w ww . j a v a 2 s . c o m */ private static void writeToStreamAndClose(List<Object[]> rows, OutputStream outputStream) { try (PrintStream writer = new PrintStream(outputStream)) { CSVPrinter printer = CSVFormat.DEFAULT.print(writer); for (Object[] row : rows) { for (Object field : row) { printer.print(field); } printer.println(); } } catch (IOException e) { e.printStackTrace(); } }
From source file:org.apache.storm.sql.runtime.serde.csv.CsvSerializer.java
@Override public ByteBuffer write(List<Object> data, ByteBuffer buffer) { try {/* www .j av a2s .c o m*/ StringWriter writer = new StringWriter(); CSVPrinter printer = new CSVPrinter(writer, CSVFormat.RFC4180); for (Object o : data) { printer.print(o); } //since using StringWriter, we do not need to close it. return ByteBuffer.wrap(writer.getBuffer().toString().getBytes(StandardCharsets.UTF_8)); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:org.cast.cwm.admin.CSVDownload.java
/** * creates a new resource response based on the request attributes * * @param attributes current request attributes from client * @return resource response for answering request *//*from w w w . ja v a2s . c om*/ @Override protected ResourceResponse newResourceResponse(Attributes attributes) { ResourceResponse rr = new ResourceResponse(); rr.disableCaching(); rr.setFileName("log.csv"); rr.setContentDisposition(ContentDisposition.ATTACHMENT); rr.setContentType("text/csv"); if (rr.dataNeedsToBeWritten(attributes)) { rr.setWriteCallback(new WriteCallback() { @Override public void writeData(Attributes attributes) { Response response = attributes.getResponse(); try { CSVPrinter writer = new CSVPrinter( new OutputStreamWriter(response.getOutputStream(), "UTF-8"), CSVFormat.EXCEL); // Write header row for (IDataColumn<E> col : columns) { writer.print(col.getHeaderString()); } writer.println(); // Write documentation row, if requested if (includeDocumentationRow) { for (IDataColumn<E> col : columns) { if (col instanceof IDocumentedColumn && ((IDocumentedColumn) col).getDocumentationModel() != null) { writer.print(((IDocumentedColumn) col).getDocumentationModel().getObject()); } else { writer.print(""); } } writer.println(); } // Write Data Iterator<? extends E> it = iteratorProvider.getIterator(); while (it.hasNext()) { E e = it.next(); for (IDataColumn<E> col : columns) { String columnValue = col.getItemString(new Model<E>(e)); if (columnValue == null) { log.warn("Got a null value for {} of item {}", col.getHeaderString(), e); columnValue = "null"; } // Clean up text -- CSV file cannot have newlines in it writer.print(columnValue.replaceAll("[\r\n]", " ")); } writer.println(); } writer.close(); } catch (UnsupportedEncodingException e) { throw new StringValueConversionException("UTF-8 translation not supported?!", e); } catch (IOException e) { throw new WicketRuntimeException("Couldn't write to resource", e); } } }); } return rr; }
From source file:org.commonvox.hbase_column_manager.ColumnQualifierReport.java
void printReport(CSVPrinter csvPrinter, Set<SchemaEntity> schemaEntities) throws IOException { for (SchemaEntity entity : schemaEntities) { switch (entity.getSchemaEntityType()) { case NAMESPACE: namespaceReportString = entity.getNameAsString(); tableReportString = ""; colFamilyReportString = ""; colQualifierReportString = ""; colMaxLengthReportString = ""; printReport(csvPrinter, entity.getChildren()); break; case TABLE: tableReportString = entity.getNameAsString(); printReport(csvPrinter, entity.getChildren()); break; case COLUMN_FAMILY: colFamilyReportString = entity.getNameAsString(); if (this.sourceColFamily != null && !colFamilyReportString.equals(Bytes.toString(sourceColFamily))) { continue; }/*from w w w . j a va 2 s . c om*/ printReport(csvPrinter, entity.getChildren()); break; case COLUMN_AUDITOR: colQualifierReportString = entity.getNameAsString(); colMaxLengthReportString = entity.getValue(ColumnAuditor.MAX_VALUE_LENGTH_KEY); csvPrinter.print(namespaceReportString); csvPrinter.print(tableReportString); csvPrinter.print(colFamilyReportString); csvPrinter.print(colQualifierReportString); csvPrinter.print(colMaxLengthReportString); csvPrinter.println(); recordsWrittenToReport = true; } } }
From source file:org.openmrs.projectbuendia.servlet.DataExportServlet.java
private void writeHeaders(CSVPrinter printer, FixedSortedConceptIndexer indexer) throws IOException { for (String fixedHeader : FIXED_HEADERS) { printer.print(fixedHeader); }/*from w ww.j a v a 2 s . c om*/ for (int i = 0; i < indexer.size(); i++) { // For each observation there are three columns: one for the English // name, one for the OpenMRS ID, and one for the UUID of the concept. assert COLUMNS_PER_OBS == 3; Concept concept = indexer.getConcept(i); printer.print(NAMER.getClientName(concept)); printer.print(concept.getId()); printer.print(concept.getUuid()); } printer.println(); }
From source file:org.opennms.netmgt.integrations.R.RScriptExecutor.java
/** * Convert the table to a CSV string.//from w w w.j a v a2 s . com */ protected static StringBuilder toCsv(final RowSortedTable<Long, String, Double> table) throws IOException { final String columnNames[] = table.columnKeySet().toArray(new String[] {}); final StringBuilder sb = new StringBuilder(); final CSVPrinter printer = CSVFormat.RFC4180.withHeader(columnNames).print(sb); for (long rowIndex : table.rowKeySet()) { for (String columnName : columnNames) { Double value = table.get(rowIndex, columnName); if (value == null) { value = Double.NaN; } printer.print(value); } printer.println(); } return sb; }
From source file:org.thegalactic.context.io.ContextSerializerCsv.java
/** * Write a context to a csv file./*w ww. j a va 2 s . co m*/ * * The following format is respected: * * The first line contains the attribute names, the other lines contains the * observations identifier followed by boolean values * * ~~~ * "",a,b,c,d,e * 1,1,0,1,0,0 * 2,1,1,0,0,0 * 3,0,1,0,1,1 * 4,0,0,1,0,1 * ~~~ * * @param context a context to write * @param file a file * * @throws IOException When an IOException occurs */ public void write(Context context, BufferedWriter file) throws IOException { CSVPrinter printer = new CSVPrinter(file, CSVFormat.RFC4180); // Get the observations and the attributes TreeSet<Comparable> observations = context.getObservations(); TreeSet<Comparable> attributes = context.getAttributes(); // Prepare the attribute line printer.print(""); for (Comparable attribute : attributes) { // Write each attribute printer.print(attribute); } printer.println(); for (Comparable observation : observations) { // Write the observation printer.print(observation); // Write the extent/intents for (Comparable attribute : attributes) { if (context.getIntent(observation).contains(attribute)) { printer.print(1); } else { printer.print(0); } } printer.println(); } printer.close(); }