Example usage for org.apache.commons.csv CSVFormat withSkipHeaderRecord

List of usage examples for org.apache.commons.csv CSVFormat withSkipHeaderRecord

Introduction

In this page you can find the example usage for org.apache.commons.csv CSVFormat withSkipHeaderRecord.

Prototype

public CSVFormat withSkipHeaderRecord(final boolean skipHeaderRecord) 

Source Link

Document

Sets whether to skip the header record.

Usage

From source file:com.streamsets.pipeline.lib.csv.CsvParser.java

@SuppressWarnings("unchecked")
public CsvParser(CountingReader reader, CSVFormat format, int maxObjectLen, long initialPosition,
        int skipStartLines) throws IOException {
    Utils.checkNotNull(reader, "reader");
    Utils.checkNotNull(reader.getPos() == 0,
            "reader must be in position zero, the CsvParser will fast-forward to the initialPosition");
    Utils.checkNotNull(format, "format");
    Utils.checkArgument(initialPosition >= 0, "initialPosition must be greater or equal than zero");
    Utils.checkArgument(skipStartLines >= 0, "skipStartLines must be greater or equal than zero");
    this.reader = reader;
    currentPos = initialPosition;/*from w  w w . jav a  2 s . c o  m*/
    this.maxObjectLen = maxObjectLen;
    if (initialPosition == 0) {
        if (skipStartLines > 0) {
            skipLinesPosCorrection = skipLines(reader, skipStartLines);
            currentPos = skipLinesPosCorrection;
        }
        if (format.getSkipHeaderRecord()) {
            format = format.withSkipHeaderRecord(false);
            parser = new CSVParser(reader, format, 0, 0);
            headers = read();
        } else {
            parser = new CSVParser(reader, format, 0, 0);
            headers = null;
        }
    } else {
        if (format.getSkipHeaderRecord()) {
            format = format.withSkipHeaderRecord(false);
            parser = new CSVParser(reader, format, 0, 0);
            headers = read();
            while (getReaderPosition() < initialPosition && read() != null) {
            }
            if (getReaderPosition() != initialPosition) {
                throw new IOException(
                        Utils.format("Could not position reader at position '{}', got '{}' instead",
                                initialPosition, getReaderPosition()));
            }
        } else {
            IOUtils.skipFully(reader, initialPosition);
            parser = new CSVParser(reader, format, initialPosition, 0);
            headers = null;
        }
    }
}

From source file:org.apache.batchee.csv.CSVFormatFactory.java

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));
    }/*from  www .  jav  a 2  s  .c om*/
    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;
}

From source file:org.apache.nifi.csv.WriteCSVResult.java

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);// ww  w.  j a  v a 2 s.co m
    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()];
}

From source file:org.apache.nifi.processors.ParseCSV.ParseCSV.java

private CSVFormat buildFormat(String format, char delimiter, Boolean with_header, String custom_header) {
    CSVFormat csvFormat = null;

    // set pre built format
    if (format.equals("DEFAULT")) {
        csvFormat = CSVFormat.DEFAULT;//from  w  w  w .  j  a  va2  s  .c  o m
    } else if (format.equals("EXCEL")) {
        csvFormat = CSVFormat.EXCEL;
    }

    if (with_header & custom_header != null) {
        csvFormat = csvFormat.withSkipHeaderRecord(true);
        csvFormat = csvFormat.withHeader(custom_header);
    } else if (with_header & custom_header == null) {
        csvFormat = csvFormat.withHeader();
    }

    if (delimiter > 0) {
        csvFormat = csvFormat.withDelimiter(delimiter);
    }
    return csvFormat;
}

From source file:org.structr.csv.FromCsvFunction.java

@Override
public Object apply(ActionContext ctx, final GraphObject entity, final Object[] sources) {

    if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 1, 4)) {

        try {/*from   w w  w  .  j a v a2s.c  om*/

            final List<Map<String, String>> objects = new LinkedList<>();
            final String source = sources[0].toString();
            String delimiter = ";";
            String quoteChar = "\"";
            String recordSeparator = "\n";

            switch (sources.length) {

            case 4:
                recordSeparator = (String) sources[3];
            case 3:
                quoteChar = (String) sources[2];
            case 2:
                delimiter = (String) sources[1];
                break;
            }

            CSVFormat format = CSVFormat.newFormat(delimiter.charAt(0)).withHeader();
            format = format.withQuote(quoteChar.charAt(0));
            format = format.withRecordSeparator(recordSeparator);
            format = format.withIgnoreEmptyLines(true);
            format = format.withIgnoreSurroundingSpaces(true);
            format = format.withSkipHeaderRecord(true);
            format = format.withQuoteMode(QuoteMode.ALL);

            CSVParser parser = new CSVParser(new StringReader(source), format);
            for (final CSVRecord record : parser.getRecords()) {

                objects.add(record.toMap());
            }

            return objects;

        } catch (Throwable t) {

            logException(t, "{0}: Exception for parameter: {1}",
                    new Object[] { getName(), getParametersAsString(sources) });

        }

        return "";

    } else {

        logParameterError(entity, sources, ctx.isJavaScriptContext());

    }

    return usage(ctx.isJavaScriptContext());
}

From source file:org.structr.function.FromCsvFunction.java

@Override
public Object apply(ActionContext ctx, final GraphObject entity, final Object[] sources) {

    if (sources != null && sources.length > 0) {

        if (sources[0] != null) {

            try {

                final List<Map<String, String>> objects = new LinkedList<>();
                final String source = sources[0].toString();
                String delimiter = ";";
                String quoteChar = "\"";
                String recordSeparator = "\n";

                switch (sources.length) {

                case 4:
                    recordSeparator = (String) sources[3];
                case 3:
                    quoteChar = (String) sources[2];
                case 2:
                    delimiter = (String) sources[1];
                    break;
                }/* w  ww .jav  a  2s  .  c  o m*/

                CSVFormat format = CSVFormat.newFormat(delimiter.charAt(0)).withHeader();
                format = format.withQuote(quoteChar.charAt(0));
                format = format.withRecordSeparator(recordSeparator);
                format = format.withIgnoreEmptyLines(true);
                format = format.withIgnoreSurroundingSpaces(true);
                format = format.withSkipHeaderRecord(true);
                format = format.withQuoteMode(QuoteMode.ALL);

                CSVParser parser = new CSVParser(new StringReader(source), format);
                for (final CSVRecord record : parser.getRecords()) {

                    objects.add(record.toMap());
                }

                return objects;

            } catch (Throwable t) {
                t.printStackTrace();
            }
        }

        return "";
    }

    return usage(ctx.isJavaScriptContext());
}