Search in sources :

Example 1 with JdbcConnectionOptions

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));
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) CheckedThread(org.apache.flink.core.testutils.CheckedThread) Connection(java.sql.Connection) FakeDBUtils(org.apache.flink.connector.jdbc.fakedb.FakeDBUtils) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) JdbcConnectionOptions(org.apache.flink.connector.jdbc.JdbcConnectionOptions) Method(java.lang.reflect.Method) Function(java.util.function.Function) Connection(java.sql.Connection) JdbcConnectionOptions(org.apache.flink.connector.jdbc.JdbcConnectionOptions) CountDownLatch(java.util.concurrent.CountDownLatch) CheckedThread(org.apache.flink.core.testutils.CheckedThread) Test(org.junit.Test)

Example 2 with JdbcConnectionOptions

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);
    }
}
Also used : ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) JdbcInputFormat(org.apache.flink.connector.jdbc.JdbcInputFormat) Connection(java.sql.Connection) JdbcConnectionOptions(org.apache.flink.connector.jdbc.JdbcConnectionOptions) PreparedStatement(java.sql.PreparedStatement) JdbcNumericBetweenParametersProvider(org.apache.flink.connector.jdbc.split.JdbcNumericBetweenParametersProvider) SimpleJdbcConnectionProvider(org.apache.flink.connector.jdbc.internal.connection.SimpleJdbcConnectionProvider) ResultSet(java.sql.ResultSet) Row(org.apache.flink.types.Row)

Aggregations

Connection (java.sql.Connection)2 JdbcConnectionOptions (org.apache.flink.connector.jdbc.JdbcConnectionOptions)2 Method (java.lang.reflect.Method)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Function (java.util.function.Function)1 ExecutionEnvironment (org.apache.flink.api.java.ExecutionEnvironment)1 JdbcInputFormat (org.apache.flink.connector.jdbc.JdbcInputFormat)1 FakeDBUtils (org.apache.flink.connector.jdbc.fakedb.FakeDBUtils)1 SimpleJdbcConnectionProvider (org.apache.flink.connector.jdbc.internal.connection.SimpleJdbcConnectionProvider)1 JdbcNumericBetweenParametersProvider (org.apache.flink.connector.jdbc.split.JdbcNumericBetweenParametersProvider)1 CheckedThread (org.apache.flink.core.testutils.CheckedThread)1 Row (org.apache.flink.types.Row)1 Assert.assertFalse (org.junit.Assert.assertFalse)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 Test (org.junit.Test)1