Example usage for java.sql ResultSet unwrap

List of usage examples for java.sql ResultSet unwrap

Introduction

In this page you can find the example usage for java.sql ResultSet unwrap.

Prototype

<T> T unwrap(java.lang.Class<T> iface) throws java.sql.SQLException;

Source Link

Document

Returns an object that implements the given interface to allow access to non-standard methods, or standard methods not exposed by the proxy.

Usage

From source file:com.github.woonsan.jdbc.jcr.impl.JcrJdbcResultSetTest.java

@Test
public void testWrapper() throws Exception {
    Statement statement = getConnection().createStatement();
    ResultSet rs = statement.executeQuery(SQL_EMPS);

    assertTrue(rs.isWrapperFor(JcrResultSet.class));

    try {//  w w  w.  j  a v  a 2  s . com
        rs.isWrapperFor(null);
        fail();
    } catch (IllegalArgumentException ignore) {
    }

    assertFalse(rs.isWrapperFor(QueryResult.class));

    JcrResultSet jrs = rs.unwrap(JcrResultSet.class);
    assertNotNull(jrs);

    int count = 0;

    while (jrs.next()) {
        ++count;

        Node node = jrs.getCurrentRow().getNode();

        String nodeName = node.getName();
        String nodePath = node.getPath();
        String nodeId = node.getIdentifier();
        double score = jrs.getCurrentRow().getScore();

        assertEquals("testdata-" + count, nodeName);
        assertEquals("/testdatafolder/" + nodeName, nodePath);
        assertTrue(nodeId != null && !nodeId.isEmpty());
        assertTrue(score > 0.0);

    }

    rs.close();
    statement.close();
}

From source file:org.apache.phoenix.mapreduce.index.IndexScrutinyMapper.java

private void queryTargetTable(Context context, PreparedStatement targetStatement,
        Map<String, Pair<Long, List<Object>>> targetPkToSourceValues)
        throws SQLException, IOException, InterruptedException {
    ResultSet targetResultSet = targetStatement.executeQuery();

    while (targetResultSet.next()) {
        indxWritable.readFields(targetResultSet);
        List<Object> targetValues = indxWritable.getValues();
        // first grab the PK and try to join against the source input
        // the query is such that first numTargetPkCols of the resultSet is the PK
        List<Object> pkObjects = new ArrayList<>(numTargetPkCols);
        for (int i = 0; i < numTargetPkCols; i++) {
            Object pkPart = targetResultSet.getObject(i + 1);
            pkObjects.add(pkPart);/*from   w  w  w.  j a va  2  s .co  m*/
        }
        Long targetTS = targetResultSet.unwrap(PhoenixResultSet.class).getCurrentRow().getValue(0)
                .getTimestamp();
        String targetPk = getPkHash(pkObjects);

        // use the pk to fetch the source table column values
        Pair<Long, List<Object>> sourceTsValues = targetPkToSourceValues.get(targetPk);

        Long sourceTS = sourceTsValues.getFirst();
        List<Object> sourceValues = sourceTsValues.getSecond();
        // compare values starting after the PK (i.e. covered columns)
        boolean isIndexedCorrectly = compareValues(numTargetPkCols, targetValues, sourceValues, context);
        if (isIndexedCorrectly) {
            context.getCounter(PhoenixScrutinyJobCounters.VALID_ROW_COUNT).increment(1);
        } else {
            context.getCounter(PhoenixScrutinyJobCounters.INVALID_ROW_COUNT).increment(1);
            if (outputInvalidRows) {
                outputInvalidRow(context, sourceValues, targetValues, sourceTS, targetTS);
            }
        }
        targetPkToSourceValues.remove(targetPk);
    }
}

From source file:org.apache.phoenix.monitoring.PhoenixMetricsIT.java

