Search in sources :

Example 11 with TestEntry

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

the class JdbcRowOutputFormatTest method testExceptionOnClose.

@Test
public void testExceptionOnClose() {
    String expectedMsg = "Writing records to JDBC failed.";
    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);
        row.setField(3, entry.price);
        row.setField(4, entry.qty);
        jdbcOutputFormat.writeRecord(row);
        jdbcOutputFormat.writeRecord(// writing the same record twice must yield a unique key violation.
        row);
        jdbcOutputFormat.close();
    } catch (Exception e) {
        assertTrue(findThrowable(e, RuntimeException.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