@Override public byte[] serialize(final String topic, final GenericRow genericRow) { if (genericRow == null) { return null; } try { final StringWriter stringWriter = new StringWriter(); final CSVPrinter csvPrinter = new CSVPrinter(stringWriter, CSVFormat.DEFAULT); csvPrinter.printRecord(genericRow.getColumns()); final String result = stringWriter.toString(); return result.substring(0, result.length() - 2).getBytes(StandardCharsets.UTF_8); } catch (final Exception e) { throw new SerializationException("Error serializing CSV message", e); } }
private void includeHeaderIfNecessary(final Record record, final boolean includeOnlySchemaFields) throws IOException { if (headerWritten || !includeHeaderLine) { return; } final Object[] fieldNames; if (includeOnlySchemaFields) { fieldNames = recordSchema.getFieldNames().toArray(new Object[0]); } else { fieldNames = getFieldNames(record); } printer.printRecord(fieldNames); headerWritten = true; }
@Override public void endRow(int rowNum) { if(skipRow) { return; } if(firstRow){ readConfig.setLastColumn(currentCol); } //if there was no data in this row, don't write it if(!rowHasValues) { return; } // Ensure the correct number of columns int columnsToAdd = (readConfig.getLastColumn() - currentCol) - readConfig.getColumnsToSkip().size(); for (int i=0; i<columnsToAdd; i++) { fieldValues.add(null); } try { printer.printRecord(fieldValues); } catch (IOException e) { e.printStackTrace(); } rowCount++; }
@BeforeClass public void setUp() throws Exception { FileUtils.forceMkdir(TEMP_DIR); try (FileWriter fileWriter = new FileWriter(DATA_FILE); CSVPrinter csvPrinter = new CSVPrinter(fileWriter, CSVFormat.DEFAULT.withHeader(COLUMNS))) { for (Object[] record : RECORDS) { csvPrinter.printRecord(record[0], StringUtils.join((int[]) record[1], CSVRecordReaderConfig.DEFAULT_MULTI_VALUE_DELIMITER)); } } }
@Override public Map<String, String> writeRecord(final Record record) throws IOException { // If we are not writing an active record set, then we need to ensure that we write the // schema information. if (!isActiveRecordSet()) { schemaWriter.writeHeader(recordSchema, getOutputStream()); } includeHeaderIfNecessary(record, true); int i = 0; for (final RecordField recordField : recordSchema.getFields()) { fieldValues[i++] = record.getAsString(recordField, getFormat(recordField)); } printer.printRecord(fieldValues); return schemaWriter.getAttributes(recordSchema); }
@Override public WriteResult writeRawRecord(final Record record) throws IOException { // If we are not writing an active record set, then we need to ensure that we write the // schema information. if (!isActiveRecordSet()) { schemaWriter.writeHeader(recordSchema, getOutputStream()); } includeHeaderIfNecessary(record, false); final String[] fieldNames = getFieldNames(record); // Avoid creating a new Object[] for every Record if we can. But if the record has a different number of columns than does our // schema, we don't have a lot of options here, so we just create a new Object[] in that case. final Object[] recordFieldValues = (fieldNames.length == this.fieldValues.length) ? this.fieldValues : new String[fieldNames.length]; int i = 0; for (final String fieldName : fieldNames) { final Optional<RecordField> recordField = recordSchema.getField(fieldName); if (recordField.isPresent()) { recordFieldValues[i++] = record.getAsString(fieldName, getFormat(recordField.get())); } else { recordFieldValues[i++] = record.getAsString(fieldName); } } printer.printRecord(recordFieldValues); final Map<String, String> attributes = schemaWriter.getAttributes(recordSchema); return WriteResult.of(incrementRecordCount(), attributes); }
@Override protected void open(String header) throws IOException { // Check if already so we only open one writer if (csvPrinter != null) { return; } csvPrinter = new CSVPrinter(new PrintWriter(resultFileName), CSVFormat.DEFAULT); Object[] records = header.split(PherfConstants.RESULT_FILE_DELIMETER); csvPrinter.printRecord(records); isClosed = false; } }
@Override public synchronized void write(Result result) throws IOException { csvPrinter.printRecord(result.getResultValues()); flush(); }
@Test public void testPrintToFileWithCharsetUtf16Be() throws IOException { final File file = File.createTempFile(getClass().getName(), ".csv"); try (final CSVPrinter printer = CSVFormat.DEFAULT.print(file, StandardCharsets.UTF_16BE)) { printer.printRecord("a", "b\\c"); } assertEquals("a,b\\c" + recordSeparator, FileUtils.readFileToString(file, StandardCharsets.UTF_16BE)); }
@Test public void testQuoteCommaFirstChar() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.RFC4180)) { printer.printRecord(","); assertEquals("\",\"" + recordSeparator, sw.toString()); } }
@Test public void testHeader() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(null).withHeader("C1", "C2", "C3"))) { printer.printRecord("a", "b", "c"); printer.printRecord("x", "y", "z"); assertEquals("C1,C2,C3\r\na,b,c\r\nx,y,z\r\n", sw.toString()); } }
@Test public void testPrint() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = CSVFormat.DEFAULT.print(sw)) { printer.printRecord("a", "b\\c"); assertEquals("a,b\\c" + recordSeparator, sw.toString()); } }
@Test public void testPrinter1() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT)) { printer.printRecord("a", "b"); assertEquals("a,b" + recordSeparator, sw.toString()); } }
@Test public void testDontQuoteEuroFirstChar() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.RFC4180)) { printer.printRecord(EURO_CH, "Deux"); assertEquals(EURO_CH + ",Deux" + recordSeparator, sw.toString()); } }
@Test public void testPrintNullValues() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT)) { printer.printRecord("a", null, "b"); assertEquals("a,,b" + recordSeparator, sw.toString()); } }
@Test public void testQuoteAll() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL))) { printer.printRecord("a", "b\nc", "d"); assertEquals("\"a\",\"b\nc\",\"d\"" + recordSeparator, sw.toString()); } }
@Test public void testTrailingDelimiterOnTwoColumns() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withTrailingDelimiter())) { printer.printRecord("A", "B"); assertEquals("A,B,\r\n", sw.toString()); } }
@Test public void testQuoteModeAll() throws Exception { final CSVFormat format = CSVFormat.EXCEL .withNullString("N/A") .withIgnoreSurroundingSpaces(true) .withQuoteMode(QuoteMode.ALL); final StringBuffer buffer = new StringBuffer(); final CSVPrinter printer = new CSVPrinter(buffer, format); printer.printRecord(new Object[] { null, "Hello", null, "World" }); Assert.assertEquals("\"N/A\",\"Hello\",\"N/A\",\"World\"\r\n", buffer.toString()); }
@Test public void testQuoteModeMinimal() throws Exception { final CSVFormat format = CSVFormat.EXCEL .withNullString("N/A") .withIgnoreSurroundingSpaces(true) .withQuoteMode(QuoteMode.MINIMAL); final StringBuffer buffer = new StringBuffer(); final CSVPrinter printer = new CSVPrinter(buffer, format); printer.printRecord(new Object[] { null, "Hello", null, "World" }); Assert.assertEquals("N/A,Hello,N/A,World\r\n", buffer.toString()); }
@Test public void testQuoteModeNonNumeric() throws Exception { final CSVFormat format = CSVFormat.EXCEL .withNullString("N/A") .withIgnoreSurroundingSpaces(true) .withQuoteMode(QuoteMode.NON_NUMERIC); final StringBuffer buffer = new StringBuffer(); final CSVPrinter printer = new CSVPrinter(buffer, format); printer.printRecord(new Object[] { null, "Hello", null, "World" }); Assert.assertEquals("N/A,\"Hello\",N/A,\"World\"\r\n", buffer.toString()); }