Search in sources :

Example 31 with JdbcConnectorOptions

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

the class JdbcOutputFormatTest method testFlushWithBatchSizeEqualsZero.

@Test
public void testFlushWithBatchSizeEqualsZero() 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(0).build();
    outputFormat = new JdbcOutputFormatBuilder().setJdbcOptions(jdbcOptions).setFieldDataTypes(fieldDataTypes).setJdbcDmlOptions(dmlOptions).setJdbcExecutionOptions(executionOptions).setRowDataTypeInfo(rowDataTypeInfo).build();
    setRuntimeContext(outputFormat, true);
    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());
        }
    } 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)

Example 32 with JdbcConnectorOptions

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

the class JdbcOutputFormatTest method testInvalidDriver.

@Test
public void testInvalidDriver() {
    String expectedMsg = "unable to open JDBC writer";
    try {
        JdbcConnectorOptions jdbcOptions = JdbcConnectorOptions.builder().setDriverName("org.apache.derby.jdbc.idontexist").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()).build();
        outputFormat.open(0, 1);
        fail("Expected exception is not thrown.");
    } catch (Exception e) {
        assertTrue(findThrowable(e, IOException.class).isPresent());
        assertTrue(findThrowableWithMessage(e, expectedMsg).isPresent());
    }
}
Also used : JdbcConnectorOptions(org.apache.flink.connector.jdbc.internal.options.JdbcConnectorOptions) JdbcDmlOptions(org.apache.flink.connector.jdbc.internal.options.JdbcDmlOptions) IOException(java.io.IOException) SQLException(java.sql.SQLException) IOException(java.io.IOException) Test(org.junit.Test)

Example 33 with JdbcConnectorOptions

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

the class JdbcTableOutputFormatTest method testJdbcOutputFormat.

@Test
public void testJdbcOutputFormat() throws Exception {
    JdbcConnectorOptions options = JdbcConnectorOptions.builder().setDBUrl(getDbMetadata().getUrl()).setTableName(OUTPUT_TABLE).build();
    JdbcDmlOptions dmlOptions = JdbcDmlOptions.builder().withTableName(options.getTableName()).withDialect(options.getDialect()).withFieldNames(fieldNames).withKeyFields(keyFields).build();
    format = new TableJdbcUpsertOutputFormat(new SimpleJdbcConnectionProvider(options), dmlOptions, JdbcExecutionOptions.defaults());
    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);
    for (TestEntry entry : TEST_DATA) {
        format.writeRecord(Tuple2.of(true, toRow(entry)));
    }
    format.flush();
    check(Arrays.stream(TEST_DATA).map(JdbcDataTestBase::toRow).toArray(Row[]::new));
    // override
    for (TestEntry entry : TEST_DATA) {
        format.writeRecord(Tuple2.of(true, toRow(entry)));
    }
    format.flush();
    check(Arrays.stream(TEST_DATA).map(JdbcDataTestBase::toRow).toArray(Row[]::new));
    // delete
    for (int i = 0; i < TEST_DATA.length / 2; i++) {
        format.writeRecord(Tuple2.of(false, toRow(TEST_DATA[i])));
    }
    Row[] expected = new Row[TEST_DATA.length - TEST_DATA.length / 2];
    for (int i = TEST_DATA.length / 2; i < TEST_DATA.length; i++) {
        expected[i - TEST_DATA.length / 2] = toRow(TEST_DATA[i]);
    }
    format.flush();
    check(expected);
}
Also used : JdbcConnectorOptions(org.apache.flink.connector.jdbc.internal.options.JdbcConnectorOptions) SimpleJdbcConnectionProvider(org.apache.flink.connector.jdbc.internal.connection.SimpleJdbcConnectionProvider) JdbcDmlOptions(org.apache.flink.connector.jdbc.internal.options.JdbcDmlOptions) TestEntry(org.apache.flink.connector.jdbc.JdbcTestFixture.TestEntry) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) Row(org.apache.flink.types.Row) RuntimeContext(org.apache.flink.api.common.functions.RuntimeContext) JdbcDataTestBase(org.apache.flink.connector.jdbc.JdbcDataTestBase) Test(org.junit.Test)

