Search in sources :

Example 1 with JdbcDataTestBase

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

Aggregations

ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)1 RuntimeContext (org.apache.flink.api.common.functions.RuntimeContext)1 JdbcDataTestBase (org.apache.flink.connector.jdbc.JdbcDataTestBase)1 TestEntry (org.apache.flink.connector.jdbc.JdbcTestFixture.TestEntry)1 SimpleJdbcConnectionProvider (org.apache.flink.connector.jdbc.internal.connection.SimpleJdbcConnectionProvider)1 JdbcConnectorOptions (org.apache.flink.connector.jdbc.internal.options.JdbcConnectorOptions)1 JdbcDmlOptions (org.apache.flink.connector.jdbc.internal.options.JdbcDmlOptions)1 Row (org.apache.flink.types.Row)1 Test (org.junit.Test)1