Search in sources :

Example 1 with JdbcDmlOptions

use of org.apache.flink.connector.jdbc.internal.options.JdbcDmlOptions in project flink by apache.

the class JdbcDynamicTableFactoryTest method testJdbcCommonProperties.

@Test
public void testJdbcCommonProperties() {
    Map<String, String> properties = getAllOptions();
    properties.put("driver", "org.apache.derby.jdbc.EmbeddedDriver");
    properties.put("username", "user");
    properties.put("password", "pass");
    properties.put("connection.max-retry-timeout", "120s");
    // validation for source
    DynamicTableSource actualSource = createTableSource(SCHEMA, properties);
    JdbcConnectorOptions options = JdbcConnectorOptions.builder().setDBUrl("jdbc:derby:memory:mydb").setTableName("mytable").setDriverName("org.apache.derby.jdbc.EmbeddedDriver").setUsername("user").setPassword("pass").setConnectionCheckTimeoutSeconds(120).build();
    JdbcLookupOptions lookupOptions = JdbcLookupOptions.builder().setCacheMaxSize(-1).setCacheExpireMs(10_000).setMaxRetryTimes(3).build();
    JdbcDynamicTableSource expectedSource = new JdbcDynamicTableSource(options, JdbcReadOptions.builder().build(), lookupOptions, SCHEMA.toPhysicalRowDataType());
    assertEquals(expectedSource, actualSource);
    // validation for sink
    DynamicTableSink actualSink = createTableSink(SCHEMA, properties);
    // default flush configurations
    JdbcExecutionOptions executionOptions = JdbcExecutionOptions.builder().withBatchSize(100).withBatchIntervalMs(1000).withMaxRetries(3).build();
    JdbcDmlOptions dmlOptions = JdbcDmlOptions.builder().withTableName(options.getTableName()).withDialect(options.getDialect()).withFieldNames(SCHEMA.getColumnNames().toArray(new String[0])).withKeyFields("bbb", "aaa").build();
    JdbcDynamicTableSink expectedSink = new JdbcDynamicTableSink(options, executionOptions, dmlOptions, SCHEMA.toPhysicalRowDataType());
    assertEquals(expectedSink, actualSink);
}
Also used : JdbcExecutionOptions(org.apache.flink.connector.jdbc.JdbcExecutionOptions) JdbcLookupOptions(org.apache.flink.connector.jdbc.internal.options.JdbcLookupOptions) JdbcConnectorOptions(org.apache.flink.connector.jdbc.internal.options.JdbcConnectorOptions) JdbcDmlOptions(org.apache.flink.connector.jdbc.internal.options.JdbcDmlOptions) DynamicTableSink(org.apache.flink.table.connector.sink.DynamicTableSink) DynamicTableSource(org.apache.flink.table.connector.source.DynamicTableSource) Test(org.junit.Test)

Example 2 with JdbcDmlOptions

use of org.apache.flink.connector.jdbc.internal.options.JdbcDmlOptions in project flink by apache.

the class JdbcDynamicTableFactoryTest method testJDBCSinkWithParallelism.

@Test
public void testJDBCSinkWithParallelism() {
    Map<String, String> properties = getAllOptions();
    properties.put("sink.parallelism", "2");
    DynamicTableSink actual = createTableSink(SCHEMA, properties);
    JdbcConnectorOptions options = JdbcConnectorOptions.builder().setDBUrl("jdbc:derby:memory:mydb").setTableName("mytable").setParallelism(2).build();
    JdbcExecutionOptions executionOptions = JdbcExecutionOptions.builder().withBatchSize(100).withBatchIntervalMs(1000).withMaxRetries(3).build();
    JdbcDmlOptions dmlOptions = JdbcDmlOptions.builder().withTableName(options.getTableName()).withDialect(options.getDialect()).withFieldNames(SCHEMA.getColumnNames().toArray(new String[0])).withKeyFields("bbb", "aaa").build();
    JdbcDynamicTableSink expected = new JdbcDynamicTableSink(options, executionOptions, dmlOptions, SCHEMA.toPhysicalRowDataType());
    assertEquals(expected, actual);
}
Also used : JdbcExecutionOptions(org.apache.flink.connector.jdbc.JdbcExecutionOptions) JdbcConnectorOptions(org.apache.flink.connector.jdbc.internal.options.JdbcConnectorOptions) JdbcDmlOptions(org.apache.flink.connector.jdbc.internal.options.JdbcDmlOptions) DynamicTableSink(org.apache.flink.table.connector.sink.DynamicTableSink) Test(org.junit.Test)

