Search in sources :

Example 36 with TableSchema

use of org.apache.flink.table.api.TableSchema in project flink by apache.

the class TestValuesTableFactory method validateAndExtractRowtimeIndex.

private static int validateAndExtractRowtimeIndex(CatalogTable sinkTable, boolean dropLateEvent, boolean isInsertOnly) {
    if (!dropLateEvent) {
        return -1;
    } else if (!isInsertOnly) {
        throw new ValidationException("Option 'sink.drop-late-event' only works for insert-only sink now.");
    }
    TableSchema schema = sinkTable.getSchema();
    List<WatermarkSpec> watermarkSpecs = schema.getWatermarkSpecs();
    if (watermarkSpecs.size() == 0) {
        throw new ValidationException("Please define the watermark in the schema that is used to indicate the rowtime column. " + "The sink function will compare the rowtime and the current watermark to determine whether the event is late.");
    }
    String rowtimeName = watermarkSpecs.get(0).getRowtimeAttribute();
    return Arrays.asList(schema.getFieldNames()).indexOf(rowtimeName);
}
Also used : ValidationException(org.apache.flink.table.api.ValidationException) TableSchema(org.apache.flink.table.api.TableSchema) WatermarkSpec(org.apache.flink.table.api.WatermarkSpec)

Example 37 with TableSchema

use of org.apache.flink.table.api.TableSchema in project flink by apache.

the class OperationConverterUtils method convertChangeColumn.

public static Operation convertChangeColumn(ObjectIdentifier tableIdentifier, SqlChangeColumn changeColumn, CatalogTable catalogTable, SqlValidator sqlValidator) {
    String oldName = changeColumn.getOldName().getSimple();
    if (catalogTable.getPartitionKeys().indexOf(oldName) >= 0) {
        // disallow changing partition columns
        throw new ValidationException("CHANGE COLUMN cannot be applied to partition columns");
    }
    TableSchema oldSchema = catalogTable.getSchema();
    boolean first = changeColumn.isFirst();
    String after = changeColumn.getAfter() == null ? null : changeColumn.getAfter().getSimple();
    TableColumn newTableColumn = toTableColumn(changeColumn.getNewColumn(), sqlValidator);
    TableSchema newSchema = changeColumn(oldSchema, oldName, newTableColumn, first, after);
    Map<String, String> newProperties = new HashMap<>(catalogTable.getOptions());
    newProperties.putAll(extractProperties(changeColumn.getProperties()));
    return new AlterTableSchemaOperation(tableIdentifier, new CatalogTableImpl(newSchema, catalogTable.getPartitionKeys(), newProperties, catalogTable.getComment()));
// TODO: handle watermark and constraints
}
Also used : ValidationException(org.apache.flink.table.api.ValidationException) TableSchema(org.apache.flink.table.api.TableSchema) HashMap(java.util.HashMap) CatalogTableImpl(org.apache.flink.table.catalog.CatalogTableImpl) AlterTableSchemaOperation(org.apache.flink.table.operations.ddl.AlterTableSchemaOperation) TableColumn(org.apache.flink.table.api.TableColumn) SqlTableColumn(org.apache.flink.sql.parser.ddl.SqlTableColumn)

Example 38 with TableSchema

use of org.apache.flink.table.api.TableSchema in project flink by apache.

the class HiveCatalogUdfITCase method testFlinkUdf.

