Search in sources :

Example 6 with TestEntry

use of org.apache.flink.connector.jdbc.JdbcTestFixture.TestEntry in project flink by apache.

the class JdbcAppendOnlyWriterTest method testMaxRetry.

@Test(expected = IOException.class)
public void testMaxRetry() throws Exception {
    format = JdbcOutputFormat.builder().setOptions(JdbcConnectorOptions.builder().setDBUrl(getDbMetadata().getUrl()).setTableName(OUTPUT_TABLE).build()).setFieldNames(fieldNames).setKeyFields(null).build();
    RuntimeContext context = Mockito.mock(RuntimeContext.class);
    ExecutionConfig config = Mockito.mock(ExecutionConfig.class);
    doReturn(config).when(context).getExecutionConfig();
    doReturn(true).when(config).isObjectReuseEnabled();
    format.setRuntimeContext(context);
    format.open(0, 1);
    // alter table schema to trigger retry logic after failure.
    alterTable();
    for (TestEntry entry : TEST_DATA) {
        format.writeRecord(Tuple2.of(true, toRow(entry)));
    }
    // after retry default times, throws a BatchUpdateException.
    format.flush();
}
Also used : TestEntry(org.apache.flink.connector.jdbc.JdbcTestFixture.TestEntry) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) RuntimeContext(org.apache.flink.api.common.functions.RuntimeContext) Test(org.junit.Test)

Example 7 with TestEntry

use of org.apache.flink.connector.jdbc.JdbcTestFixture.TestEntry 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 8 with TestEntry

use of org.apache.flink.connector.jdbc.JdbcTestFixture.TestEntry 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 9 with TestEntry

use of org.apache.flink.connector.jdbc.JdbcTestFixture.TestEntry 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)

Example 10 with TestEntry

use of org.apache.flink.connector.jdbc.JdbcTestFixture.TestEntry in project flink by apache.

the class JdbcRowOutputFormatTest method testExceptionOnInvalidType.

@Test
public void testExceptionOnInvalidType() {
    String expectedMsg = "field index: 3, field value: 0.";
    try {
        jdbcOutputFormat = JdbcRowOutputFormat.buildJdbcOutputFormat().setDrivername(DERBY_EBOOKSHOP_DB.getDriverClass()).setDBUrl(DERBY_EBOOKSHOP_DB.getUrl()).setQuery(String.format(INSERT_TEMPLATE, OUTPUT_TABLE)).setSqlTypes(new int[] { Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.DOUBLE, Types.INTEGER }).finish();
        setRuntimeContext(jdbcOutputFormat, true);
        jdbcOutputFormat.open(0, 1);
        TestEntry entry = TEST_DATA[0];
        Row row = new Row(5);
        row.setField(0, entry.id);
        row.setField(1, entry.title);
        row.setField(2, entry.author);
        // use incompatible type (Long instead of Double)
        row.setField(3, 0L);
        row.setField(4, entry.qty);
        jdbcOutputFormat.writeRecord(row);
        jdbcOutputFormat.close();
    } catch (Exception e) {
        assertTrue(findThrowable(e, ClassCastException.class).isPresent());
        assertTrue(findThrowableWithMessage(e, expectedMsg).isPresent());
    }
}
Also used : TestEntry(org.apache.flink.connector.jdbc.JdbcTestFixture.TestEntry) Row(org.apache.flink.types.Row) SQLException(java.sql.SQLException) SQLDataException(java.sql.SQLDataException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

TestEntry (org.apache.flink.connector.jdbc.JdbcTestFixture.TestEntry)11 Test (org.junit.Test)9 JdbcConnectorOptions (org.apache.flink.connector.jdbc.internal.options.JdbcConnectorOptions)5 JdbcDmlOptions (org.apache.flink.connector.jdbc.internal.options.JdbcDmlOptions)5 IOException (java.io.IOException)4 Connection (java.sql.Connection)4 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)4 PreparedStatement (java.sql.PreparedStatement)3 Row (org.apache.flink.types.Row)3 SQLDataException (java.sql.SQLDataException)2 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)2 RuntimeContext (org.apache.flink.api.common.functions.RuntimeContext)2 RowData (org.apache.flink.table.data.RowData)2 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 Configuration (org.apache.flink.configuration.Configuration)1 JdbcDataTestBase (org.apache.flink.connector.jdbc.JdbcDataTestBase)1 SimpleJdbcConnectionProvider (org.apache.flink.connector.jdbc.internal.connection.SimpleJdbcConnectionProvider)1