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

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

Introduction

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

Prototype

CSVFormat DEFAULT

To view the source code for org.apache.commons.csv CSVFormat DEFAULT.

Click Source Link

Document

Standard comma separated format, as for #RFC4180 but allowing empty lines.

Usage

From source file:org.apache.beam.sdk.extensions.sql.meta.provider.text.BeamTextCSVTable.java

/** CSV table with {@link CSVFormat#DEFAULT DEFAULT} format. */
public BeamTextCSVTable(Schema beamSchema, String filePattern) {
    this(beamSchema, filePattern, CSVFormat.DEFAULT);
}

From source file:org.apache.beam.sdk.extensions.sql.meta.provider.text.TextTableProvider.java

@Override
public BeamSqlTable buildBeamSqlTable(Table table) {
    Schema schema = table.getSchema();

    String filePattern = table.getLocation();
    JSONObject properties = table.getProperties();
    String format = MoreObjects.firstNonNull(properties.getString("format"), "csv");

    // Backwards compatibility: previously "type": "text" meant CSV and "format" was where the
    // CSV format went. So assume that any other format is the CSV format.
    @Nullable//from   w w w . java2 s . c om
    String legacyCsvFormat = null;
    if (!ImmutableSet.of("csv", "lines").contains(format)) {
        legacyCsvFormat = format;
        format = "csv";
    }

    switch (format) {
    case "csv":
        String specifiedCsvFormat = properties.getString("csvformat");
        CSVFormat csvFormat = specifiedCsvFormat != null ? CSVFormat.valueOf(specifiedCsvFormat)
                : (legacyCsvFormat != null ? CSVFormat.valueOf(legacyCsvFormat) : CSVFormat.DEFAULT);
        return new TextTable(schema, filePattern, new CsvToRow(schema, csvFormat), new RowToCsv(csvFormat));
    case "lines":
        checkArgument(
                schema.getFieldCount() == 1
                        && schema.getField(0).getType().getTypeName().equals(TypeName.STRING),
                "Table with type 'text' and format 'lines' "
                        + "must have exactly one STRING/VARCHAR/CHAR column ");
        return new TextTable(schema, filePattern, new LinesReadConverter(), new LinesWriteConverter());
    default:
        throw new IllegalArgumentException("Table with type 'text' must have format 'csv' or 'lines'");
    }
}

From source file:org.apache.beam.sdk.extensions.sql.meta.provider.text.TextTableProviderTest.java

@Test
public void testBuildBeamSqlTable() throws Exception {
    Table table = mockTable("hello", null);
    BeamSqlTable sqlTable = provider.buildBeamSqlTable(table);

    assertNotNull(sqlTable);/*from   w w w  .  ja  v  a2  s . c o  m*/
    assertTrue(sqlTable instanceof BeamTextCSVTable);

    BeamTextCSVTable csvTable = (BeamTextCSVTable) sqlTable;
    assertEquals(CSVFormat.DEFAULT, csvTable.getCsvFormat());
    assertEquals("/home/admin/hello", csvTable.getFilePattern());
}

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

@Test
public void shouldUseDefaultFormat() {
    CsvDataFormat dataFormat = new CsvDataFormat();

    // Properly initialized
    assertSame(CSVFormat.DEFAULT, dataFormat.getFormat());

    // Properly used
    assertEquals(CSVFormat.DEFAULT, dataFormat.getActiveFormat());
}

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

@Test
public void shouldFallbackToDefaultFormat() {
    CsvDataFormat dataFormat = new CsvDataFormat(CSVFormat.EXCEL).setFormat(null);

    // Properly saved
    assertSame(CSVFormat.DEFAULT, dataFormat.getFormat());

    // Properly used
    assertEquals(CSVFormat.DEFAULT, dataFormat.getActiveFormat());
}

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

@Test
public void shouldDisableCommentMarker() {
    CsvDataFormat dataFormat = new CsvDataFormat().setCommentMarkerDisabled(true).setCommentMarker('c');

    // Properly saved
    assertSame(CSVFormat.DEFAULT, dataFormat.getFormat());
    assertTrue(dataFormat.isCommentMarkerDisabled());
    assertEquals(Character.valueOf('c'), dataFormat.getCommentMarker());

    // Properly used
    assertNull(dataFormat.getActiveFormat().getCommentMarker());
}

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

@Test
public void shouldOverrideCommentMarker() {
    CsvDataFormat dataFormat = new CsvDataFormat().setCommentMarker('c');

    // Properly saved
    assertSame(CSVFormat.DEFAULT, dataFormat.getFormat());
    assertEquals(Character.valueOf('c'), dataFormat.getCommentMarker());

    // Properly used
    assertEquals(Character.valueOf('c'), dataFormat.getActiveFormat().getCommentMarker());
}

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

@Test
public void shouldOverrideDelimiter() {
    CsvDataFormat dataFormat = new CsvDataFormat().setDelimiter('d');

    // Properly saved
    assertSame(CSVFormat.DEFAULT, dataFormat.getFormat());
    assertEquals(Character.valueOf('d'), dataFormat.getDelimiter());

    // Properly used
    assertEquals('d', dataFormat.getActiveFormat().getDelimiter());
}

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

@Test
public void shouldDisableEscape() {
    CsvDataFormat dataFormat = new CsvDataFormat().setEscapeDisabled(true).setEscape('e');

    // Properly saved
    assertSame(CSVFormat.DEFAULT, dataFormat.getFormat());
    assertTrue(dataFormat.isEscapeDisabled());
    assertEquals(Character.valueOf('e'), dataFormat.getEscape());

    // Properly used
    assertNull(dataFormat.getActiveFormat().getEscapeCharacter());
}

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

@Test
public void shouldOverrideEscape() {
    CsvDataFormat dataFormat = new CsvDataFormat().setEscape('e');

    // Properly saved
    assertSame(CSVFormat.DEFAULT, dataFormat.getFormat());
    assertEquals(Character.valueOf('e'), dataFormat.getEscape());

    // Properly used
    assertEquals(Character.valueOf('e'), dataFormat.getActiveFormat().getEscapeCharacter());
}