@Test
public void testFlinkUdf() throws Exception {
    final TableSchema schema = TableSchema.builder().field("name", DataTypes.STRING()).field("age", DataTypes.INT()).build();
    final Map<String, String> sourceOptions = new HashMap<>();
    sourceOptions.put("connector.type", "filesystem");
    sourceOptions.put("connector.path", getClass().getResource("/csv/test.csv").getPath());
    sourceOptions.put("format.type", "csv");
    CatalogTable source = new CatalogTableImpl(schema, sourceOptions, "Comment.");
    hiveCatalog.createTable(new ObjectPath(HiveCatalog.DEFAULT_DB, sourceTableName), source, false);
    hiveCatalog.createFunction(new ObjectPath(HiveCatalog.DEFAULT_DB, "myudf"), new CatalogFunctionImpl(TestHiveSimpleUDF.class.getCanonicalName()), false);
    hiveCatalog.createFunction(new ObjectPath(HiveCatalog.DEFAULT_DB, "mygenericudf"), new CatalogFunctionImpl(TestHiveGenericUDF.class.getCanonicalName()), false);
    hiveCatalog.createFunction(new ObjectPath(HiveCatalog.DEFAULT_DB, "myudtf"), new CatalogFunctionImpl(TestHiveUDTF.class.getCanonicalName()), false);
    hiveCatalog.createFunction(new ObjectPath(HiveCatalog.DEFAULT_DB, "myudaf"), new CatalogFunctionImpl(GenericUDAFSum.class.getCanonicalName()), false);
    testUdf(true);
    testUdf(false);
}
Also used : ObjectPath(org.apache.flink.table.catalog.ObjectPath) TableSchema(org.apache.flink.table.api.TableSchema) HashMap(java.util.HashMap) CatalogTableImpl(org.apache.flink.table.catalog.CatalogTableImpl) CatalogTable(org.apache.flink.table.catalog.CatalogTable) CatalogFunctionImpl(org.apache.flink.table.catalog.CatalogFunctionImpl) Test(org.junit.Test)

Example 39 with TableSchema

use of org.apache.flink.table.api.TableSchema in project flink by apache.

the class HiveCatalogDataTypeTest method createCatalogTable.

private CatalogTable createCatalogTable(DataType[] types) {
    String[] colNames = new String[types.length];
    for (int i = 0; i < types.length; i++) {
        colNames[i] = String.format("%s_%d", types[i].toString().toLowerCase(), i);
    }
    TableSchema schema = TableSchema.builder().fields(colNames, types).build();
    return new CatalogTableImpl(schema, new HashMap<String, String>() {

        {
            put("is_streaming", "false");
            put(FactoryUtil.CONNECTOR.key(), SqlCreateHiveTable.IDENTIFIER);
        }
    }, "");
}
Also used : TableSchema(org.apache.flink.table.api.TableSchema) CatalogTableImpl(org.apache.flink.table.catalog.CatalogTableImpl)

Example 40 with TableSchema

use of org.apache.flink.table.api.TableSchema in project flink by apache.

the class HiveTableSource method getProducedTableSchema.

