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);
}
}
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;
}
};
}
Aggregations