Search in sources :

Example 1 with InvalidTableException

use of org.apache.beam.sdk.extensions.sql.meta.provider.InvalidTableException in project beam by apache.

the class BigtableTableCreationFailuresTest method testShouldFailOnIncorrectLocation.

@Test
public void testShouldFailOnIncorrectLocation() {
    String createTable = "CREATE EXTERNAL TABLE fail(key VARCHAR, q BIGINT) \n" + "TYPE bigtable \n" + "LOCATION 'googleapis.com/incorrect/projects/fakeProject/instances/fakeInstance/tables/beamTable' \n" + "TBLPROPERTIES '{\"columnsMapping\": \"f:q\"}'";
    cli.execute(createTable);
    Table table = metaStore.getTables().get("fail");
    InvalidTableException e = assertThrows(InvalidTableException.class, () -> tableProvider.buildBeamSqlTable(table));
    checkMessage(e.getMessage(), "Bigtable location must be in the following format:");
}
Also used : Table(org.apache.beam.sdk.extensions.sql.meta.Table) InvalidTableException(org.apache.beam.sdk.extensions.sql.meta.provider.InvalidTableException) Test(org.junit.Test)

Example 2 with InvalidTableException

use of org.apache.beam.sdk.extensions.sql.meta.provider.InvalidTableException in project beam by apache.

the class TextTableProvider method buildBeamSqlTable.

@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");
    String deadLetterFile = properties.getString("deadLetterFile");
    // 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 String legacyCsvFormat = null;
    if (!ImmutableSet.of("csv", "lines", "json").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 "json":
            return new TextJsonTable(schema, filePattern, JsonToRow.create(schema, deadLetterFile), RowToJson.create());
        case "lines":
            if (!(schema.getFieldCount() == 1 && schema.getField(0).getType().getTypeName().equals(TypeName.STRING))) {
                throw new InvalidTableException("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 InvalidTableException("Table with type 'text' must have format 'csv' or 'lines' or 'json'");
    }
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) InvalidTableException(org.apache.beam.sdk.extensions.sql.meta.provider.InvalidTableException) Schema(org.apache.beam.sdk.schemas.Schema) CSVFormat(org.apache.commons.csv.CSVFormat) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Aggregations

InvalidTableException (org.apache.beam.sdk.extensions.sql.meta.provider.InvalidTableException)2 JSONObject (com.alibaba.fastjson.JSONObject)1 Table (org.apache.beam.sdk.extensions.sql.meta.Table)1 Schema (org.apache.beam.sdk.schemas.Schema)1 CSVFormat (org.apache.commons.csv.CSVFormat)1 Nullable (org.checkerframework.checker.nullness.qual.Nullable)1 Test (org.junit.Test)1