protected TableSchema getProducedTableSchema() {
    TableSchema fullSchema = getTableSchema();
    if (projectedFields == null) {
        return fullSchema;
    } else {
        String[] fullNames = fullSchema.getFieldNames();
        DataType[] fullTypes = fullSchema.getFieldDataTypes();
        return TableSchema.builder().fields(Arrays.stream(projectedFields).mapToObj(i -> fullNames[i]).toArray(String[]::new), Arrays.stream(projectedFields).mapToObj(i -> fullTypes[i]).toArray(DataType[]::new)).build();
    }
}
Also used : HivePartitionUtils(org.apache.flink.connectors.hive.util.HivePartitionUtils) DataType(org.apache.flink.table.types.DataType) Arrays(java.util.Arrays) ChangelogMode(org.apache.flink.table.connector.ChangelogMode) CatalogTable(org.apache.flink.table.catalog.CatalogTable) LocalDateTime(java.time.LocalDateTime) StringSerializer(org.apache.flink.api.common.typeutils.base.StringSerializer) DefaultPartTimeExtractor(org.apache.flink.connector.file.table.DefaultPartTimeExtractor) DataStreamSource(org.apache.flink.streaming.api.datastream.DataStreamSource) SupportsPartitionPushDown(org.apache.flink.table.connector.source.abilities.SupportsPartitionPushDown) ObjectPath(org.apache.flink.table.catalog.ObjectPath) SupportsProjectionPushDown(org.apache.flink.table.connector.source.abilities.SupportsProjectionPushDown) Partition(org.apache.hadoop.hive.metastore.api.Partition) ScanTableSource(org.apache.flink.table.connector.source.ScanTableSource) HiveShim(org.apache.flink.table.catalog.hive.client.HiveShim) DataStreamScanProvider(org.apache.flink.table.connector.source.DataStreamScanProvider) PARTITION_TIME_EXTRACTOR_TIMESTAMP_FORMATTER(org.apache.flink.connector.file.table.FileSystemConnectorOptions.PARTITION_TIME_EXTRACTOR_TIMESTAMP_FORMATTER) HiveContinuousPartitionContext(org.apache.flink.connectors.hive.read.HiveContinuousPartitionContext) ReadableConfig(org.apache.flink.configuration.ReadableConfig) Map(java.util.Map) HivePartitionFetcherContextBase(org.apache.flink.connectors.hive.read.HivePartitionFetcherContextBase) LongSerializer(org.apache.flink.api.common.typeutils.base.LongSerializer) Nullable(javax.annotation.Nullable) HiveShimLoader(org.apache.flink.table.catalog.hive.client.HiveShimLoader) HiveCatalogFactoryOptions(org.apache.flink.table.catalog.hive.factories.HiveCatalogFactoryOptions) HivePartitionUtils.getAllPartitions(org.apache.flink.connectors.hive.util.HivePartitionUtils.getAllPartitions) TypeSerializer(org.apache.flink.api.common.typeutils.TypeSerializer) DynamicTableSource(org.apache.flink.table.connector.source.DynamicTableSource) RowData(org.apache.flink.table.data.RowData) STREAMING_SOURCE_CONSUME_START_OFFSET(org.apache.flink.connectors.hive.HiveOptions.STREAMING_SOURCE_CONSUME_START_OFFSET) ProviderContext(org.apache.flink.table.connector.ProviderContext) TimestampData(org.apache.flink.table.data.TimestampData) SupportsLimitPushDown(org.apache.flink.table.connector.source.abilities.SupportsLimitPushDown) Configuration(org.apache.flink.configuration.Configuration) TException(org.apache.thrift.TException) WatermarkStrategy(org.apache.flink.api.common.eventtime.WatermarkStrategy) TableSchema(org.apache.flink.table.api.TableSchema) Preconditions(org.apache.flink.util.Preconditions) STREAMING_SOURCE_ENABLE(org.apache.flink.connectors.hive.HiveOptions.STREAMING_SOURCE_ENABLE) VisibleForTesting(org.apache.flink.annotation.VisibleForTesting) DataStream(org.apache.flink.streaming.api.datastream.DataStream) JobConf(org.apache.hadoop.mapred.JobConf) List(java.util.List) ContinuousPartitionFetcher(org.apache.flink.connector.file.table.ContinuousPartitionFetcher) Optional(java.util.Optional) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) TableSchema(org.apache.flink.table.api.TableSchema) DataType(org.apache.flink.table.types.DataType)

Aggregations

TableSchema (org.apache.flink.table.api.TableSchema)86 Test (org.junit.Test)54 HashMap (java.util.HashMap)26 CatalogTableImpl (org.apache.flink.table.catalog.CatalogTableImpl)21 SqlNode (org.apache.calcite.sql.SqlNode)19 ObjectPath (org.apache.flink.table.catalog.ObjectPath)19 CatalogTable (org.apache.flink.table.catalog.CatalogTable)18 DataType (org.apache.flink.table.types.DataType)16 ValidationException (org.apache.flink.table.api.ValidationException)14 TableColumn (org.apache.flink.table.api.TableColumn)10 UniqueConstraint (org.apache.flink.table.api.constraints.UniqueConstraint)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 Map (java.util.Map)9 FeatureOption (org.apache.flink.sql.parser.ddl.SqlTableLike.FeatureOption)9 MergingStrategy (org.apache.flink.sql.parser.ddl.SqlTableLike.MergingStrategy)9 CatalogBaseTable (org.apache.flink.table.catalog.CatalogBaseTable)8 ObjectIdentifier (org.apache.flink.table.catalog.ObjectIdentifier)8 Arrays (java.util.Arrays)7 Configuration (org.apache.flink.configuration.Configuration)7