Search in sources :

Example 11 with JdbcDmlOptions

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

the class JdbcDynamicTableFactoryTest method testJdbcSinkProperties.

@Test
public void testJdbcSinkProperties() {
    Map<String, String> properties = getAllOptions();
    properties.put("sink.buffer-flush.max-rows", "1000");
    properties.put("sink.buffer-flush.interval", "2min");
    properties.put("sink.max-retries", "5");
    DynamicTableSink actual = createTableSink(SCHEMA, properties);
    JdbcConnectorOptions options = JdbcConnectorOptions.builder().setDBUrl("jdbc:derby:memory:mydb").setTableName("mytable").build();
    JdbcExecutionOptions executionOptions = JdbcExecutionOptions.builder().withBatchSize(1000).withBatchIntervalMs(120_000).withMaxRetries(5).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 12 with JdbcDmlOptions

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

the class JdbcOutputFormatTest method testExceptionOnClose.

@Test
public void testExceptionOnClose() {
    String expectedMsg = "Writing records to JDBC failed.";
    try {
        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);
        TestEntry entry = TEST_DATA[0];
        RowData row = buildGenericData(entry.id, entry.title, entry.author, entry.price, entry.qty);
        outputFormat.writeRecord(row);
        outputFormat.writeRecord(// writing the same record twice must yield a unique key violation.
        row);
        outputFormat.close();
        fail("Expected exception is not thrown.");
    } catch (Exception e) {
        assertTrue(findThrowable(e, RuntimeException.class).isPresent());
        assertTrue(findThrowableWithMessage(e, expectedMsg).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) TestEntry(org.apache.flink.connector.jdbc.JdbcTestFixture.TestEntry) SQLException(java.sql.SQLException) IOException(java.io.IOException) Test(org.junit.Test)

Example 13 with JdbcDmlOptions

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

the class JdbcOutputFormatTest method testExceptionOnInvalidType.

@Test
public void testExceptionOnInvalidType() {
    try {
        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, false);
        outputFormat.open(0, 1);
        TestEntry entry = TEST_DATA[0];
        RowData row = buildGenericData(entry.id, entry.title, entry.author, 0L, entry.qty);
        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) TestEntry(org.apache.flink.connector.jdbc.JdbcTestFixture.TestEntry) SQLException(java.sql.SQLException) IOException(java.io.IOException) Test(org.junit.Test)

Example 14 with JdbcDmlOptions

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

the class JdbcOutputFormatTest method testInvalidURL.

@Test
public void testInvalidURL() {
    try {
        JdbcConnectorOptions jdbcOptions = JdbcConnectorOptions.builder().setDriverName(DERBY_EBOOKSHOP_DB.getDriverClass()).setDBUrl("jdbc:der:iamanerror:mory:ebookshop").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()).build();
        outputFormat.open(0, 1);
        fail("Expected exception is not thrown.");
    } catch (Exception e) {
        assertTrue(findThrowable(e, IllegalStateException.class).isPresent());
    }
}
Also used : 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 15 with JdbcDmlOptions

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

the class JdbcOutputFormatTest method testInvalidConnectionInJdbcOutputFormat.

@Test
public void testInvalidConnectionInJdbcOutputFormat() throws IOException, SQLException {
    JdbcConnectorOptions jdbcOptions = JdbcConnectorOptions.builder().setDriverName(DERBY_EBOOKSHOP_DB.getDriverClass()).setDBUrl(DERBY_EBOOKSHOP_DB.getUrl()).setTableName(OUTPUT_TABLE_3).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);
    // write records
    for (int i = 0; i < 3; i++) {
        TestEntry entry = TEST_DATA[i];
        outputFormat.writeRecord(buildGenericData(entry.id, entry.title, entry.author, entry.price, entry.qty));
    }
    // close connection
    outputFormat.getConnection().close();
    // continue to write rest records
    for (int i = 3; i < TEST_DATA.length; i++) {
        TestEntry entry = TEST_DATA[i];
        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_3);
        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)

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