Search in sources :

Example 1 with JdbcNumericBetweenParametersProvider

use of org.apache.flink.connector.jdbc.split.JdbcNumericBetweenParametersProvider 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 2 with JdbcNumericBetweenParametersProvider

use of org.apache.flink.connector.jdbc.split.JdbcNumericBetweenParametersProvider in project flink by apache.

the class JdbcRowDataInputFormatTest method testJdbcInputFormatWithoutParallelismAndNumericColumnSplitting.

@Test
public void testJdbcInputFormatWithoutParallelismAndNumericColumnSplitting() throws IOException {
    final long min = TEST_DATA[0].id;
    final long max = TEST_DATA[TEST_DATA.length - 1].id;
    // generate a single split
    final long fetchSize = max + 1;
    JdbcParameterValuesProvider pramProvider = new JdbcNumericBetweenParametersProvider(min, max).ofBatchSize(fetchSize);
    inputFormat = JdbcRowDataInputFormat.builder().setDrivername(DERBY_EBOOKSHOP_DB.getDriverClass()).setDBUrl(DERBY_EBOOKSHOP_DB.getUrl()).setQuery(SELECT_ALL_BOOKS_SPLIT_BY_ID).setParametersProvider(pramProvider).setResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE).setRowConverter(dialect.getRowConverter(rowType)).build();
    inputFormat.openInputFormat();
    InputSplit[] splits = inputFormat.createInputSplits(1);
    // assert that a single split was generated
    Assert.assertEquals(1, splits.length);
    int recordCount = 0;
    RowData row = new GenericRowData(5);
    for (InputSplit split : splits) {
        inputFormat.open(split);
        while (!inputFormat.reachedEnd()) {
            RowData next = inputFormat.nextRecord(row);
            assertEquals(TEST_DATA[recordCount], next);
            recordCount++;
        }
        inputFormat.close();
    }
    inputFormat.closeInputFormat();
    Assert.assertEquals(TEST_DATA.length, recordCount);
}
Also used : GenericRowData(org.apache.flink.table.data.GenericRowData) RowData(org.apache.flink.table.data.RowData) GenericRowData(org.apache.flink.table.data.GenericRowData) JdbcNumericBetweenParametersProvider(org.apache.flink.connector.jdbc.split.JdbcNumericBetweenParametersProvider) InputSplit(org.apache.flink.core.io.InputSplit) JdbcParameterValuesProvider(org.apache.flink.connector.jdbc.split.JdbcParameterValuesProvider) Test(org.junit.Test)

Example 3 with JdbcNumericBetweenParametersProvider

use of org.apache.flink.connector.jdbc.split.JdbcNumericBetweenParametersProvider in project flink by apache.

the class JdbcInputFormatTest method testJdbcInputFormatWithParallelismAndNumericColumnSplitting.

@Test
public void testJdbcInputFormatWithParallelismAndNumericColumnSplitting() throws IOException {
    final int fetchSize = 1;
    final long min = TEST_DATA[0].id;
    final long max = TEST_DATA[TEST_DATA.length - fetchSize].id;
    JdbcParameterValuesProvider pramProvider = new JdbcNumericBetweenParametersProvider(min, max).ofBatchSize(fetchSize);
    jdbcInputFormat = JdbcInputFormat.buildJdbcInputFormat().setDrivername(DERBY_EBOOKSHOP_DB.getDriverClass()).setDBUrl(DERBY_EBOOKSHOP_DB.getUrl()).setQuery(SELECT_ALL_BOOKS_SPLIT_BY_ID).setRowTypeInfo(ROW_TYPE_INFO).setParametersProvider(pramProvider).setResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE).finish();
    jdbcInputFormat.openInputFormat();
    InputSplit[] splits = jdbcInputFormat.createInputSplits(1);
    // this query exploit parallelism (1 split for every id)
    Assert.assertEquals(TEST_DATA.length, splits.length);
    int recordCount = 0;
    Row row = new Row(5);
    for (InputSplit split : splits) {
        jdbcInputFormat.open(split);
        while (!jdbcInputFormat.reachedEnd()) {
            Row next = jdbcInputFormat.nextRecord(row);
            assertEquals(TEST_DATA[recordCount], next);
            recordCount++;
        }
        jdbcInputFormat.close();
    }
    jdbcInputFormat.closeInputFormat();
    Assert.assertEquals(TEST_DATA.length, recordCount);
}
Also used : Row(org.apache.flink.types.Row) JdbcNumericBetweenParametersProvider(org.apache.flink.connector.jdbc.split.JdbcNumericBetweenParametersProvider) InputSplit(org.apache.flink.core.io.InputSplit) JdbcParameterValuesProvider(org.apache.flink.connector.jdbc.split.JdbcParameterValuesProvider) Test(org.junit.Test)

