Search in sources :

Example 1 with JdbcDialect

use of org.apache.flink.connector.jdbc.dialect.JdbcDialect in project flink by apache.

the class JdbcOutputFormatBuilder method createBufferReduceExecutor.

private static JdbcBatchStatementExecutor<RowData> createBufferReduceExecutor(JdbcDmlOptions opt, RuntimeContext ctx, TypeInformation<RowData> rowDataTypeInfo, LogicalType[] fieldTypes) {
    checkArgument(opt.getKeyFields().isPresent());
    JdbcDialect dialect = opt.getDialect();
    String tableName = opt.getTableName();
    String[] pkNames = opt.getKeyFields().get();
    int[] pkFields = Arrays.stream(pkNames).mapToInt(Arrays.asList(opt.getFieldNames())::indexOf).toArray();
    LogicalType[] pkTypes = Arrays.stream(pkFields).mapToObj(f -> fieldTypes[f]).toArray(LogicalType[]::new);
    final TypeSerializer<RowData> typeSerializer = rowDataTypeInfo.createSerializer(ctx.getExecutionConfig());
    final Function<RowData, RowData> valueTransform = ctx.getExecutionConfig().isObjectReuseEnabled() ? typeSerializer::copy : Function.identity();
    return new TableBufferReducedStatementExecutor(createUpsertRowExecutor(dialect, tableName, opt.getFieldNames(), fieldTypes, pkFields, pkNames, pkTypes), createDeleteExecutor(dialect, tableName, pkNames, pkTypes), createRowKeyExtractor(fieldTypes, pkFields), valueTransform);
}
Also used : DataType(org.apache.flink.table.types.DataType) Arrays(java.util.Arrays) RuntimeContext(org.apache.flink.api.common.functions.RuntimeContext) JdbcExecutionOptions(org.apache.flink.connector.jdbc.JdbcExecutionOptions) JdbcDmlOptions(org.apache.flink.connector.jdbc.internal.options.JdbcDmlOptions) JdbcRowConverter(org.apache.flink.connector.jdbc.converter.JdbcRowConverter) RowType(org.apache.flink.table.types.logical.RowType) Function(java.util.function.Function) FieldNamedPreparedStatement(org.apache.flink.connector.jdbc.statement.FieldNamedPreparedStatement) RowData.createFieldGetter(org.apache.flink.table.data.RowData.createFieldGetter) TableSimpleStatementExecutor(org.apache.flink.connector.jdbc.internal.executor.TableSimpleStatementExecutor) GenericRowData(org.apache.flink.table.data.GenericRowData) JdbcBatchStatementExecutor(org.apache.flink.connector.jdbc.internal.executor.JdbcBatchStatementExecutor) TableBufferReducedStatementExecutor(org.apache.flink.connector.jdbc.internal.executor.TableBufferReducedStatementExecutor) TableBufferedStatementExecutor(org.apache.flink.connector.jdbc.internal.executor.TableBufferedStatementExecutor) SimpleJdbcConnectionProvider(org.apache.flink.connector.jdbc.internal.connection.SimpleJdbcConnectionProvider) Preconditions.checkNotNull(org.apache.flink.util.Preconditions.checkNotNull) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) TypeSerializer(org.apache.flink.api.common.typeutils.TypeSerializer) RowData(org.apache.flink.table.data.RowData) JdbcOutputFormat(org.apache.flink.connector.jdbc.internal.JdbcOutputFormat) JdbcConnectorOptions(org.apache.flink.connector.jdbc.internal.options.JdbcConnectorOptions) Serializable(java.io.Serializable) LogicalType(org.apache.flink.table.types.logical.LogicalType) Preconditions.checkArgument(org.apache.flink.util.Preconditions.checkArgument) TableInsertOrUpdateStatementExecutor(org.apache.flink.connector.jdbc.internal.executor.TableInsertOrUpdateStatementExecutor) JdbcDialect(org.apache.flink.connector.jdbc.dialect.JdbcDialect) GenericRowData(org.apache.flink.table.data.GenericRowData) RowData(org.apache.flink.table.data.RowData) LogicalType(org.apache.flink.table.types.logical.LogicalType) JdbcDialect(org.apache.flink.connector.jdbc.dialect.JdbcDialect) TableBufferReducedStatementExecutor(org.apache.flink.connector.jdbc.internal.executor.TableBufferReducedStatementExecutor)

Example 2 with JdbcDialect

use of org.apache.flink.connector.jdbc.dialect.JdbcDialect in project flink by apache.

the class JdbcDynamicTableSource method getScanRuntimeProvider.

