Search in sources :

Example 1 with GenericParameterValuesProvider

use of org.apache.flink.api.java.io.jdbc.split.GenericParameterValuesProvider in project flink by apache.

the class JDBCInputFormatTest method testJDBCInputFormatWithParallelismAndGenericSplitting.

@Test
public void testJDBCInputFormatWithParallelismAndGenericSplitting() throws IOException, InstantiationException, IllegalAccessException {
    Serializable[][] queryParameters = new String[2][1];
    queryParameters[0] = new String[] { "Kumar" };
    queryParameters[1] = new String[] { "Tan Ah Teck" };
    ParameterValuesProvider paramProvider = new GenericParameterValuesProvider(queryParameters);
    jdbcInputFormat = JDBCInputFormat.buildJDBCInputFormat().setDrivername(DRIVER_CLASS).setDBUrl(DB_URL).setQuery(JDBCTestBase.SELECT_ALL_BOOKS_SPLIT_BY_AUTHOR).setRowTypeInfo(rowTypeInfo).setParametersProvider(paramProvider).setResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE).finish();
    jdbcInputFormat.openInputFormat();
    InputSplit[] splits = jdbcInputFormat.createInputSplits(1);
    //this query exploit parallelism (1 split for every queryParameters row)
    Assert.assertEquals(queryParameters.length, splits.length);
    int recordCount = 0;
    Row row = new Row(5);
    for (int i = 0; i < splits.length; i++) {
        jdbcInputFormat.open(splits[i]);
        while (!jdbcInputFormat.reachedEnd()) {
            Row next = jdbcInputFormat.nextRecord(row);
            if (next == null) {
                break;
            }
            if (next.getField(0) != null) {
                Assert.assertEquals("Field 0 should be int", Integer.class, next.getField(0).getClass());
            }
            if (next.getField(1) != null) {
                Assert.assertEquals("Field 1 should be String", String.class, next.getField(1).getClass());
            }
            if (next.getField(2) != null) {
                Assert.assertEquals("Field 2 should be String", String.class, next.getField(2).getClass());
            }
            if (next.getField(3) != null) {
                Assert.assertEquals("Field 3 should be float", Double.class, next.getField(3).getClass());
            }
            if (next.getField(4) != null) {
                Assert.assertEquals("Field 4 should be int", Integer.class, next.getField(4).getClass());
            }
            recordCount++;
        }
        jdbcInputFormat.close();
    }
    Assert.assertEquals(3, recordCount);
    jdbcInputFormat.closeInputFormat();
}
Also used : GenericParameterValuesProvider(org.apache.flink.api.java.io.jdbc.split.GenericParameterValuesProvider) ParameterValuesProvider(org.apache.flink.api.java.io.jdbc.split.ParameterValuesProvider) Row(org.apache.flink.types.Row) InputSplit(org.apache.flink.core.io.InputSplit) GenericParameterValuesProvider(org.apache.flink.api.java.io.jdbc.split.GenericParameterValuesProvider) Test(org.junit.Test)

Aggregations

GenericParameterValuesProvider (org.apache.flink.api.java.io.jdbc.split.GenericParameterValuesProvider)1 ParameterValuesProvider (org.apache.flink.api.java.io.jdbc.split.ParameterValuesProvider)1 InputSplit (org.apache.flink.core.io.InputSplit)1 Row (org.apache.flink.types.Row)1 Test (org.junit.Test)1