Example 34 with JdbcConnectorOptions

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

the class JdbcTableOutputFormatTest method testUpsertFormatCloseBeforeOpen.

@Test
public void testUpsertFormatCloseBeforeOpen() throws Exception {
    JdbcConnectorOptions options = JdbcConnectorOptions.builder().setDBUrl(getDbMetadata().getUrl()).setTableName(OUTPUT_TABLE).build();
    JdbcDmlOptions dmlOptions = JdbcDmlOptions.builder().withTableName(options.getTableName()).withDialect(options.getDialect()).withFieldNames(fieldNames).withKeyFields(keyFields).build();
    format = new TableJdbcUpsertOutputFormat(new SimpleJdbcConnectionProvider(options), dmlOptions, JdbcExecutionOptions.defaults());
    // FLINK-17544: There should be no NPE thrown from this method
    format.close();
}
Also used : JdbcConnectorOptions(org.apache.flink.connector.jdbc.internal.options.JdbcConnectorOptions) SimpleJdbcConnectionProvider(org.apache.flink.connector.jdbc.internal.connection.SimpleJdbcConnectionProvider) JdbcDmlOptions(org.apache.flink.connector.jdbc.internal.options.JdbcDmlOptions) Test(org.junit.Test)

Example 35 with JdbcConnectorOptions

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

the class JdbcTableOutputFormatTest method testUpsertFormatCloseBeforeOpen.

@Test
public void testUpsertFormatCloseBeforeOpen() throws Exception {
    JdbcConnectorOptions options = JdbcConnectorOptions.builder().setDBUrl(getDbMetadata().getUrl()).setTableName(OUTPUT_TABLE).build();
    JdbcDmlOptions dmlOptions = JdbcDmlOptions.builder().withTableName(options.getTableName()).withDialect(options.getDialect()).withFieldNames(fieldNames).withKeyFields(keyFields).build();
    format = new TableJdbcUpsertOutputFormat(new SimpleJdbcConnectionProvider(options), dmlOptions, JdbcExecutionOptions.defaults());
    // FLINK-17544: There should be no NPE thrown from this method
    format.close();
}
Also used : JdbcConnectorOptions(org.apache.flink.connector.jdbc.internal.options.JdbcConnectorOptions) SimpleJdbcConnectionProvider(org.apache.flink.connector.jdbc.internal.connection.SimpleJdbcConnectionProvider) JdbcDmlOptions(org.apache.flink.connector.jdbc.internal.options.JdbcDmlOptions) Test(org.junit.Test)

Aggregations

JdbcConnectorOptions (org.apache.flink.connector.jdbc.internal.options.JdbcConnectorOptions)81 Test (org.junit.Test)71 JdbcDmlOptions (org.apache.flink.connector.jdbc.internal.options.JdbcDmlOptions)57 JdbcExecutionOptions (org.apache.flink.connector.jdbc.JdbcExecutionOptions)21 IOException (java.io.IOException)20 SQLException (java.sql.SQLException)20 TestEntry (org.apache.flink.connector.jdbc.JdbcTestFixture.TestEntry)20 JdbcLookupOptions (org.apache.flink.connector.jdbc.internal.options.JdbcLookupOptions)18 Connection (java.sql.Connection)16 PreparedStatement (java.sql.PreparedStatement)16 ResultSet (java.sql.ResultSet)16 DynamicTableSource (org.apache.flink.table.connector.source.DynamicTableSource)15 DynamicTableSink (org.apache.flink.table.connector.sink.DynamicTableSink)12 RowData (org.apache.flink.table.data.RowData)12 SimpleJdbcConnectionProvider (org.apache.flink.connector.jdbc.internal.connection.SimpleJdbcConnectionProvider)8 ReadableConfig (org.apache.flink.configuration.ReadableConfig)5 JdbcReadOptions (org.apache.flink.connector.jdbc.internal.options.JdbcReadOptions)5 FactoryUtil (org.apache.flink.table.factories.FactoryUtil)5 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)4 RuntimeContext (org.apache.flink.api.common.functions.RuntimeContext)4