@Override
public ScanRuntimeProvider getScanRuntimeProvider(ScanContext runtimeProviderContext) {
    final JdbcRowDataInputFormat.Builder builder = JdbcRowDataInputFormat.builder().setDrivername(options.getDriverName()).setDBUrl(options.getDbURL()).setUsername(options.getUsername().orElse(null)).setPassword(options.getPassword().orElse(null)).setAutoCommit(readOptions.getAutoCommit());
    if (readOptions.getFetchSize() != 0) {
        builder.setFetchSize(readOptions.getFetchSize());
    }
    final JdbcDialect dialect = options.getDialect();
    String query = dialect.getSelectFromStatement(options.getTableName(), DataType.getFieldNames(physicalRowDataType).toArray(new String[0]), new String[0]);
    if (readOptions.getPartitionColumnName().isPresent()) {
        long lowerBound = readOptions.getPartitionLowerBound().get();
        long upperBound = readOptions.getPartitionUpperBound().get();
        int numPartitions = readOptions.getNumPartitions().get();
        builder.setParametersProvider(new JdbcNumericBetweenParametersProvider(lowerBound, upperBound).ofBatchNum(numPartitions));
        query += " WHERE " + dialect.quoteIdentifier(readOptions.getPartitionColumnName().get()) + " BETWEEN ? AND ?";
    }
    if (limit >= 0) {
        query = String.format("%s %s", query, dialect.getLimitClause(limit));
    }
    builder.setQuery(query);
    final RowType rowType = (RowType) physicalRowDataType.getLogicalType();
    builder.setRowConverter(dialect.getRowConverter(rowType));
    builder.setRowDataTypeInfo(runtimeProviderContext.createTypeInformation(physicalRowDataType));
    return InputFormatProvider.of(builder.build());
}
Also used : RowType(org.apache.flink.table.types.logical.RowType) JdbcDialect(org.apache.flink.connector.jdbc.dialect.JdbcDialect) JdbcNumericBetweenParametersProvider(org.apache.flink.connector.jdbc.split.JdbcNumericBetweenParametersProvider)

Example 3 with JdbcDialect

use of org.apache.flink.connector.jdbc.dialect.JdbcDialect in project flink by apache.

the class JdbcDynamicTableFactory method validateDataTypeWithJdbcDialect.

private static void validateDataTypeWithJdbcDialect(DataType dataType, String url) {
    final JdbcDialect dialect = JdbcDialectLoader.load(url);
    dialect.validate((RowType) dataType.getLogicalType());
}
Also used : JdbcDialect(org.apache.flink.connector.jdbc.dialect.JdbcDialect)

Aggregations

JdbcDialect (org.apache.flink.connector.jdbc.dialect.JdbcDialect)3 RowType (org.apache.flink.table.types.logical.RowType)2 Serializable (java.io.Serializable)1 Arrays (java.util.Arrays)1 Function (java.util.function.Function)1 RuntimeContext (org.apache.flink.api.common.functions.RuntimeContext)1 TypeInformation (org.apache.flink.api.common.typeinfo.TypeInformation)1 TypeSerializer (org.apache.flink.api.common.typeutils.TypeSerializer)1 JdbcExecutionOptions (org.apache.flink.connector.jdbc.JdbcExecutionOptions)1 JdbcRowConverter (org.apache.flink.connector.jdbc.converter.JdbcRowConverter)1 JdbcOutputFormat (org.apache.flink.connector.jdbc.internal.JdbcOutputFormat)1 SimpleJdbcConnectionProvider (org.apache.flink.connector.jdbc.internal.connection.SimpleJdbcConnectionProvider)1 JdbcBatchStatementExecutor (org.apache.flink.connector.jdbc.internal.executor.JdbcBatchStatementExecutor)1 TableBufferReducedStatementExecutor (org.apache.flink.connector.jdbc.internal.executor.TableBufferReducedStatementExecutor)1 TableBufferedStatementExecutor (org.apache.flink.connector.jdbc.internal.executor.TableBufferedStatementExecutor)1 TableInsertOrUpdateStatementExecutor (org.apache.flink.connector.jdbc.internal.executor.TableInsertOrUpdateStatementExecutor)1 TableSimpleStatementExecutor (org.apache.flink.connector.jdbc.internal.executor.TableSimpleStatementExecutor)1 JdbcConnectorOptions (org.apache.flink.connector.jdbc.internal.options.JdbcConnectorOptions)1 JdbcDmlOptions (org.apache.flink.connector.jdbc.internal.options.JdbcDmlOptions)1 JdbcNumericBetweenParametersProvider (org.apache.flink.connector.jdbc.split.JdbcNumericBetweenParametersProvider)1