Example 3 with JdbcDmlOptions

use of org.apache.flink.connector.jdbc.internal.options.JdbcDmlOptions in project flink by apache.

the class JdbcOutputFormatTest method testJdbcOutputFormat.

@Test
public void testJdbcOutputFormat() throws IOException, SQLException {
    JdbcConnectorOptions jdbcOptions = JdbcConnectorOptions.builder().setDriverName(DERBY_EBOOKSHOP_DB.getDriverClass()).setDBUrl(DERBY_EBOOKSHOP_DB.getUrl()).setTableName(OUTPUT_TABLE).build();
    JdbcDmlOptions dmlOptions = JdbcDmlOptions.builder().withTableName(jdbcOptions.getTableName()).withDialect(jdbcOptions.getDialect()).withFieldNames(fieldNames).build();
    outputFormat = new JdbcOutputFormatBuilder().setJdbcOptions(jdbcOptions).setFieldDataTypes(fieldDataTypes).setJdbcDmlOptions(dmlOptions).setJdbcExecutionOptions(JdbcExecutionOptions.builder().build()).setRowDataTypeInfo(rowDataTypeInfo).build();
    setRuntimeContext(outputFormat, true);
    outputFormat.open(0, 1);
    setRuntimeContext(outputFormat, true);
    outputFormat.open(0, 1);
    for (TestEntry entry : TEST_DATA) {
        outputFormat.writeRecord(buildGenericData(entry.id, entry.title, entry.author, entry.price, entry.qty));
    }
    outputFormat.close();
    try (Connection dbConn = DriverManager.getConnection(DERBY_EBOOKSHOP_DB.getUrl());
        PreparedStatement statement = dbConn.prepareStatement(SELECT_ALL_NEWBOOKS);
        ResultSet resultSet = statement.executeQuery()) {
        int recordCount = 0;
        while (resultSet.next()) {
            assertEquals(TEST_DATA[recordCount].id, resultSet.getObject("id"));
            assertEquals(TEST_DATA[recordCount].title, resultSet.getObject("title"));
            assertEquals(TEST_DATA[recordCount].author, resultSet.getObject("author"));
            assertEquals(TEST_DATA[recordCount].price, resultSet.getObject("price"));
            assertEquals(TEST_DATA[recordCount].qty, resultSet.getObject("qty"));
            recordCount++;
        }
        assertEquals(TEST_DATA.length, recordCount);
    }
}
Also used : JdbcConnectorOptions(org.apache.flink.connector.jdbc.internal.options.JdbcConnectorOptions) JdbcDmlOptions(org.apache.flink.connector.jdbc.internal.options.JdbcDmlOptions) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) TestEntry(org.apache.flink.connector.jdbc.JdbcTestFixture.TestEntry) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 4 with JdbcDmlOptions

use of org.apache.flink.connector.jdbc.internal.options.JdbcDmlOptions in project flink by apache.

the class JdbcOutputFormatTest method testIncompatibleTypes.

@Test
public void testIncompatibleTypes() {
    try {
        JdbcConnectorOptions jdbcOptions = JdbcConnectorOptions.builder().setDriverName(DERBY_EBOOKSHOP_DB.getDriverClass()).setDBUrl(DERBY_EBOOKSHOP_DB.getUrl()).setTableName(INPUT_TABLE).build();
        JdbcDmlOptions dmlOptions = JdbcDmlOptions.builder().withTableName(jdbcOptions.getTableName()).withDialect(jdbcOptions.getDialect()).withFieldNames(fieldNames).build();
        outputFormat = new JdbcOutputFormatBuilder().setJdbcOptions(jdbcOptions).setFieldDataTypes(fieldDataTypes).setJdbcDmlOptions(dmlOptions).setJdbcExecutionOptions(JdbcExecutionOptions.builder().build()).setRowDataTypeInfo(rowDataTypeInfo).build();
        setRuntimeContext(outputFormat, false);
        outputFormat.open(0, 1);
        RowData row = buildGenericData(4, "hello", "world", 0.99, "imthewrongtype");
        outputFormat.writeRecord(row);
        outputFormat.close();
        fail("Expected exception is not thrown.");
    } catch (Exception e) {
        assertTrue(findThrowable(e, ClassCastException.class).isPresent());
    }
}
Also used : RowData(org.apache.flink.table.data.RowData) JdbcConnectorOptions(org.apache.flink.connector.jdbc.internal.options.JdbcConnectorOptions) JdbcDmlOptions(org.apache.flink.connector.jdbc.internal.options.JdbcDmlOptions) SQLException(java.sql.SQLException) IOException(java.io.IOException) Test(org.junit.Test)

