Java Code Examples for org.apache.phoenix.jdbc.PhoenixConnection#setIteratorFactory()

The following examples show how to use org.apache.phoenix.jdbc.PhoenixConnection#setIteratorFactory() . 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: RoundRobinResultIteratorIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testIteratorsPickedInRoundRobinFashionForSaltedTable() throws Exception {
    try (Connection conn = getConnection()) {
        String testTable = "testIteratorsPickedInRoundRobinFashionForSaltedTable".toUpperCase();
        Statement stmt = conn.createStatement();
        stmt.execute("CREATE TABLE " + testTable + "(K VARCHAR PRIMARY KEY) SALT_BUCKETS = 8");
        PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
        MockParallelIteratorFactory parallelIteratorFactory = new MockParallelIteratorFactory();
        phxConn.setIteratorFactory(parallelIteratorFactory);
        ResultSet rs = stmt.executeQuery("SELECT * FROM " + testTable);
        StatementContext ctx = rs.unwrap(PhoenixResultSet.class).getContext();
        PTable table = ctx.getResolver().getTables().get(0).getTable();
        parallelIteratorFactory.setTable(table);
        PhoenixStatement pstmt = stmt.unwrap(PhoenixStatement.class);
        int numIterators = pstmt.getQueryPlan().getSplits().size();
        assertEquals(8, numIterators);
        int numFetches = 2 * numIterators;
        List<String> iteratorOrder = new ArrayList<>(numFetches);
        for (int i = 1; i <= numFetches; i++) {
            rs.next();
            iteratorOrder.add(rs.getString(1));
        }
        /*
         * Because TableResultIterators are created in parallel in multiple threads, their relative order is not
         * deterministic. However, once the iterators are assigned to a RoundRobinResultIterator, the order in which
         * the next iterator is picked is deterministic - i1, i2, .. i7, i8, i1, i2, .. i7, i8, i1, i2, ..
         */
        for (int i = 0; i < numIterators; i++) {
            assertEquals(iteratorOrder.get(i), iteratorOrder.get(i + numIterators));
        }
    }
}
 
Example 2
Source File: RoundRobinResultIteratorWithStatsIT.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Test
public void testRoundRobinBehavior() throws Exception {
    int nRows = 30000;
    try (Connection conn = DriverManager.getConnection(getUrl())) {
        conn.createStatement().execute("CREATE TABLE " + tableName + "(K VARCHAR PRIMARY KEY)");
        PreparedStatement stmt = conn.prepareStatement("UPSERT INTO " + tableName + " VALUES(?)");
        for (int i = 1; i <= nRows; i++) {
            stmt.setString(1, i + "");
            stmt.executeUpdate();
            if ((i % 2000) == 0) {
                conn.commit();
            }
        }
        conn.commit();
        conn.createStatement().execute("UPDATE STATISTICS " + tableName);
        PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
        MockParallelIteratorFactory parallelIteratorFactory = new MockParallelIteratorFactory();
        phxConn.setIteratorFactory(parallelIteratorFactory);
        ResultSet rs = stmt.executeQuery("SELECT * FROM " + tableName);
        StatementContext ctx = rs.unwrap(PhoenixResultSet.class).getContext();
        PTable table = ctx.getResolver().getTables().get(0).getTable();
        parallelIteratorFactory.setTable(table);
        PhoenixStatement pstmt = stmt.unwrap(PhoenixStatement.class);
        int numIterators = pstmt.getQueryPlan().getSplits().size();
        assertTrue(numIterators > 1);
        int numFetches = 2 * numIterators;
        List<String> iteratorOrder = new ArrayList<>(numFetches);
        for (int i = 1; i <= numFetches; i++) {
            rs.next();
            iteratorOrder.add(rs.getString(1));
        }
        /*
         * Because TableResultIterators are created in parallel in multiple threads, their relative order is not
         * deterministic. However, once the iterators are assigned to a RoundRobinResultIterator, the order in which
         * the next iterator is picked is deterministic - i1, i2, .. i7, i8, i1, i2, .. i7, i8, i1, i2, ..
         */
        for (int i = 0; i < numIterators; i++) {
            assertEquals(iteratorOrder.get(i), iteratorOrder.get(i + numIterators));
        }
    }
}