org.apache.flink.api.java.io.jdbc.split.NumericBetweenParametersProvider Java Examples
The following examples show how to use
org.apache.flink.api.java.io.jdbc.split.NumericBetweenParametersProvider.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JDBCInputFormatTest.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
@Test public void testJDBCInputFormatWithParallelismAndNumericColumnSplitting() throws IOException { final int fetchSize = 1; final long min = TEST_DATA[0].id; final long max = TEST_DATA[TEST_DATA.length - fetchSize].id; ParameterValuesProvider pramProvider = new NumericBetweenParametersProvider(fetchSize, min, max); jdbcInputFormat = JDBCInputFormat.buildJDBCInputFormat() .setDrivername(DRIVER_CLASS) .setDBUrl(DB_URL) .setQuery(JDBCTestBase.SELECT_ALL_BOOKS_SPLIT_BY_ID) .setRowTypeInfo(ROW_TYPE_INFO) .setParametersProvider(pramProvider) .setResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE) .finish(); jdbcInputFormat.openInputFormat(); InputSplit[] splits = jdbcInputFormat.createInputSplits(1); //this query exploit parallelism (1 split for every id) Assert.assertEquals(TEST_DATA.length, splits.length); int recordCount = 0; Row row = new Row(5); for (InputSplit split : splits) { jdbcInputFormat.open(split); while (!jdbcInputFormat.reachedEnd()) { Row next = jdbcInputFormat.nextRecord(row); assertEquals(TEST_DATA[recordCount], next); recordCount++; } jdbcInputFormat.close(); } jdbcInputFormat.closeInputFormat(); Assert.assertEquals(TEST_DATA.length, recordCount); }
Example #2
Source File: JDBCInputFormatTest.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
@Test public void testJDBCInputFormatWithoutParallelismAndNumericColumnSplitting() throws IOException { final long min = TEST_DATA[0].id; final long max = TEST_DATA[TEST_DATA.length - 1].id; final long fetchSize = max + 1; //generate a single split ParameterValuesProvider pramProvider = new NumericBetweenParametersProvider(fetchSize, min, max); jdbcInputFormat = JDBCInputFormat.buildJDBCInputFormat() .setDrivername(DRIVER_CLASS) .setDBUrl(DB_URL) .setQuery(JDBCTestBase.SELECT_ALL_BOOKS_SPLIT_BY_ID) .setRowTypeInfo(ROW_TYPE_INFO) .setParametersProvider(pramProvider) .setResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE) .finish(); jdbcInputFormat.openInputFormat(); InputSplit[] splits = jdbcInputFormat.createInputSplits(1); //assert that a single split was generated Assert.assertEquals(1, splits.length); int recordCount = 0; Row row = new Row(5); for (InputSplit split : splits) { jdbcInputFormat.open(split); while (!jdbcInputFormat.reachedEnd()) { Row next = jdbcInputFormat.nextRecord(row); assertEquals(TEST_DATA[recordCount], next); recordCount++; } jdbcInputFormat.close(); } jdbcInputFormat.closeInputFormat(); Assert.assertEquals(TEST_DATA.length, recordCount); }
Example #3
Source File: JDBCTableSource.java From flink with Apache License 2.0 | 5 votes |
private JDBCInputFormat getInputFormat() { JDBCInputFormat.JDBCInputFormatBuilder builder = JDBCInputFormat.buildJDBCInputFormat() .setDrivername(options.getDriverName()) .setDBUrl(options.getDbURL()) .setUsername(options.getUsername()) .setPassword(options.getPassword()) .setRowTypeInfo(new RowTypeInfo(returnType.getFieldTypes(), returnType.getFieldNames())); if (readOptions.getFetchSize() != 0) { builder.setFetchSize(readOptions.getFetchSize()); } final JDBCDialect dialect = options.getDialect(); String query = dialect.getSelectFromStatement( options.getTableName(), returnType.getFieldNames(), new String[0]); if (readOptions.getPartitionColumnName().isPresent()) { long lowerBound = readOptions.getPartitionLowerBound().get(); long upperBound = readOptions.getPartitionUpperBound().get(); int numPartitions = readOptions.getNumPartitions().get(); builder.setParametersProvider( new NumericBetweenParametersProvider(lowerBound, upperBound).ofBatchNum(numPartitions)); query += " WHERE " + dialect.quoteIdentifier(readOptions.getPartitionColumnName().get()) + " BETWEEN ? AND ?"; } builder.setQuery(query); return builder.finish(); }
Example #4
Source File: JDBCInputFormatTest.java From flink with Apache License 2.0 | 5 votes |
@Test public void testJDBCInputFormatWithParallelismAndNumericColumnSplitting() throws IOException { final int fetchSize = 1; final long min = TEST_DATA[0].id; final long max = TEST_DATA[TEST_DATA.length - fetchSize].id; ParameterValuesProvider pramProvider = new NumericBetweenParametersProvider(min, max).ofBatchSize(fetchSize); jdbcInputFormat = JDBCInputFormat.buildJDBCInputFormat() .setDrivername(DRIVER_CLASS) .setDBUrl(DB_URL) .setQuery(JDBCTestBase.SELECT_ALL_BOOKS_SPLIT_BY_ID) .setRowTypeInfo(ROW_TYPE_INFO) .setParametersProvider(pramProvider) .setResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE) .finish(); jdbcInputFormat.openInputFormat(); InputSplit[] splits = jdbcInputFormat.createInputSplits(1); //this query exploit parallelism (1 split for every id) Assert.assertEquals(TEST_DATA.length, splits.length); int recordCount = 0; Row row = new Row(5); for (InputSplit split : splits) { jdbcInputFormat.open(split); while (!jdbcInputFormat.reachedEnd()) { Row next = jdbcInputFormat.nextRecord(row); assertEquals(TEST_DATA[recordCount], next); recordCount++; } jdbcInputFormat.close(); } jdbcInputFormat.closeInputFormat(); Assert.assertEquals(TEST_DATA.length, recordCount); }
Example #5
Source File: JDBCInputFormatTest.java From flink with Apache License 2.0 | 5 votes |
@Test public void testJDBCInputFormatWithoutParallelismAndNumericColumnSplitting() throws IOException { final long min = TEST_DATA[0].id; final long max = TEST_DATA[TEST_DATA.length - 1].id; final long fetchSize = max + 1; //generate a single split ParameterValuesProvider pramProvider = new NumericBetweenParametersProvider(min, max).ofBatchSize(fetchSize); jdbcInputFormat = JDBCInputFormat.buildJDBCInputFormat() .setDrivername(DRIVER_CLASS) .setDBUrl(DB_URL) .setQuery(JDBCTestBase.SELECT_ALL_BOOKS_SPLIT_BY_ID) .setRowTypeInfo(ROW_TYPE_INFO) .setParametersProvider(pramProvider) .setResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE) .finish(); jdbcInputFormat.openInputFormat(); InputSplit[] splits = jdbcInputFormat.createInputSplits(1); //assert that a single split was generated Assert.assertEquals(1, splits.length); int recordCount = 0; Row row = new Row(5); for (InputSplit split : splits) { jdbcInputFormat.open(split); while (!jdbcInputFormat.reachedEnd()) { Row next = jdbcInputFormat.nextRecord(row); assertEquals(TEST_DATA[recordCount], next); recordCount++; } jdbcInputFormat.close(); } jdbcInputFormat.closeInputFormat(); Assert.assertEquals(TEST_DATA.length, recordCount); }
Example #6
Source File: JDBCInputFormatTest.java From flink with Apache License 2.0 | 5 votes |
@Test public void testJDBCInputFormatWithParallelismAndNumericColumnSplitting() throws IOException { final int fetchSize = 1; final long min = TEST_DATA[0].id; final long max = TEST_DATA[TEST_DATA.length - fetchSize].id; ParameterValuesProvider pramProvider = new NumericBetweenParametersProvider(min, max).ofBatchSize(fetchSize); jdbcInputFormat = JDBCInputFormat.buildJDBCInputFormat() .setDrivername(DERBY_EBOOKSHOP_DB.getDriverClass()) .setDBUrl(DERBY_EBOOKSHOP_DB.getUrl()) .setQuery(SELECT_ALL_BOOKS_SPLIT_BY_ID) .setRowTypeInfo(ROW_TYPE_INFO) .setParametersProvider(pramProvider) .setResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE) .finish(); jdbcInputFormat.openInputFormat(); InputSplit[] splits = jdbcInputFormat.createInputSplits(1); //this query exploit parallelism (1 split for every id) Assert.assertEquals(TEST_DATA.length, splits.length); int recordCount = 0; Row row = new Row(5); for (InputSplit split : splits) { jdbcInputFormat.open(split); while (!jdbcInputFormat.reachedEnd()) { Row next = jdbcInputFormat.nextRecord(row); assertEquals(TEST_DATA[recordCount], next); recordCount++; } jdbcInputFormat.close(); } jdbcInputFormat.closeInputFormat(); Assert.assertEquals(TEST_DATA.length, recordCount); }
Example #7
Source File: JDBCInputFormatTest.java From flink with Apache License 2.0 | 5 votes |
@Test public void testJDBCInputFormatWithoutParallelismAndNumericColumnSplitting() throws IOException { final long min = TEST_DATA[0].id; final long max = TEST_DATA[TEST_DATA.length - 1].id; final long fetchSize = max + 1; //generate a single split ParameterValuesProvider pramProvider = new NumericBetweenParametersProvider(min, max).ofBatchSize(fetchSize); jdbcInputFormat = JDBCInputFormat.buildJDBCInputFormat() .setDrivername(DERBY_EBOOKSHOP_DB.getDriverClass()) .setDBUrl(DERBY_EBOOKSHOP_DB.getUrl()) .setQuery(SELECT_ALL_BOOKS_SPLIT_BY_ID) .setRowTypeInfo(ROW_TYPE_INFO) .setParametersProvider(pramProvider) .setResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE) .finish(); jdbcInputFormat.openInputFormat(); InputSplit[] splits = jdbcInputFormat.createInputSplits(1); //assert that a single split was generated Assert.assertEquals(1, splits.length); int recordCount = 0; Row row = new Row(5); for (InputSplit split : splits) { jdbcInputFormat.open(split); while (!jdbcInputFormat.reachedEnd()) { Row next = jdbcInputFormat.nextRecord(row); assertEquals(TEST_DATA[recordCount], next); recordCount++; } jdbcInputFormat.close(); } jdbcInputFormat.closeInputFormat(); Assert.assertEquals(TEST_DATA.length, recordCount); }
Example #8
Source File: JDBCFullTest.java From Flink-CEPplus with Apache License 2.0 | 4 votes |
private void runTest(boolean exploitParallelism) throws Exception { ExecutionEnvironment environment = ExecutionEnvironment.getExecutionEnvironment(); JDBCInputFormatBuilder inputBuilder = JDBCInputFormat.buildJDBCInputFormat() .setDrivername(JDBCTestBase.DRIVER_CLASS) .setDBUrl(JDBCTestBase.DB_URL) .setQuery(JDBCTestBase.SELECT_ALL_BOOKS) .setRowTypeInfo(ROW_TYPE_INFO); if (exploitParallelism) { final int fetchSize = 1; final long min = JDBCTestBase.TEST_DATA[0].id; final long max = JDBCTestBase.TEST_DATA[JDBCTestBase.TEST_DATA.length - fetchSize].id; //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 databases don't null values correctly when no column type was 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()); environment.execute(); 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.TEST_DATA.length, count); } }
Example #9
Source File: JDBCFullTest.java From flink with Apache License 2.0 | 4 votes |
private void runTest(boolean exploitParallelism) throws Exception { ExecutionEnvironment environment = ExecutionEnvironment.getExecutionEnvironment(); JDBCInputFormatBuilder inputBuilder = JDBCInputFormat.buildJDBCInputFormat() .setDrivername(JDBCTestBase.DRIVER_CLASS) .setDBUrl(JDBCTestBase.DB_URL) .setQuery(JDBCTestBase.SELECT_ALL_BOOKS) .setRowTypeInfo(ROW_TYPE_INFO); if (exploitParallelism) { final int fetchSize = 1; final long min = JDBCTestBase.TEST_DATA[0].id; final long max = JDBCTestBase.TEST_DATA[JDBCTestBase.TEST_DATA.length - fetchSize].id; //use a "splittable" query to exploit parallelism inputBuilder = inputBuilder .setQuery(JDBCTestBase.SELECT_ALL_BOOKS_SPLIT_BY_ID) .setParametersProvider(new NumericBetweenParametersProvider(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) 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()); environment.execute(); 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.TEST_DATA.length, count); } }