Search in sources :

Example 1 with JDBCInputFormatBuilder

use of org.apache.flink.api.java.io.jdbc.JDBCInputFormat.JDBCInputFormatBuilder in project flink by apache.

the class JDBCFullTest method runTest.

private void runTest(boolean exploitParallelism) {
    ExecutionEnvironment environment = ExecutionEnvironment.getExecutionEnvironment();
    JDBCInputFormatBuilder inputBuilder = JDBCInputFormat.buildJDBCInputFormat().setDrivername(JDBCTestBase.DRIVER_CLASS).setDBUrl(JDBCTestBase.DB_URL).setQuery(JDBCTestBase.SELECT_ALL_BOOKS).setRowTypeInfo(rowTypeInfo);
    if (exploitParallelism) {
        final int fetchSize = 1;
        final Long min = new Long(JDBCTestBase.testData[0][0].toString());
        final Long max = new Long(JDBCTestBase.testData[JDBCTestBase.testData.length - fetchSize][0].toString());
        //use a "splittable" query to exploit parallelism
        inputBuilder = inputBuilder.setQuery(JDBCTestBase.SELECT_ALL_BOOKS_SPLIT_BY_ID).setParametersProvider(new NumericBetweenParametersProvider(fetchSize, min, max));
    }
    DataSet<Row> source = environment.createInput(inputBuilder.finish());
    //NOTE: in this case (with Derby driver) setSqlTypes could be skipped, but
    //some database, doens't handle correctly null values when no column type specified
    //in PreparedStatement.setObject (see its javadoc for more details)
    source.output(JDBCOutputFormat.buildJDBCOutputFormat().setDrivername(JDBCTestBase.DRIVER_CLASS).setDBUrl(JDBCTestBase.DB_URL).setQuery("insert into newbooks (id,title,author,price,qty) values (?,?,?,?,?)").setSqlTypes(new int[] { Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.DOUBLE, Types.INTEGER }).finish());
    try {
        environment.execute();
    } catch (Exception e) {
        Assert.fail("JDBC full test failed. " + e.getMessage());
    }
    try (Connection dbConn = DriverManager.getConnection(JDBCTestBase.DB_URL);
        PreparedStatement statement = dbConn.prepareStatement(JDBCTestBase.SELECT_ALL_NEWBOOKS);
        ResultSet resultSet = statement.executeQuery()) {
        int count = 0;
        while (resultSet.next()) {
            count++;
        }
        Assert.assertEquals(JDBCTestBase.testData.length, count);
    } catch (SQLException e) {
        Assert.fail("JDBC full test failed. " + e.getMessage());
    }
}
Also used : ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) NumericBetweenParametersProvider(org.apache.flink.api.java.io.jdbc.split.NumericBetweenParametersProvider) SQLException(java.sql.SQLException) JDBCInputFormatBuilder(org.apache.flink.api.java.io.jdbc.JDBCInputFormat.JDBCInputFormatBuilder) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Row(org.apache.flink.types.Row) SQLException(java.sql.SQLException)

Aggregations

Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ExecutionEnvironment (org.apache.flink.api.java.ExecutionEnvironment)1 JDBCInputFormatBuilder (org.apache.flink.api.java.io.jdbc.JDBCInputFormat.JDBCInputFormatBuilder)1 NumericBetweenParametersProvider (org.apache.flink.api.java.io.jdbc.split.NumericBetweenParametersProvider)1 Row (org.apache.flink.types.Row)1