Search in sources :

Example 1 with PeekingResultIterator

use of org.apache.phoenix.iterate.PeekingResultIterator in project phoenix by apache.

the class PhoenixRecordReader method initialize.

public void initialize(InputSplit split) throws IOException {
    final PhoenixInputSplit pSplit = (PhoenixInputSplit) split;
    final List<Scan> scans = pSplit.getScans();
    if (LOG.isInfoEnabled()) {
        LOG.info("Target table : " + queryPlan.getTableRef().getTable().getPhysicalName());
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Scan count[" + scans.size() + "] : " + Bytes.toStringBinary(scans.get(0).getStartRow()) + " ~ " + Bytes.toStringBinary(scans.get(scans.size() - 1).getStopRow()));
        LOG.debug("First scan : " + scans.get(0) + " scanAttribute : " + scans.get(0).getAttributesMap());
        for (int i = 0, limit = scans.size(); i < limit; i++) {
            LOG.debug("EXPECTED_UPPER_REGION_KEY[" + i + "] : " + Bytes.toStringBinary(scans.get(i).getAttribute(BaseScannerRegionObserver.EXPECTED_UPPER_REGION_KEY)));
        }
    }
    try {
        List<PeekingResultIterator> iterators = Lists.newArrayListWithExpectedSize(scans.size());
        StatementContext ctx = queryPlan.getContext();
        ReadMetricQueue readMetrics = ctx.getReadMetricsQueue();
        String tableName = queryPlan.getTableRef().getTable().getPhysicalName().getString();
        long renewScannerLeaseThreshold = queryPlan.getContext().getConnection().getQueryServices().getRenewLeaseThresholdMilliSeconds();
        boolean isRequestMetricsEnabled = readMetrics.isRequestMetricsEnabled();
        for (Scan scan : scans) {
            scan.setAttribute(BaseScannerRegionObserver.SKIP_REGION_BOUNDARY_CHECK, Bytes.toBytes(true));
            ScanMetricsHolder scanMetricsHolder = ScanMetricsHolder.getInstance(readMetrics, tableName, scan, isRequestMetricsEnabled);
            final TableResultIterator tableResultIterator = new TableResultIterator(queryPlan.getContext().getConnection().getMutationState(), scan, scanMetricsHolder, renewScannerLeaseThreshold, queryPlan, MapReduceParallelScanGrouper.getInstance());
            PeekingResultIterator peekingResultIterator = LookAheadResultIterator.wrap(tableResultIterator);
            iterators.add(peekingResultIterator);
        }
        ResultIterator iterator = queryPlan.useRoundRobinIterator() ? RoundRobinResultIterator.newIterator(iterators, queryPlan) : ConcatResultIterator.newIterator(iterators);
        if (queryPlan.getContext().getSequenceManager().getSequenceCount() > 0) {
            iterator = new SequenceResultIterator(iterator, queryPlan.getContext().getSequenceManager());
        }
        this.resultIterator = iterator;
        // Clone the row projector as it's not thread safe and would be used
        // simultaneously by multiple threads otherwise.
        this.resultSet = new PhoenixResultSet(this.resultIterator, queryPlan.getProjector().cloneIfNecessary(), queryPlan.getContext());
    } catch (SQLException e) {
        LOG.error(String.format(" Error [%s] initializing PhoenixRecordReader. ", e.getMessage()));
        Throwables.propagate(e);
    }
}
Also used : ReadMetricQueue(org.apache.phoenix.monitoring.ReadMetricQueue) SQLException(java.sql.SQLException) SequenceResultIterator(org.apache.phoenix.iterate.SequenceResultIterator) SequenceResultIterator(org.apache.phoenix.iterate.SequenceResultIterator) ConcatResultIterator(org.apache.phoenix.iterate.ConcatResultIterator) RoundRobinResultIterator(org.apache.phoenix.iterate.RoundRobinResultIterator) TableResultIterator(org.apache.phoenix.iterate.TableResultIterator) ResultIterator(org.apache.phoenix.iterate.ResultIterator) LookAheadResultIterator(org.apache.phoenix.iterate.LookAheadResultIterator) PeekingResultIterator(org.apache.phoenix.iterate.PeekingResultIterator) TableResultIterator(org.apache.phoenix.iterate.TableResultIterator) ScanMetricsHolder(org.apache.phoenix.monitoring.ScanMetricsHolder) PeekingResultIterator(org.apache.phoenix.iterate.PeekingResultIterator) StatementContext(org.apache.phoenix.compile.StatementContext) PhoenixResultSet(org.apache.phoenix.jdbc.PhoenixResultSet) Scan(org.apache.hadoop.hbase.client.Scan)

