use of org.apache.flink.connector.jdbc.JdbcConnectionOptions in project flink by apache.
the class SimpleJdbcConnectionProviderDriverClassConcurrentLoadingTest method testDriverClassConcurrentLoading.
@Test(timeout = 5000)
public void testDriverClassConcurrentLoading() throws Exception {
ClassLoader classLoader = getClass().getClassLoader();
assertFalse(isClassLoaded(classLoader, FakeDBUtils.DRIVER1_CLASS_NAME));
assertFalse(isClassLoaded(classLoader, FakeDBUtils.DRIVER2_CLASS_NAME));
JdbcConnectionOptions connectionOptions1 = new JdbcConnectionOptions.JdbcConnectionOptionsBuilder().withUrl(FakeDBUtils.TEST_DB_URL).withDriverName(FakeDBUtils.DRIVER1_CLASS_NAME).build();
JdbcConnectionOptions connectionOptions2 = new JdbcConnectionOptions.JdbcConnectionOptionsBuilder().withUrl(FakeDBUtils.TEST_DB_URL).withDriverName(FakeDBUtils.DRIVER2_CLASS_NAME).build();
CountDownLatch startLatch = new CountDownLatch(1);
Function<JdbcConnectionOptions, CheckedThread> connectionThreadCreator = options -> {
CheckedThread thread = new CheckedThread() {
@Override
public void go() throws Exception {
startLatch.await();
JdbcConnectionProvider connectionProvider = new SimpleJdbcConnectionProvider(options);
Connection connection = connectionProvider.getOrEstablishConnection();
connection.close();
}
};
thread.setName("Loading " + options.getDriverName());
thread.setDaemon(true);
return thread;
};
CheckedThread connectionThread1 = connectionThreadCreator.apply(connectionOptions1);
CheckedThread connectionThread2 = connectionThreadCreator.apply(connectionOptions2);
connectionThread1.start();
connectionThread2.start();
Thread.sleep(2);
startLatch.countDown();
connectionThread1.sync();
connectionThread2.sync();
assertTrue(isClassLoaded(classLoader, FakeDBUtils.DRIVER1_CLASS_NAME));
assertTrue(isClassLoaded(classLoader, FakeDBUtils.DRIVER2_CLASS_NAME));
}
use of org.apache.flink.connector.jdbc.JdbcConnectionOptions in project flink by apache.
the class JdbcFullTest method runTest.
private void runTest(boolean exploitParallelism) throws Exception {
ExecutionEnvironment environment = ExecutionEnvironment.getExecutionEnvironment();
JdbcInputFormat.JdbcInputFormatBuilder inputBuilder = JdbcInputFormat.buildJdbcInputFormat().setDrivername(getDbMetadata().getDriverClass()).setDBUrl(getDbMetadata().getUrl()).setQuery(SELECT_ALL_BOOKS).setRowTypeInfo(ROW_TYPE_INFO);
if (exploitParallelism) {
final int fetchSize = 1;
final long min = TEST_DATA[0].id;
final long max = TEST_DATA[TEST_DATA.length - fetchSize].id;
// use a "splittable" query to exploit parallelism
inputBuilder = inputBuilder.setQuery(SELECT_ALL_BOOKS_SPLIT_BY_ID).setParametersProvider(new JdbcNumericBetweenParametersProvider(min, max).ofBatchSize(fetchSize));
}
DataSet<Row> source = environment.createInput(inputBuilder.finish());
// NOTE: in this case (with Derby driver) setSqlTypes could be skipped, but
// some databases don't null values correctly when no column type was specified
// in PreparedStatement.setObject (see its javadoc for more details)
JdbcConnectionOptions connectionOptions = new JdbcConnectionOptions.JdbcConnectionOptionsBuilder().withUrl(getDbMetadata().getUrl()).withDriverName(getDbMetadata().getDriverClass()).build();
JdbcOutputFormat jdbcOutputFormat = new JdbcOutputFormat<>(new SimpleJdbcConnectionProvider(connectionOptions), JdbcExecutionOptions.defaults(), ctx -> createSimpleRowExecutor(String.format(INSERT_TEMPLATE, OUTPUT_TABLE), new int[] { Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.DOUBLE, Types.INTEGER }, ctx.getExecutionConfig().isObjectReuseEnabled()), JdbcOutputFormat.RecordExtractor.identity());
source.output(jdbcOutputFormat);
environment.execute();
try (Connection dbConn = DriverManager.getConnection(getDbMetadata().getUrl());
PreparedStatement statement = dbConn.prepareStatement(SELECT_ALL_NEWBOOKS);
ResultSet resultSet = statement.executeQuery()) {
int count = 0;
while (resultSet.next()) {
count++;
}
Assert.assertEquals(TEST_DATA.length, count);
}
}
Aggregations