Example 4 with JdbcNumericBetweenParametersProvider

use of org.apache.flink.connector.jdbc.split.JdbcNumericBetweenParametersProvider in project flink by apache.

the class JdbcRowDataInputFormatTest method testJdbcInputFormatWithParallelismAndNumericColumnSplitting.

@Test
public void testJdbcInputFormatWithParallelismAndNumericColumnSplitting() throws IOException {
    final int fetchSize = 1;
    final long min = TEST_DATA[0].id;
    final long max = TEST_DATA[TEST_DATA.length - fetchSize].id;
    JdbcParameterValuesProvider pramProvider = new JdbcNumericBetweenParametersProvider(min, max).ofBatchSize(fetchSize);
    inputFormat = JdbcRowDataInputFormat.builder().setDrivername(DERBY_EBOOKSHOP_DB.getDriverClass()).setDBUrl(DERBY_EBOOKSHOP_DB.getUrl()).setQuery(SELECT_ALL_BOOKS_SPLIT_BY_ID).setParametersProvider(pramProvider).setResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE).setRowConverter(dialect.getRowConverter(rowType)).build();
    inputFormat.openInputFormat();
    InputSplit[] splits = inputFormat.createInputSplits(1);
    // this query exploit parallelism (1 split for every id)
    Assert.assertEquals(TEST_DATA.length, splits.length);
    int recordCount = 0;
    RowData row = new GenericRowData(5);
    for (InputSplit split : splits) {
        inputFormat.open(split);
        while (!inputFormat.reachedEnd()) {
            RowData next = inputFormat.nextRecord(row);
            assertEquals(TEST_DATA[recordCount], next);
            recordCount++;
        }
        inputFormat.close();
    }
    inputFormat.closeInputFormat();
    Assert.assertEquals(TEST_DATA.length, recordCount);
}
Also used : GenericRowData(org.apache.flink.table.data.GenericRowData) RowData(org.apache.flink.table.data.RowData) GenericRowData(org.apache.flink.table.data.GenericRowData) JdbcNumericBetweenParametersProvider(org.apache.flink.connector.jdbc.split.JdbcNumericBetweenParametersProvider) InputSplit(org.apache.flink.core.io.InputSplit) JdbcParameterValuesProvider(org.apache.flink.connector.jdbc.split.JdbcParameterValuesProvider) Test(org.junit.Test)

Example 5 with JdbcNumericBetweenParametersProvider

use of org.apache.flink.connector.jdbc.split.JdbcNumericBetweenParametersProvider in project flink by apache.

the class JdbcFullTest method runTest.

