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

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

Introduction

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

Prototype

public String[] getHeader() 

Source Link

Document

Returns a copy of the header array.

Usage

From source file:cz.pichlik.goodsentiment.common.CSVFormatsTest.java

@Test
public void emptyHeader() {
    CSVFormat format = CSVFormats.format();
    assertThat(format.getHeader(), nullValue());
}

From source file:cz.pichlik.goodsentiment.common.CSVFormatsTest.java

@Test
public void formatWithHeader() {
    CSVFormat format = CSVFormats.format("test");
    assertThat(format.getHeader(), hasItemInArray("test"));
}

From source file:com.rodaxsoft.mailgun.CampaignManager.java

/**
 * Saves campaign events to a CSV file with the following format:
 * <code>&lt;campaign name&gt;_(&lt;campaign id&gt;)_&lt;timestamp&gt;.csv</code>
 * @param campaignId The campaign ID/*from www .  j a  v  a  2  s .c om*/
 * @throws ContextedException if a processing error occurs
 * @throws IOException if an I/O error occurs
 */
void saveCampaignEventsToCSV(String campaignId) throws ContextedException, IOException {

    Campaign campaign = getCampaign(campaignId);

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

    String dateTime = format.format(new Date());
    String fileName;

    if (campaign != null) {

        String name = StringUtils.replace(campaign.getName(), " ", "_");
        fileName = new StringBuilder(name).append("_(").append(campaignId).append(")_").append(dateTime)
                .append(".csv").toString();
    } else {
        fileName = campaignId + "_" + dateTime + ".csv";
    }

    CSVPrinter csvPrinter = null;
    PrintWriter pw = null;
    CSVFormat csvFormat = null;
    try {

        pw = new PrintWriter(fileName);
        final List<Map<String, Object>> events = getEvents(campaignId);

        for (Map<String, Object> map : events) {

            if (null == csvPrinter) {
                final Set<String> keySet = map.keySet();
                int size = keySet.size();
                String[] keys = keySet.toArray(new String[size]);
                csvFormat = CSVFormat.DEFAULT.withHeader(keys);
                csvPrinter = new CSVPrinter(pw, csvFormat);
            }
            //            city   domain   tags   timestamp   region   ip   country   recipient   event   user_vars

            String[] headers = csvFormat.getHeader();
            for (String key : headers) {
                csvPrinter.print(map.get(key));
            }

            csvPrinter.println();
        }

    } finally {

        if (csvPrinter != null) {
            csvPrinter.flush();
        }

        IOUtils.closeQuietly(csvPrinter);
    }

}

From source file:fabrice.CSVPrinter.java

/**
 * Creates a printer that will print values to the given stream following the CSVFormat.
 * <p>//from  ww  w  .  j av  a 2  s. c  o m
 * Currently, only a pure encapsulation format or a pure escaping format is supported. Hybrid formats (encapsulation
 * and escaping with a different character) are not supported.
 * </p>
 *
 * @param out
 *            stream to which to print. Must not be null.
 * @param format
 *            the CSV format. Must not be null.
 * @throws IOException
 *             thrown if the optional header cannot be printed.
 * @throws IllegalArgumentException
 *             thrown if the parameters of the format are inconsistent or if either out or format are null.
 */
public CSVPrinter(final Appendable out, final CSVFormat format) throws IOException {

    if (out == null) {
        throw new IllegalArgumentException("Parameter out must not be null!");
    }
    if (format == null) {
        throw new IllegalArgumentException("Parameter format must not be null!");
    }

    this.out = out;
    this.format = format;
    // TODO: Is it a good idea to do this here instead of on the first call to a print method?
    // It seems a pain to have to track whether the header has already been printed or not.
    if (format.getHeaderComments() != null) {
        for (String line : format.getHeaderComments()) {
            if (line != null) {
                this.printComment(line);
            }
        }
    }
    if (format.getHeader() != null) {
        this.printRecord((Object[]) format.getHeader());
    }
}

From source file:com.datascience.cascading.scheme.CsvScheme.java

