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());
}
}
Aggregations