Example 5 with JdbcDmlOptions

use of org.apache.flink.connector.jdbc.internal.options.JdbcDmlOptions in project flink by apache.

the class JdbcOutputFormatTest method testFlush.

@Test
public void testFlush() throws SQLException, IOException {
    JdbcConnectorOptions jdbcOptions = JdbcConnectorOptions.builder().setDriverName(DERBY_EBOOKSHOP_DB.getDriverClass()).setDBUrl(DERBY_EBOOKSHOP_DB.getUrl()).setTableName(OUTPUT_TABLE_2).build();
    JdbcDmlOptions dmlOptions = JdbcDmlOptions.builder().withTableName(jdbcOptions.getTableName()).withDialect(jdbcOptions.getDialect()).withFieldNames(fieldNames).build();
    JdbcExecutionOptions executionOptions = JdbcExecutionOptions.builder().withBatchSize(3).build();
    outputFormat = new JdbcOutputFormatBuilder().setJdbcOptions(jdbcOptions).setFieldDataTypes(fieldDataTypes).setJdbcDmlOptions(dmlOptions).setJdbcExecutionOptions(executionOptions).setRowDataTypeInfo(rowDataTypeInfo).build();
    setRuntimeContext(outputFormat, true);
    outputFormat.open(0, 1);
    try (Connection dbConn = DriverManager.getConnection(DERBY_EBOOKSHOP_DB.getUrl());
        PreparedStatement statement = dbConn.prepareStatement(SELECT_ALL_NEWBOOKS_2)) {
        outputFormat.open(0, 1);
        for (int i = 0; i < 2; ++i) {
            outputFormat.writeRecord(buildGenericData(TEST_DATA[i].id, TEST_DATA[i].title, TEST_DATA[i].author, TEST_DATA[i].price, TEST_DATA[i].qty));
        }
        try (ResultSet resultSet = statement.executeQuery()) {
            assertFalse(resultSet.next());
        }
        outputFormat.writeRecord(buildGenericData(TEST_DATA[2].id, TEST_DATA[2].title, TEST_DATA[2].author, TEST_DATA[2].price, TEST_DATA[2].qty));
        try (ResultSet resultSet = statement.executeQuery()) {
            int recordCount = 0;
            while (resultSet.next()) {
                assertEquals(TEST_DATA[recordCount].id, resultSet.getObject("id"));
                assertEquals(TEST_DATA[recordCount].title, resultSet.getObject("title"));
                assertEquals(TEST_DATA[recordCount].author, resultSet.getObject("author"));
                assertEquals(TEST_DATA[recordCount].price, resultSet.getObject("price"));
                assertEquals(TEST_DATA[recordCount].qty, resultSet.getObject("qty"));
                recordCount++;
            }
            assertEquals(3, recordCount);
        }
    } finally {
        outputFormat.close();
    }
}
Also used : JdbcExecutionOptions(org.apache.flink.connector.jdbc.JdbcExecutionOptions) JdbcConnectorOptions(org.apache.flink.connector.jdbc.internal.options.JdbcConnectorOptions) JdbcDmlOptions(org.apache.flink.connector.jdbc.internal.options.JdbcDmlOptions) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Aggregations

JdbcConnectorOptions (org.apache.flink.connector.jdbc.internal.options.JdbcConnectorOptions)15 JdbcDmlOptions (org.apache.flink.connector.jdbc.internal.options.JdbcDmlOptions)15 Test (org.junit.Test)14 JdbcExecutionOptions (org.apache.flink.connector.jdbc.JdbcExecutionOptions)6 IOException (java.io.IOException)5 SQLException (java.sql.SQLException)5 TestEntry (org.apache.flink.connector.jdbc.JdbcTestFixture.TestEntry)5 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 RowData (org.apache.flink.table.data.RowData)4 SimpleJdbcConnectionProvider (org.apache.flink.connector.jdbc.internal.connection.SimpleJdbcConnectionProvider)3 DynamicTableSink (org.apache.flink.table.connector.sink.DynamicTableSink)3 RuntimeContext (org.apache.flink.api.common.functions.RuntimeContext)2 Serializable (java.io.Serializable)1 Arrays (java.util.Arrays)1 Function (java.util.function.Function)1 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)1 TypeInformation (org.apache.flink.api.common.typeinfo.TypeInformation)1 TypeSerializer (org.apache.flink.api.common.typeutils.TypeSerializer)1