Github user fhueske commented on a diff in the pull request:
https://github.com/apache/flink/pull/1941#discussion_r62833701
--- Diff:
flink-batch-connectors/flink-jdbc/src/test/java/org/apache/flink/api/java/io/jdbc/JDBCInputFormatTest.java
---
@@ -19,180 +19,224 @@
package org.apache.flink.api.java.io.jdbc;
import java.io.IOException;
-import java.sql.Connection;
-import java.sql.DriverManager;
-import java.sql.SQLException;
-import java.sql.Statement;
import java.sql.ResultSet;
-
+import org.apache.flink.api.common.ExecutionConfig;
+import
org.apache.flink.api.java.io.jdbc.split.GenericParameterValuesProvider;
+import
org.apache.flink.api.java.io.jdbc.split.NumericBetweenParametersProvider;
+import org.apache.flink.api.java.io.jdbc.split.ParameterValuesProvider;
+import org.apache.flink.api.java.typeutils.runtime.kryo.KryoSerializer;
+import org.apache.flink.api.table.Row;
+import org.apache.flink.core.io.InputSplit;
import org.junit.Assert;
-
-import org.apache.flink.api.java.tuple.Tuple2;
-import org.apache.flink.api.java.tuple.Tuple5;
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
import org.junit.Test;
-public class JDBCInputFormatTest {
- JDBCInputFormat jdbcInputFormat;
-
- static Connection conn;
-
- static final Object[][] dbData = {
- {1001, ("Java for dummies"), ("Tan Ah Teck"), 11.11, 11},
- {1002, ("More Java for dummies"), ("Tan Ah Teck"), 22.22, 22},
- {1003, ("More Java for more dummies"), ("Mohammad Ali"), 33.33,
33},
- {1004, ("A Cup of Java"), ("Kumar"), 44.44, 44},
- {1005, ("A Teaspoon of Java"), ("Kevin Jones"), 55.55, 55}};
-
- @BeforeClass
- public static void setUpClass() {
- try {
- prepareDerbyDatabase();
- } catch (Exception e) {
- Assert.fail();
- }
- }
-
- private static void prepareDerbyDatabase() throws
ClassNotFoundException, SQLException {
- System.setProperty("derby.stream.error.field",
"org.apache.flink.api.java.io.jdbc.DerbyUtil.DEV_NULL");
- String dbURL = "jdbc:derby:memory:ebookshop;create=true";
- Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
- conn = DriverManager.getConnection(dbURL);
- createTable();
- insertDataToSQLTable();
- conn.close();
- }
-
- private static void createTable() throws SQLException {
- StringBuilder sqlQueryBuilder = new StringBuilder("CREATE TABLE
books (");
- sqlQueryBuilder.append("id INT NOT NULL DEFAULT 0,");
- sqlQueryBuilder.append("title VARCHAR(50) DEFAULT NULL,");
- sqlQueryBuilder.append("author VARCHAR(50) DEFAULT NULL,");
- sqlQueryBuilder.append("price FLOAT DEFAULT NULL,");
- sqlQueryBuilder.append("qty INT DEFAULT NULL,");
- sqlQueryBuilder.append("PRIMARY KEY (id))");
-
- Statement stat = conn.createStatement();
- stat.executeUpdate(sqlQueryBuilder.toString());
- stat.close();
- }
-
- private static void insertDataToSQLTable() throws SQLException {
- StringBuilder sqlQueryBuilder = new StringBuilder("INSERT INTO
books (id, title, author, price, qty) VALUES ");
- sqlQueryBuilder.append("(1001, 'Java for dummies', 'Tan Ah
Teck', 11.11, 11),");
- sqlQueryBuilder.append("(1002, 'More Java for dummies', 'Tan Ah
Teck', 22.22, 22),");
- sqlQueryBuilder.append("(1003, 'More Java for more dummies',
'Mohammad Ali', 33.33, 33),");
- sqlQueryBuilder.append("(1004, 'A Cup of Java', 'Kumar', 44.44,
44),");
- sqlQueryBuilder.append("(1005, 'A Teaspoon of Java', 'Kevin
Jones', 55.55, 55)");
-
- Statement stat = conn.createStatement();
- stat.execute(sqlQueryBuilder.toString());
- stat.close();
- }
-
- @AfterClass
- public static void tearDownClass() {
- cleanUpDerbyDatabases();
- }
-
- private static void cleanUpDerbyDatabases() {
- try {
- String dbURL =
"jdbc:derby:memory:ebookshop;create=true";
- Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
-
- conn = DriverManager.getConnection(dbURL);
- Statement stat = conn.createStatement();
- stat.executeUpdate("DROP TABLE books");
- stat.close();
- conn.close();
- } catch (Exception e) {
- e.printStackTrace();
- Assert.fail();
- }
- }
-
- @After
- public void tearDown() {
- jdbcInputFormat = null;
- }
+public class JDBCInputFormatTest extends JDBCTestBase {
@Test(expected = IllegalArgumentException.class)
public void testInvalidDriver() throws IOException {
jdbcInputFormat = JDBCInputFormat.buildJDBCInputFormat()
.setDrivername("org.apache.derby.jdbc.idontexist")
- .setDBUrl("jdbc:derby:memory:ebookshop")
- .setQuery("select * from books")
+ .setDBUrl(DB_URL)
+ .setQuery(SELECT_ALL_BOOKS)
.finish();
- jdbcInputFormat.open(null);
+ jdbcInputFormat.openInputFormat();
}
@Test(expected = IllegalArgumentException.class)
public void testInvalidURL() throws IOException {
jdbcInputFormat = JDBCInputFormat.buildJDBCInputFormat()
-
.setDrivername("org.apache.derby.jdbc.EmbeddedDriver")
+ .setDrivername(DRIVER_CLASS)
.setDBUrl("jdbc:der:iamanerror:mory:ebookshop")
- .setQuery("select * from books")
+ .setQuery(SELECT_ALL_BOOKS)
.finish();
- jdbcInputFormat.open(null);
+ jdbcInputFormat.openInputFormat();
}
@Test(expected = IllegalArgumentException.class)
public void testInvalidQuery() throws IOException {
jdbcInputFormat = JDBCInputFormat.buildJDBCInputFormat()
-
.setDrivername("org.apache.derby.jdbc.EmbeddedDriver")
- .setDBUrl("jdbc:derby:memory:ebookshop")
+ .setDrivername(DRIVER_CLASS)
+ .setDBUrl(DB_URL)
.setQuery("iamnotsql")
.finish();
- jdbcInputFormat.open(null);
+ jdbcInputFormat.openInputFormat();
}
@Test(expected = IllegalArgumentException.class)
public void testIncompleteConfiguration() throws IOException {
jdbcInputFormat = JDBCInputFormat.buildJDBCInputFormat()
-
.setDrivername("org.apache.derby.jdbc.EmbeddedDriver")
- .setQuery("select * from books")
+ .setDrivername(DRIVER_CLASS)
+ .setQuery(SELECT_ALL_BOOKS)
.finish();
}
- @Test(expected = IOException.class)
- public void testIncompatibleTuple() throws IOException {
+ @Test
+ public void testJDBCInputFormatWithoutParallelism() throws IOException,
InstantiationException, IllegalAccessException {
jdbcInputFormat = JDBCInputFormat.buildJDBCInputFormat()
-
.setDrivername("org.apache.derby.jdbc.EmbeddedDriver")
- .setDBUrl("jdbc:derby:memory:ebookshop")
- .setQuery("select * from books")
+ .setDrivername(DRIVER_CLASS)
+ .setDBUrl(DB_URL)
+ .setQuery(SELECT_ALL_BOOKS)
+
.setResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE)
.finish();
+ jdbcInputFormat.openInputFormat();
jdbcInputFormat.open(null);
- jdbcInputFormat.nextRecord(new Tuple2());
+ Row row = new Row(5);
+ int recordCount = 0;
+ while (!jdbcInputFormat.reachedEnd()) {
+ Row next = jdbcInputFormat.nextRecord(row);
+ if (next == null) {
+ break;
+ }
+
+ if(next.productElement(0)!=null) {
Assert.assertEquals("Field 0 should be int", Integer.class,
next.productElement(0).getClass());}
+ if(next.productElement(1)!=null) {
Assert.assertEquals("Field 1 should be String", String.class,
next.productElement(1).getClass());}
+ if(next.productElement(2)!=null) {
Assert.assertEquals("Field 2 should be String", String.class,
next.productElement(2).getClass());}
+ if(next.productElement(3)!=null) {
Assert.assertEquals("Field 3 should be float", Double.class,
next.productElement(3).getClass());}
+ if(next.productElement(4)!=null) {
Assert.assertEquals("Field 4 should be int", Integer.class,
next.productElement(4).getClass());}
+
+ for (int x = 0; x < 5; x++) {
+ if(testData[recordCount][x]!=null) {
+
Assert.assertEquals(testData[recordCount][x], next.productElement(x));
+ }
+ }
+ recordCount++;
+ }
+ jdbcInputFormat.close();
+ jdbcInputFormat.closeInputFormat();
+ Assert.assertEquals(testData.length, recordCount);
+ }
+
+ @Test
+ public void
testJDBCInputFormatWithParallelismAndNumericColumnSplitting() throws
IOException, InstantiationException, IllegalAccessException {
+ final int fetchSize = 1;
+ final Long min = new Long(JDBCTestBase.testData[0][0] + "");
+ final Long max = new
Long(JDBCTestBase.testData[JDBCTestBase.testData.length - fetchSize][0] + "");
+ ParameterValuesProvider pramProvider = new
NumericBetweenParametersProvider(fetchSize, min, max);
+ jdbcInputFormat = JDBCInputFormat.buildJDBCInputFormat()
+ .setDrivername(DRIVER_CLASS)
+ .setDBUrl(DB_URL)
+
.setQuery(JDBCTestBase.SELECT_ALL_BOOKS_SPLIT_BY_ID)
+ .setParametersProvider(pramProvider)
+
.setResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE)
+ .finish();
+ jdbcInputFormat.openInputFormat();
+ InputSplit[] splits = jdbcInputFormat.createInputSplits(1);
+ 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.productElement(0)!=null) {
Assert.assertEquals("Field 0 should be int", Integer.class,
next.productElement(0).getClass());}
+ if(next.productElement(1)!=null) {
Assert.assertEquals("Field 1 should be String", String.class,
next.productElement(1).getClass());}
+ if(next.productElement(2)!=null) {
Assert.assertEquals("Field 2 should be String", String.class,
next.productElement(2).getClass());}
+ if(next.productElement(3)!=null) {
Assert.assertEquals("Field 3 should be float", Double.class,
next.productElement(3).getClass());}
+ if(next.productElement(4)!=null) {
Assert.assertEquals("Field 4 should be int", Integer.class,
next.productElement(4).getClass());}
+
+ for (int x = 0; x < 5; x++) {
+ if(testData[recordCount][x]!=null) {
+
Assert.assertEquals(testData[recordCount][x], next.productElement(x));
+ }
+ }
+ recordCount++;
+ }
+ jdbcInputFormat.close();
+ }
+ jdbcInputFormat.closeInputFormat();
+ Assert.assertEquals(testData.length, recordCount);
}
+
+ @Test
+ public void testJDBCInputFormatWithParallelismAndGenericSplitting()
throws IOException, InstantiationException, IllegalAccessException {
+ Object[][] queryParameters = new Object[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)
+ .setParametersProvider(paramProvider)
+
.setResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE)
+ .finish();
+ jdbcInputFormat.openInputFormat();
+ InputSplit[] splits = jdbcInputFormat.createInputSplits(1);
--- End diff --
check number of splits
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---