@Override public void close() throws IOException { _parser.close(); } }
private static CSVFormat formatForDelimiter(char delimiter) { CSVFormat format = CSV_FORMAT; if (delimiter != format.getDelimiter()) { format = format.withDelimiter(delimiter); } return format; }
public void close() throws IOException { printer.close(); } }
@Override public List<Object> deserialize(ByteBuffer ser) { try { String data = new String(Utils.toByteArray(ser), StandardCharsets.UTF_8); CSVParser parser = CSVParser.parse(data, CSVFormat.RFC4180); CSVRecord record = parser.getRecords().get(0); Preconditions.checkArgument(record.size() == fieldNames.size(), "Invalid schema"); ArrayList<Object> list = new ArrayList<>(fieldNames.size()); for (int i = 0; i < record.size(); i++) { list.add(record.get(i)); } return list; } catch (IOException e) { throw new RuntimeException(e); } }
public static String[] parseAsCsv(String key, String value, Function<String, String> valueProcessor) { String cleanValue = MultivalueProperty.trimFieldsAndRemoveEmptyFields(value); List<String> result = new ArrayList<>(); try (CSVParser csvParser = CSVFormat.RFC4180 .withHeader((String) null) .withIgnoreEmptyLines() .withIgnoreSurroundingSpaces() .parse(new StringReader(cleanValue))) { List<CSVRecord> records = csvParser.getRecords(); if (records.isEmpty()) { return ArrayUtils.EMPTY_STRING_ARRAY; } processRecords(result, records, valueProcessor); return result.toArray(new String[result.size()]); } catch (IOException e) { throw new IllegalStateException("Property: '" + key + "' doesn't contain a valid CSV value: '" + value + "'", e); } }
@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)); } } }
public CSVRecordReader(final InputStream in, final ComponentLog logger, final RecordSchema schema, final CSVFormat csvFormat, final boolean hasHeader, final boolean ignoreHeader, final String dateFormat, final String timeFormat, final String timestampFormat, final String encoding) throws IOException { super(logger, schema, hasHeader, ignoreHeader, dateFormat, timeFormat, timestampFormat); final Reader reader = new InputStreamReader(new BOMInputStream(in), encoding); CSVFormat withHeader; if (hasHeader) { withHeader = csvFormat.withSkipHeaderRecord(); if (ignoreHeader) { withHeader = withHeader.withHeader(schema.getFieldNames().toArray(new String[0])); } else { withHeader = withHeader.withFirstRecordAsHeader(); } } else { withHeader = csvFormat.withHeader(schema.getFieldNames().toArray(new String[0])); } csvParser = new CSVParser(reader, withHeader); }
private static String doJoinDelimited(Iterable<?> elements, CSVFormat format) { StringWriter out = new StringWriter(); try (CSVPrinter printer = new CSVPrinter(out, format)) { for (Object element : elements) { printer.print(element); } printer.flush(); } catch (IOException e) { throw new IllegalStateException(e); } return out.toString(); }
@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); } }
@Override public ByteBuffer write(List<Object> data, ByteBuffer buffer) { try { 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); } } }
public WriteCSVResult(final CSVFormat csvFormat, final RecordSchema recordSchema, final SchemaAccessWriter schemaWriter, final OutputStream out, final String dateFormat, final String timeFormat, final String timestampFormat, final boolean includeHeaderLine, final String charSet) throws IOException { super(out); this.recordSchema = recordSchema; this.schemaWriter = schemaWriter; this.dateFormat = dateFormat; this.timeFormat = timeFormat; this.timestampFormat = timestampFormat; this.includeHeaderLine = includeHeaderLine; final CSVFormat formatWithHeader = csvFormat.withSkipHeaderRecord(true); final OutputStreamWriter streamWriter = new OutputStreamWriter(out, charSet); printer = new CSVPrinter(streamWriter, formatWithHeader); fieldValues = new Object[recordSchema.getFieldCount()]; }
private static String[] doParseDelimited(String delimited, CSVFormat format) { try (CSVParser parser = CSVParser.parse(delimited, format)) { Iterator<CSVRecord> records = parser.iterator(); return records.hasNext() ? StreamSupport.stream(records.next().spliterator(), false).toArray(String[]::new) : EMPTY_STRING; } catch (IOException e) { throw new IllegalStateException(e); // Can't happen } }
public void setOutput(PrintStream output){ final OutputStreamWriter streamWriter = new OutputStreamWriter(output); try { printer = new CSVPrinter(streamWriter, csvFormat); } catch (IOException e) { throw new ProcessException("Failed to create CSV Printer.", 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 flush() throws IOException { printer.flush(); }
/** * @param elements numbers to join by space to make one line of text * @return one line of text, formatted according to PMML quoting rules */ public static String joinPMMLDelimitedNumbers(Iterable<? extends Number> elements) { // bit of a workaround because NON_NUMERIC quote mode still quote "-1"! CSVFormat format = formatForDelimiter(' ').withQuoteMode(QuoteMode.NONE); // No quoting, no need to convert quoting return doJoinDelimited(elements, format); }
@Override public void close() throws IOException { csvParser.close(); } }
@Override public void close() throws IOException { printer.close(); }
@Override public void rewind() throws IOException { _parser.close(); init(); }