@Test
public void testReadMetricsForSelect() throws Exception {
    String tableName = generateUniqueName();
    long numSaltBuckets = 6;
    String ddl = "CREATE TABLE " + tableName + " (K VARCHAR NOT NULL PRIMARY KEY, V VARCHAR)"
            + " SALT_BUCKETS = " + numSaltBuckets;
    Connection conn = DriverManager.getConnection(getUrl());
    conn.createStatement().execute(ddl);

    long numRows = 1000;
    long numExpectedTasks = numSaltBuckets;
    insertRowsInTable(tableName, numRows);

    String query = "SELECT * FROM " + tableName;
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery(query);
    PhoenixResultSet resultSetBeingTested = rs.unwrap(PhoenixResultSet.class);
    changeInternalStateForTesting(resultSetBeingTested);
    while (resultSetBeingTested.next()) {
    }// www .  jav  a  2s.c  o m
    resultSetBeingTested.close();
    Set<String> expectedTableNames = Sets.newHashSet(tableName);
    assertReadMetricValuesForSelectSql(Lists.newArrayList(numRows), Lists.newArrayList(numExpectedTasks),
            resultSetBeingTested, expectedTableNames);
}

From source file:org.apache.phoenix.util.PhoenixRuntime.java

/**
 * Method to expose the metrics associated with performing reads using the passed result set. A typical pattern is:
 * //from   ww  w.j a  va2 s .com
 * <pre>
 * {@code
 * Map<String, Map<String, Long>> overAllQueryMetrics = null;
 * Map<String, Map<String, Long>> requestReadMetrics = null;
 * try (ResultSet rs = stmt.executeQuery()) {
 *    while(rs.next()) {
 *      .....
 *    }
 *    overAllQueryMetrics = PhoenixRuntime.getOverAllReadRequestMetrics(rs);
 *    requestReadMetrics = PhoenixRuntime.getRequestReadMetrics(rs);
 *    PhoenixRuntime.resetMetrics(rs);
 * }
 * </pre>
 * 
 * @param rs
 *            result set to get the metrics for
 * @return a map of (table name) -> (map of (metric name) -> (metric value))
 * @throws SQLException
 */
public static Map<String, Map<String, Long>> getRequestReadMetrics(ResultSet rs) throws SQLException {
    PhoenixResultSet resultSet = rs.unwrap(PhoenixResultSet.class);
    return resultSet.getReadMetrics();
}

From source file:org.apache.phoenix.util.PhoenixRuntime.java

/**
 * Method to expose the overall metrics associated with executing a query via phoenix. A typical pattern of
 * accessing request level read metrics and overall read query metrics is:
 * //from w  ww  .  j ava 2 s  .  co m
 * <pre>
 * {@code
 * Map<String, Map<String, Long>> overAllQueryMetrics = null;
 * Map<String, Map<String, Long>> requestReadMetrics = null;
 * try (ResultSet rs = stmt.executeQuery()) {
 *    while(rs.next()) {
 *      .....
 *    }
 *    overAllQueryMetrics = PhoenixRuntime.getOverAllReadRequestMetrics(rs);
 *    requestReadMetrics = PhoenixRuntime.getRequestReadMetrics(rs);
 *    PhoenixRuntime.resetMetrics(rs);
 * }
 * </pre>
 * 
 * @param rs
 *            result set to get the metrics for
 * @return a map of metric name -> metric value
 * @throws SQLException
 */
public static Map<String, Long> getOverAllReadRequestMetrics(ResultSet rs) throws SQLException {
    PhoenixResultSet resultSet = rs.unwrap(PhoenixResultSet.class);
    return resultSet.getOverAllRequestReadMetrics();
}

From source file:org.apache.phoenix.util.PhoenixRuntime.java

/**
 * Reset the read metrics collected in the result set.
 * //  w ww.  j a  v  a  2  s.  c  om
 * @see {@link #getRequestReadMetrics(ResultSet)} {@link #getOverAllReadRequestMetrics(ResultSet)}
 * @param rs
 * @throws SQLException
 */
public static void resetMetrics(ResultSet rs) throws SQLException {
    PhoenixResultSet prs = rs.unwrap(PhoenixResultSet.class);
    prs.resetMetrics();
}