/**
 * Configures the Hadoop configuration for the given CSV format.
 *//*from  w w w. j a  v  a 2 s  .c  o m*/
private void configureReaderFormat(CSVFormat format, Configuration conf) {
    conf.set(CsvOutputFormat.CHARSET, charset);

    // If the format header was explicitly provided by the user then forward it to the record reader. If skipHeaderRecord
    // is enabled then that indicates that field names were detected. We need to ensure that headers are defined in order
    // for the CSV reader to skip the header record.
    conf.setBoolean(CsvInputFormat.STRICT_MODE, strict);
    if (format.getHeader() != null) {
        conf.setStrings(CsvInputFormat.CSV_READER_COLUMNS, format.getHeader());
    } else if (format.getSkipHeaderRecord()) {
        Fields fields = getSourceFields();
        String[] columns = new String[fields.size()];
        for (int i = 0; i < fields.size(); i++) {
            columns[i] = fields.get(i).toString();
        }
        conf.setStrings(CsvInputFormat.CSV_READER_COLUMNS, columns);
    }

    conf.setBoolean(CsvInputFormat.CSV_READER_SKIP_HEADER, format.getSkipHeaderRecord());
    conf.set(CsvInputFormat.CSV_READER_DELIMITER, String.valueOf(format.getDelimiter()));

    if (format.getRecordSeparator() != null) {
        conf.set(CsvInputFormat.CSV_READER_RECORD_SEPARATOR, format.getRecordSeparator());
    }

    if (format.getQuoteCharacter() != null) {
        conf.set(CsvInputFormat.CSV_READER_QUOTE_CHARACTER, String.valueOf(format.getQuoteCharacter()));
    }

    if (format.getQuoteMode() != null) {
        conf.set(CsvInputFormat.CSV_READER_QUOTE_MODE, format.getQuoteMode().name());
    }

    if (format.getEscapeCharacter() != null) {
        conf.set(CsvInputFormat.CSV_READER_ESCAPE_CHARACTER, String.valueOf(format.getEscapeCharacter()));
    }

    conf.setBoolean(CsvInputFormat.CSV_READER_IGNORE_EMPTY_LINES, format.getIgnoreEmptyLines());
    conf.setBoolean(CsvInputFormat.CSV_READER_IGNORE_SURROUNDING_SPACES, format.getIgnoreSurroundingSpaces());

    if (format.getNullString() != null) {
        conf.set(CsvInputFormat.CSV_READER_NULL_STRING, format.getNullString());
    }
}

From source file:com.datascience.cascading.scheme.CsvScheme.java

/**
 * Configures the Hadoop configuration for the given CSV format.
 *//*  w  w w  . j ava  2  s  . co m*/
private void configureWriterFormat(CSVFormat format, Configuration conf) {
    conf.set(CsvOutputFormat.CHARSET, charset);

    // Apache CSV doesn't really handle the skipHeaderRecord flag correctly when writing output. If the skip flag is set
    // and headers are configured, headers will always be written to the output. Since we always have headers and/or
    // fields configured, we need to use the skipHeaderRecord flag to determine whether headers should be written.
    if (!format.getSkipHeaderRecord()) {
        if (format.getHeader() != null && format.getHeader().length != 0) {
            conf.setStrings(CsvOutputFormat.CSV_WRITER_COLUMNS, format.getHeader());
        } else {
            Fields fields = getSinkFields();
            String[] columns = new String[fields.size()];
            for (int i = 0; i < fields.size(); i++) {
                columns[i] = fields.get(i).toString();
            }
            conf.setStrings(CsvOutputFormat.CSV_WRITER_COLUMNS, columns);
        }
    }

    conf.setBoolean(CsvOutputFormat.CSV_WRITER_SKIP_HEADER, format.getSkipHeaderRecord());
    conf.set(CsvOutputFormat.CSV_WRITER_DELIMITER, String.valueOf(format.getDelimiter()));

    if (format.getRecordSeparator() != null) {
        conf.set(CsvOutputFormat.CSV_WRITER_RECORD_SEPARATOR, format.getRecordSeparator());
    }

    if (format.getQuoteCharacter() != null) {
        conf.set(CsvOutputFormat.CSV_WRITER_QUOTE_CHARACTER, String.valueOf(format.getQuoteCharacter()));
    }

    if (format.getQuoteMode() != null) {
        conf.set(CsvOutputFormat.CSV_WRITER_QUOTE_MODE, format.getQuoteMode().name());
    }

    if (format.getEscapeCharacter() != null) {
        conf.set(CsvOutputFormat.CSV_WRITER_ESCAPE_CHARACTER, String.valueOf(format.getEscapeCharacter()));
    }

    conf.setBoolean(CsvOutputFormat.CSV_WRITER_IGNORE_EMPTY_LINES, format.getIgnoreEmptyLines());
    conf.setBoolean(CsvOutputFormat.CSV_WRITER_IGNORE_SURROUNDING_SPACES, format.getIgnoreSurroundingSpaces());

    if (format.getNullString() != null) {
        conf.set(CsvOutputFormat.CSV_WRITER_NULL_STRING, format.getNullString());
    }
}

