Java Code Examples for org.apache.commons.csv.CSVFormat#withHeader()
The following examples show how to use
org.apache.commons.csv.CSVFormat#withHeader() .
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: CSVRecordReader.java From nifi with Apache License 2.0 | 6 votes |
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); }
Example 2
Source File: CSVFileManager.java From connector-sdk with Apache License 2.0 | 5 votes |
private static CSVFormat createCsvFormat(String csvFormatName, List<String> csvColumns, boolean skipHeader) { CSVFormat csvFormat = null; if (csvFormatName.isEmpty()) { csvFormat = CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(); } else { Set<CSVFormat.Predefined> csvFormats = getPredefinedCsvFormats(); for (CSVFormat.Predefined format : csvFormats) { if (format.toString().equalsIgnoreCase(csvFormatName)) { csvFormat = format.getFormat(); break; } } if (csvFormat == null) { throw new InvalidConfigurationException( "Invalid CSVFormat " + csvFormatName + ", must be one of " + csvFormats); } } csvFormat = applyCsvFormatMethods(csvFormat); if (csvColumns.isEmpty()) { checkState( !skipHeader, "csv.csvColumns property must be specified " + "if csv.skipHeaderRecord is true"); return csvFormat.withHeader(); } else { return csvFormat .withHeader(csvColumns.toArray(new String[0])) .withSkipHeaderRecord(skipHeader); } }
Example 3
Source File: DelimitedCharDataGenerator.java From datacollector with Apache License 2.0 | 5 votes |
public DelimitedCharDataGenerator(Writer writer, CSVFormat format, CsvHeader header, String headerKey, String valueKey, String replaceNewLines) throws IOException { format = format.withHeader((String[])null); this.format = format; this.headerKey = headerKey; this.valueKey = valueKey; printer = new CSVPrinter(writer, format); this.header = header; firstRecord = true; this.replaceNewLines = replaceNewLines; }
Example 4
Source File: CSVCommonsLoader.java From phoenix with Apache License 2.0 | 5 votes |
/** * default settings * delimiter = ',' * quoteChar = '"', * escape = null * recordSeparator = CRLF, CR, or LF * ignore empty lines allows the last data line to have a recordSeparator * * @return CSVFormat based on constructor settings. */ private CSVFormat buildFormat() { CSVFormat format = CSVFormat.DEFAULT .withIgnoreEmptyLines(true) .withDelimiter(asControlCharacter(fieldDelimiter)) .withQuote(asControlCharacter(quoteCharacter)); if (escapeCharacter != null) { format = format.withEscape(asControlCharacter(escapeCharacter)); } switch(headerSource) { case FROM_TABLE: // obtain headers from table, so format should not expect a header. break; case IN_LINE: // an empty string array triggers csv loader to grab the first line as the header format = format.withHeader(new String[0]); break; case SUPPLIED_BY_USER: // a populated string array supplied by the user format = format.withHeader(columns.toArray(new String[columns.size()])); break; default: throw new RuntimeException("Header source was unable to be inferred."); } return format; }
Example 5
Source File: GanttCSVOpen.java From ganttproject with GNU General Public License v3.0 | 5 votes |
private CSVFormat createCSVFormat(List<String> headers) { CSVFormat format = CSVFormat.DEFAULT.withIgnoreEmptyLines(false).withIgnoreSurroundingSpaces(true); if (myCsvOptions != null) { format = format.withDelimiter(myCsvOptions.sSeparatedChar.charAt(0)).withQuote(myCsvOptions.sSeparatedTextChar.charAt(0)); } if (headers != null) { format = format.withHeader(headers.toArray(new String[0])); } return format; }
Example 6
Source File: CSVCommonsLoader.java From phoenix with Apache License 2.0 | 5 votes |
/** * default settings * delimiter = ',' * quoteChar = '"', * escape = null * recordSeparator = CRLF, CR, or LF * ignore empty lines allows the last data line to have a recordSeparator * * @return CSVFormat based on constructor settings. */ private CSVFormat buildFormat() { CSVFormat format = CSVFormat.DEFAULT .withIgnoreEmptyLines(true) .withDelimiter(asControlCharacter(fieldDelimiter)) .withQuote(asControlCharacter(quoteCharacter)); if (escapeCharacter != null) { format = format.withEscape(asControlCharacter(escapeCharacter)); } switch(headerSource) { case FROM_TABLE: // obtain headers from table, so format should not expect a header. break; case IN_LINE: // an empty string array triggers csv loader to grab the first line as the header format = format.withHeader(new String[0]); break; case SUPPLIED_BY_USER: // a populated string array supplied by the user format = format.withHeader(columns.toArray(new String[columns.size()])); break; default: throw new RuntimeException("Header source was unable to be inferred."); } return format; }
Example 7
Source File: CSVRecordReader.java From incubator-pinot with Apache License 2.0 | 4 votes |
@Override public void init(File dataFile, Set<String> fieldsToRead, @Nullable RecordReaderConfig recordReaderConfig) throws IOException { _dataFile = dataFile; CSVRecordReaderConfig config = (CSVRecordReaderConfig) recordReaderConfig; char multiValueDelimiter; if (config == null) { _format = CSVFormat.DEFAULT.withDelimiter(CSVRecordReaderConfig.DEFAULT_DELIMITER).withHeader(); multiValueDelimiter = CSVRecordReaderConfig.DEFAULT_MULTI_VALUE_DELIMITER; } else { CSVFormat format; String formatString = config.getFileFormat(); if (formatString == null) { format = CSVFormat.DEFAULT; } else { switch (formatString.toUpperCase()) { case "EXCEL": format = CSVFormat.EXCEL; break; case "MYSQL": format = CSVFormat.MYSQL; break; case "RFC4180": format = CSVFormat.RFC4180; break; case "TDF": format = CSVFormat.TDF; break; default: format = CSVFormat.DEFAULT; break; } } char delimiter = config.getDelimiter(); format = format.withDelimiter(delimiter); String csvHeader = config.getHeader(); if (csvHeader == null) { format = format.withHeader(); } else { format = format.withHeader(StringUtils.split(csvHeader, delimiter)); } _format = format; multiValueDelimiter = config.getMultiValueDelimiter(); } _recordExtractor = new CSVRecordExtractor(); CSVRecordExtractorConfig recordExtractorConfig = new CSVRecordExtractorConfig(); recordExtractorConfig.setMultiValueDelimiter(multiValueDelimiter); _recordExtractor.init(fieldsToRead, recordExtractorConfig); init(); }
Example 8
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; }