private void runTest(boolean exploitParallelism) throws Exception {
    ExecutionEnvironment environment = ExecutionEnvironment.getExecutionEnvironment();
    JdbcInputFormat.JdbcInputFormatBuilder inputBuilder = JdbcInputFormat.buildJdbcInputFormat().setDrivername(getDbMetadata().getDriverClass()).setDBUrl(getDbMetadata().getUrl()).setQuery(SELECT_ALL_BOOKS).setRowTypeInfo(ROW_TYPE_INFO);
    if (exploitParallelism) {
        final int fetchSize = 1;
        final long min = TEST_DATA[0].id;
        final long max = TEST_DATA[TEST_DATA.length - fetchSize].id;
        // use a "splittable" query to exploit parallelism
        inputBuilder = inputBuilder.setQuery(SELECT_ALL_BOOKS_SPLIT_BY_ID).setParametersProvider(new JdbcNumericBetweenParametersProvider(min, max).ofBatchSize(fetchSize));
    }
    DataSet<Row> source = environment.createInput(inputBuilder.finish());
    // NOTE: in this case (with Derby driver) setSqlTypes could be skipped, but
    // some databases don't null values correctly when no column type was specified
    // in PreparedStatement.setObject (see its javadoc for more details)
    JdbcConnectionOptions connectionOptions = new JdbcConnectionOptions.JdbcConnectionOptionsBuilder().withUrl(getDbMetadata().getUrl()).withDriverName(getDbMetadata().getDriverClass()).build();
    JdbcOutputFormat jdbcOutputFormat = new JdbcOutputFormat<>(new SimpleJdbcConnectionProvider(connectionOptions), JdbcExecutionOptions.defaults(), ctx -> createSimpleRowExecutor(String.format(INSERT_TEMPLATE, OUTPUT_TABLE), new int[] { Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.DOUBLE, Types.INTEGER }, ctx.getExecutionConfig().isObjectReuseEnabled()), JdbcOutputFormat.RecordExtractor.identity());
    source.output(jdbcOutputFormat);
    environment.execute();
    try (Connection dbConn = DriverManager.getConnection(getDbMetadata().getUrl());
        PreparedStatement statement = dbConn.prepareStatement(SELECT_ALL_NEWBOOKS);
        ResultSet resultSet = statement.executeQuery()) {
        int count = 0;
        while (resultSet.next()) {
            count++;
        }
        Assert.assertEquals(TEST_DATA.length, count);
    }
}
Also used : ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) JdbcInputFormat(org.apache.flink.connector.jdbc.JdbcInputFormat) Connection(java.sql.Connection) JdbcConnectionOptions(org.apache.flink.connector.jdbc.JdbcConnectionOptions) PreparedStatement(java.sql.PreparedStatement) JdbcNumericBetweenParametersProvider(org.apache.flink.connector.jdbc.split.JdbcNumericBetweenParametersProvider) SimpleJdbcConnectionProvider(org.apache.flink.connector.jdbc.internal.connection.SimpleJdbcConnectionProvider) ResultSet(java.sql.ResultSet) Row(org.apache.flink.types.Row)

Aggregations

JdbcNumericBetweenParametersProvider (org.apache.flink.connector.jdbc.split.JdbcNumericBetweenParametersProvider)6 JdbcParameterValuesProvider (org.apache.flink.connector.jdbc.split.JdbcParameterValuesProvider)4 InputSplit (org.apache.flink.core.io.InputSplit)4 Test (org.junit.Test)4 Row (org.apache.flink.types.Row)3 GenericRowData (org.apache.flink.table.data.GenericRowData)2 RowData (org.apache.flink.table.data.RowData)2 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 ExecutionEnvironment (org.apache.flink.api.java.ExecutionEnvironment)1 JdbcConnectionOptions (org.apache.flink.connector.jdbc.JdbcConnectionOptions)1 JdbcInputFormat (org.apache.flink.connector.jdbc.JdbcInputFormat)1 JdbcDialect (org.apache.flink.connector.jdbc.dialect.JdbcDialect)1 SimpleJdbcConnectionProvider (org.apache.flink.connector.jdbc.internal.connection.SimpleJdbcConnectionProvider)1 RowType (org.apache.flink.table.types.logical.RowType)1