Search in sources :

Example 1 with CONNECTOR_PATH

use of org.apache.flink.table.descriptors.FileSystemValidator.CONNECTOR_PATH in project flink by apache.

the class CsvTableSinkFactoryBase method createTableSink.

protected CsvTableSink createTableSink(Boolean isStreaming, Map<String, String> properties) {
    DescriptorProperties params = new DescriptorProperties();
    params.putProperties(properties);
    // validate
    new FileSystemValidator().validate(params);
    new OldCsvValidator().validate(params);
    new SchemaValidator(isStreaming, false, false).validate(params);
    // build
    TableSchema tableSchema = TableSchemaUtils.getPhysicalSchema(params.getTableSchema(SCHEMA));
    // if a schema is defined, no matter derive schema is set or not, will use the defined
    // schema
    final boolean hasSchema = params.hasPrefix(FORMAT_FIELDS);
    if (hasSchema) {
        TableSchema formatSchema = params.getTableSchema(FORMAT_FIELDS);
        if (!getFieldLogicalTypes(formatSchema).equals(getFieldLogicalTypes(tableSchema))) {
            throw new TableException(String.format("Encodings that differ from the schema are not supported yet for" + " CsvTableSink, format schema is '%s', but table schema is '%s'.", formatSchema, tableSchema));
        }
    }
    String path = params.getString(CONNECTOR_PATH);
    String fieldDelimiter = params.getOptionalString(FORMAT_FIELD_DELIMITER).orElse(",");
    Optional<String> writeModeParam = params.getOptionalString(FORMAT_WRITE_MODE);
    FileSystem.WriteMode writeMode = (writeModeParam.isPresent()) ? FileSystem.WriteMode.valueOf(writeModeParam.get()) : null;
    int numFiles = params.getOptionalInt(FORMAT_NUM_FILES).orElse(-1);
    // bridge to java.sql.Timestamp/Time/Date
    DataType[] dataTypes = Arrays.stream(tableSchema.getFieldDataTypes()).map(dt -> {
        switch(dt.getLogicalType().getTypeRoot()) {
            case TIMESTAMP_WITHOUT_TIME_ZONE:
                return dt.bridgedTo(Timestamp.class);
            case TIME_WITHOUT_TIME_ZONE:
                return dt.bridgedTo(Time.class);
            case DATE:
                return dt.bridgedTo(Date.class);
            default:
                return dt;
        }
    }).toArray(DataType[]::new);
    return new CsvTableSink(path, fieldDelimiter, numFiles, writeMode, tableSchema.getFieldNames(), dataTypes);
}
Also used : DataType(org.apache.flink.table.types.DataType) FORMAT_FIELDS(org.apache.flink.table.descriptors.OldCsvValidator.FORMAT_FIELDS) Arrays(java.util.Arrays) CONNECTOR_PROPERTY_VERSION(org.apache.flink.table.descriptors.ConnectorDescriptorValidator.CONNECTOR_PROPERTY_VERSION) TableFactoryService(org.apache.flink.table.factories.TableFactoryService) Time(java.sql.Time) CONNECTOR_PATH(org.apache.flink.table.descriptors.FileSystemValidator.CONNECTOR_PATH) FileSystemValidator(org.apache.flink.table.descriptors.FileSystemValidator) HashMap(java.util.HashMap) FORMAT_NUM_FILES(org.apache.flink.table.descriptors.OldCsvValidator.FORMAT_NUM_FILES) FORMAT_WRITE_MODE(org.apache.flink.table.descriptors.OldCsvValidator.FORMAT_WRITE_MODE) ArrayList(java.util.ArrayList) FORMAT_FIELD_DELIMITER(org.apache.flink.table.descriptors.OldCsvValidator.FORMAT_FIELD_DELIMITER) CONNECTOR_TYPE(org.apache.flink.table.descriptors.ConnectorDescriptorValidator.CONNECTOR_TYPE) Map(java.util.Map) SchemaValidator(org.apache.flink.table.descriptors.SchemaValidator) TableFactory(org.apache.flink.table.factories.TableFactory) CsvTableSourceFactoryBase.getFieldLogicalTypes(org.apache.flink.table.sources.CsvTableSourceFactoryBase.getFieldLogicalTypes) CONNECTOR_TYPE_VALUE(org.apache.flink.table.descriptors.FileSystemValidator.CONNECTOR_TYPE_VALUE) SCHEMA(org.apache.flink.table.descriptors.Schema.SCHEMA) Timestamp(java.sql.Timestamp) COMMENT(org.apache.flink.table.descriptors.DescriptorProperties.COMMENT) TableException(org.apache.flink.table.api.TableException) TableSchema(org.apache.flink.table.api.TableSchema) Date(java.sql.Date) OldCsvValidator(org.apache.flink.table.descriptors.OldCsvValidator) List(java.util.List) FileSystem(org.apache.flink.core.fs.FileSystem) DescriptorProperties(org.apache.flink.table.descriptors.DescriptorProperties) Optional(java.util.Optional) Internal(org.apache.flink.annotation.Internal) TableSchemaUtils(org.apache.flink.table.utils.TableSchemaUtils) FORMAT_TYPE_VALUE(org.apache.flink.table.descriptors.OldCsvValidator.FORMAT_TYPE_VALUE) TableException(org.apache.flink.table.api.TableException) TableSchema(org.apache.flink.table.api.TableSchema) DescriptorProperties(org.apache.flink.table.descriptors.DescriptorProperties) OldCsvValidator(org.apache.flink.table.descriptors.OldCsvValidator) FileSystemValidator(org.apache.flink.table.descriptors.FileSystemValidator) SchemaValidator(org.apache.flink.table.descriptors.SchemaValidator) Time(java.sql.Time) Timestamp(java.sql.Timestamp) Date(java.sql.Date) FileSystem(org.apache.flink.core.fs.FileSystem) DataType(org.apache.flink.table.types.DataType)

Aggregations

Date (java.sql.Date)1 Time (java.sql.Time)1 Timestamp (java.sql.Timestamp)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Internal (org.apache.flink.annotation.Internal)1 FileSystem (org.apache.flink.core.fs.FileSystem)1 TableException (org.apache.flink.table.api.TableException)1 TableSchema (org.apache.flink.table.api.TableSchema)1 CONNECTOR_PROPERTY_VERSION (org.apache.flink.table.descriptors.ConnectorDescriptorValidator.CONNECTOR_PROPERTY_VERSION)1 CONNECTOR_TYPE (org.apache.flink.table.descriptors.ConnectorDescriptorValidator.CONNECTOR_TYPE)1 DescriptorProperties (org.apache.flink.table.descriptors.DescriptorProperties)1 COMMENT (org.apache.flink.table.descriptors.DescriptorProperties.COMMENT)1 FileSystemValidator (org.apache.flink.table.descriptors.FileSystemValidator)1 CONNECTOR_PATH (org.apache.flink.table.descriptors.FileSystemValidator.CONNECTOR_PATH)1 CONNECTOR_TYPE_VALUE (org.apache.flink.table.descriptors.FileSystemValidator.CONNECTOR_TYPE_VALUE)1