From source file:org.apache.camel.dataformat.csv.CsvUnmarshaller.java

public static CsvUnmarshaller create(CSVFormat format, CsvDataFormat dataFormat) {
    // If we want to use maps, thus the header must be either fixed or automatic
    if (dataFormat.isUseMaps() && format.getHeader() == null) {
        format = format.withHeader();/*from ww  w  .j a  v  a 2  s  .c o m*/
    }
    // If we want to skip the header record it must automatic otherwise it's not working
    if (format.getSkipHeaderRecord() && format.getHeader() == null) {
        format = format.withHeader();
    }

    if (dataFormat.isLazyLoad()) {
        return new StreamCsvUnmarshaller(format, dataFormat);
    }
    return new BulkCsvUnmarshaller(format, dataFormat);
}

From source file:org.qcert.util.DataLoader.java

/** Primary entry point to this service.  The 'main' method is also useful but can be bypassed to call this instead
 *   when files are named in non-standard ways or to use a CSV format understood by Apache commons-csv but other than strict RFC 4180.
 * @param tableMap a map from "table names" (type names matching CSV files) to the corresponding CSV file contents (as Strings)
 * @param jsonSchema the JSON format schema in one of several possible variations, as a JsonElement (array or object)
 * @param format the CSV format to use (if the format does not include a header we assume that the first line of 
 *   each file constitutes a header/*from w  ww.  j a v a2s . c  o m*/
 * @return a JsonObject representing the loaded data
 * @throws Exception
 */
public static JsonObject loadData(Map<String, String> tableMap, JsonElement jsonSchema, CSVFormat format)
        throws Exception {
    if (format.getHeader() == null)
        format = format.withHeader();
    Map<String, ObjectType> types = SchemaUtil.getSchema(jsonSchema);
    boolean partitioned = types.size() == 0;
    if (partitioned) {
        if (jsonSchema.isJsonObject())
            types = SchemaUtil.getGlobalsFromSchema(jsonSchema.getAsJsonObject());
        if (types.size() == 0)
            throw new IllegalArgumentException("Schema contains no useful information");
    }

    /* Process the data files */
    JsonObject ans = new JsonObject();
    JsonArray world = partitioned ? null : new JsonArray();
    if (!partitioned)
        ans.add("WORLD", world);
    for (Entry<String, String> filePair : tableMap.entrySet()) {
        String table = filePair.getKey();
        ObjectType def = types.get(table);
        if (def == null)
            throw new IllegalArgumentException("Type " + table + " is not in the schema");
        JsonArray thisType = process(filePair.getValue(), def, format);
        if (partitioned)
            ans.add(table, thisType);
        else {
            for (JsonElement elem : thisType) {
                JsonObject toAdd = new JsonObject();
                JsonArray brands = new JsonArray();
                brands.add(new JsonPrimitive(table));
                toAdd.add("type", brands);
                toAdd.add("data", elem);
                world.add(toAdd);
            }
        }
    }
    return ans;
}