Search in sources :

Example 11 with JdbcConnectorOptions

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

the class JdbcOutputFormatTest method testJdbcOutputFormat.

@Test
public void testJdbcOutputFormat() throws IOException, SQLException {
    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);
    setRuntimeContext(outputFormat, true);
    outputFormat.open(0, 1);
    for (TestEntry entry : TEST_DATA) {
        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);
        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 12 with JdbcConnectorOptions

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

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 13 with JdbcConnectorOptions

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

the class JdbcOutputFormatTest method testIncompatibleTypes.

@Test
public void testIncompatibleTypes() {
    try {
        JdbcConnectorOptions jdbcOptions = JdbcConnectorOptions.builder().setDriverName(DERBY_EBOOKSHOP_DB.getDriverClass()).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()).setRowDataTypeInfo(rowDataTypeInfo).build();
        setRuntimeContext(outputFormat, false);
        outputFormat.open(0, 1);
        RowData row = buildGenericData(4, "hello", "world", 0.99, "imthewrongtype");
        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) SQLException(java.sql.SQLException) IOException(java.io.IOException) Test(org.junit.Test)

Example 14 with JdbcConnectorOptions

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

the class JdbcOutputFormatTest method testFlush.

@Test
public void testFlush() 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(3).build();
    outputFormat = new JdbcOutputFormatBuilder().setJdbcOptions(jdbcOptions).setFieldDataTypes(fieldDataTypes).setJdbcDmlOptions(dmlOptions).setJdbcExecutionOptions(executionOptions).setRowDataTypeInfo(rowDataTypeInfo).build();
    setRuntimeContext(outputFormat, true);
    outputFormat.open(0, 1);
    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());
        }
        outputFormat.writeRecord(buildGenericData(TEST_DATA[2].id, TEST_DATA[2].title, TEST_DATA[2].author, TEST_DATA[2].price, TEST_DATA[2].qty));
        try (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(3, recordCount);
        }
    } 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 15 with JdbcConnectorOptions

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

the class JdbcRowDataLookupFunctionTest method buildRowDataLookupFunction.

private JdbcRowDataLookupFunction buildRowDataLookupFunction(JdbcLookupOptions lookupOptions) {
    JdbcConnectorOptions jdbcOptions = JdbcConnectorOptions.builder().setDriverName(DERBY_EBOOKSHOP_DB.getDriverClass()).setDBUrl(DB_URL).setTableName(LOOKUP_TABLE).build();
    RowType rowType = RowType.of(Arrays.stream(fieldDataTypes).map(DataType::getLogicalType).toArray(LogicalType[]::new), fieldNames);
    JdbcRowDataLookupFunction lookupFunction = new JdbcRowDataLookupFunction(jdbcOptions, lookupOptions, fieldNames, fieldDataTypes, lookupKeys, rowType);
    return lookupFunction;
}
Also used : JdbcConnectorOptions(org.apache.flink.connector.jdbc.internal.options.JdbcConnectorOptions) RowType(org.apache.flink.table.types.logical.RowType) DataType(org.apache.flink.table.types.DataType) LogicalType(org.apache.flink.table.types.logical.LogicalType)

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