Example 2 with PeekingResultIterator

use of org.apache.phoenix.iterate.PeekingResultIterator in project phoenix by apache.

the class MutatingParallelIteratorFactory method newIterator.

@Override
public PeekingResultIterator newIterator(final StatementContext parentContext, ResultIterator iterator, Scan scan, String tableName, QueryPlan plan) throws SQLException {
    final PhoenixConnection clonedConnection = new PhoenixConnection(this.connection);
    MutationState state = mutate(parentContext, iterator, clonedConnection);
    long totalRowCount = state.getUpdateCount();
    if (clonedConnection.getAutoCommit()) {
        clonedConnection.getMutationState().join(state);
        state = clonedConnection.getMutationState();
    }
    final MutationState finalState = state;
    byte[] value = PLong.INSTANCE.toBytes(totalRowCount);
    KeyValue keyValue = KeyValueUtil.newKeyValue(UNGROUPED_AGG_ROW_KEY, SINGLE_COLUMN_FAMILY, SINGLE_COLUMN, AGG_TIMESTAMP, value, 0, value.length);
    final Tuple tuple = new SingleKeyValueTuple(keyValue);
    return new PeekingResultIterator() {

        private boolean done = false;

        @Override
        public Tuple next() throws SQLException {
            if (done) {
                return null;
            }
            done = true;
            return tuple;
        }

        @Override
        public void explain(List<String> planSteps) {
        }

        @Override
        public void close() throws SQLException {
            try {
                /* 
                     * Join the child mutation states in close, since this is called in a single threaded manner
                     * after the parallel results have been processed. 
                     * If auto-commit is on for the cloned child connection, then the finalState here is an empty mutation 
                     * state (with no mutations). However, it still has the metrics for mutation work done by the 
                     * mutating-iterator. Joining the mutation state makes sure those metrics are passed over
                     * to the parent connection.
                     */
                MutatingParallelIteratorFactory.this.connection.getMutationState().join(finalState);
            } finally {
                clonedConnection.close();
            }
        }

        @Override
        public Tuple peek() throws SQLException {
            return done ? null : tuple;
        }
    };
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) KeyValue(org.apache.hadoop.hbase.KeyValue) MutationState(org.apache.phoenix.execute.MutationState) SingleKeyValueTuple(org.apache.phoenix.schema.tuple.SingleKeyValueTuple) List(java.util.List) Tuple(org.apache.phoenix.schema.tuple.Tuple) SingleKeyValueTuple(org.apache.phoenix.schema.tuple.SingleKeyValueTuple) PeekingResultIterator(org.apache.phoenix.iterate.PeekingResultIterator)

Aggregations

PeekingResultIterator (org.apache.phoenix.iterate.PeekingResultIterator)2 SQLException (java.sql.SQLException)1 List (java.util.List)1 KeyValue (org.apache.hadoop.hbase.KeyValue)1 Scan (org.apache.hadoop.hbase.client.Scan)1 StatementContext (org.apache.phoenix.compile.StatementContext)1 MutationState (org.apache.phoenix.execute.MutationState)1 ConcatResultIterator (org.apache.phoenix.iterate.ConcatResultIterator)1 LookAheadResultIterator (org.apache.phoenix.iterate.LookAheadResultIterator)1 ResultIterator (org.apache.phoenix.iterate.ResultIterator)1 RoundRobinResultIterator (org.apache.phoenix.iterate.RoundRobinResultIterator)1 SequenceResultIterator (org.apache.phoenix.iterate.SequenceResultIterator)1 TableResultIterator (org.apache.phoenix.iterate.TableResultIterator)1 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)1 PhoenixResultSet (org.apache.phoenix.jdbc.PhoenixResultSet)1 ReadMetricQueue (org.apache.phoenix.monitoring.ReadMetricQueue)1 ScanMetricsHolder (org.apache.phoenix.monitoring.ScanMetricsHolder)1 SingleKeyValueTuple (org.apache.phoenix.schema.tuple.SingleKeyValueTuple)1 Tuple (org.apache.phoenix.schema.tuple.Tuple)1