View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.client;
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.NavigableSet;
29  import java.util.TreeMap;
30  import java.util.TreeSet;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.apache.hadoop.hbase.classification.InterfaceAudience;
35  import org.apache.hadoop.hbase.classification.InterfaceStability;
36  import org.apache.hadoop.hbase.HConstants;
37  import org.apache.hadoop.hbase.client.metrics.ScanMetrics;
38  import org.apache.hadoop.hbase.filter.Filter;
39  import org.apache.hadoop.hbase.filter.IncompatibleFilterException;
40  import org.apache.hadoop.hbase.io.TimeRange;
41  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
42  import org.apache.hadoop.hbase.security.access.Permission;
43  import org.apache.hadoop.hbase.security.visibility.Authorizations;
44  import org.apache.hadoop.hbase.util.Bytes;
45  
46  /**
47   * Used to perform Scan operations.
48   * <p>
49   * All operations are identical to {@link Get} with the exception of
50   * instantiation.  Rather than specifying a single row, an optional startRow
51   * and stopRow may be defined.  If rows are not specified, the Scanner will
52   * iterate over all rows.
53   * <p>
54   * To get all columns from all rows of a Table, create an instance with no constraints; use the
55   * {@link #Scan()} constructor. To constrain the scan to specific column families,
56   * call {@link #addFamily(byte[]) addFamily} for each family to retrieve on your Scan instance.
57   * <p>
58   * To get specific columns, call {@link #addColumn(byte[], byte[]) addColumn}
59   * for each column to retrieve.
60   * <p>
61   * To only retrieve columns within a specific range of version timestamps,
62   * call {@link #setTimeRange(long, long) setTimeRange}.
63   * <p>
64   * To only retrieve columns with a specific timestamp, call
65   * {@link #setTimeStamp(long) setTimestamp}.
66   * <p>
67   * To limit the number of versions of each column to be returned, call
68   * {@link #setMaxVersions(int) setMaxVersions}.
69   * <p>
70   * To limit the maximum number of values returned for each call to next(),
71   * call {@link #setBatch(int) setBatch}.
72   * <p>
73   * To add a filter, call {@link #setFilter(org.apache.hadoop.hbase.filter.Filter) setFilter}.
74   * <p>
75   * Expert: To explicitly disable server-side block caching for this scan,
76   * execute {@link #setCacheBlocks(boolean)}.
77   * <p><em>Note:</em> Usage alters Scan instances. Internally, attributes are updated as the Scan
78   * runs and if enabled, metrics accumulate in the Scan instance. Be aware this is the case when
79   * you go to clone a Scan instance or if you go to reuse a created Scan instance; safer is create
80   * a Scan instance per usage.
81   */
82  @InterfaceAudience.Public
83  @InterfaceStability.Stable
84  public class Scan extends Query {
85    private static final Log LOG = LogFactory.getLog(Scan.class);
86  
87    private static final String RAW_ATTR = "_raw_";
88  
89    private byte[] startRow = HConstants.EMPTY_START_ROW;
90    private boolean includeStartRow = true;
91    private byte[] stopRow  = HConstants.EMPTY_END_ROW;
92    private boolean includeStopRow = false;
93    private int maxVersions = 1;
94    private int batch = -1;
95  
96    /**
97     * Partial {@link Result}s are {@link Result}s must be combined to form a complete {@link Result}.
98     * The {@link Result}s had to be returned in fragments (i.e. as partials) because the size of the
99     * cells in the row exceeded max result size on the server. Typically partial results will be
100    * combined client side into complete results before being delivered to the caller. However, if
101    * this flag is set, the caller is indicating that they do not mind seeing partial results (i.e.
102    * they understand that the results returned from the Scanner may only represent part of a
103    * particular row). In such a case, any attempt to combine the partials into a complete result on
104    * the client side will be skipped, and the caller will be able to see the exact results returned
105    * from the server.
106    */
107   private boolean allowPartialResults = false;
108 
109   private int storeLimit = -1;
110   private int storeOffset = 0;
111   private boolean getScan;
112 
113   /**
114    * @deprecated since 1.0.0. Use {@link #setScanMetricsEnabled(boolean)}
115    */
116   // Make private or remove.
117   @Deprecated
118   static public final String SCAN_ATTRIBUTES_METRICS_ENABLE = "scan.attributes.metrics.enable";
119 
120   /**
121    * Use {@link #getScanMetrics()}
122    */
123   // Make this private or remove.
124   @Deprecated
125   static public final String SCAN_ATTRIBUTES_METRICS_DATA = "scan.attributes.metrics.data";
126 
127   // If an application wants to use multiple scans over different tables each scan must
128   // define this attribute with the appropriate table name by calling
129   // scan.setAttribute(Scan.SCAN_ATTRIBUTES_TABLE_NAME, Bytes.toBytes(tableName))
130   static public final String SCAN_ATTRIBUTES_TABLE_NAME = "scan.attributes.table.name";
131 
132   /**
133    * @deprecated without replacement
134    *             This is now a no-op, SEEKs and SKIPs are optimizated automatically.
135    *             Will be removed in 2.0+
136    */
137   @Deprecated
138   public static final String HINT_LOOKAHEAD = "_look_ahead_";
139 
140   /*
141    * -1 means no caching specified and the value of {@link HConstants#HBASE_CLIENT_SCANNER_CACHING}
142    * (default to {@link HConstants#DEFAULT_HBASE_CLIENT_SCANNER_CACHING}) will be used
143    */
144   private int caching = -1;
145   private long maxResultSize = -1;
146   private boolean cacheBlocks = true;
147   private boolean reversed = false;
148   private TimeRange tr = new TimeRange();
149   private Map<byte [], NavigableSet<byte []>> familyMap =
150     new TreeMap<byte [], NavigableSet<byte []>>(Bytes.BYTES_COMPARATOR);
151 
152   /**
153    * Set it true for small scan to get better performance
154    *
155    * Small scan should use pread and big scan can use seek + read
156    *
157    * seek + read is fast but can cause two problem (1) resource contention (2)
158    * cause too much network io
159    *
160    * [89-fb] Using pread for non-compaction read request
161    * https://issues.apache.org/jira/browse/HBASE-7266
162    *
163    * On the other hand, if setting it true, we would do
164    * openScanner,next,closeScanner in one RPC call. It means the better
165    * performance for small scan. [HBASE-9488].
166    *
167    * Generally, if the scan range is within one data block(64KB), it could be
168    * considered as a small scan.
169    */
170   private boolean small = false;
171 
172   /**
173    * The mvcc read point to use when open a scanner. Remember to clear it after switching regions as
174    * the mvcc is only valid within region scope.
175    */
176   private long mvccReadPoint = -1L;
177 
178   /**
179    * The number of rows we want for this scan. We will terminate the scan if the number of return
180    * rows reaches this value.
181    */
182   private int limit = -1;
183 
184   /**
185    * Control whether to use pread at server side.
186    */
187   private ReadType readType = ReadType.DEFAULT;
188 
189   private boolean needCursorResult = false;
190 
191   /**
192    * Create a Scan operation across all rows.
193    */
194   public Scan() {}
195 
196   /**
197    * @deprecated use {@code new Scan().withStartRow(startRow).setFilter(filter)} instead.
198    */
199   @Deprecated
200   public Scan(byte[] startRow, Filter filter) {
201     this(startRow);
202     this.filter = filter;
203   }
204 
205   /**
206    * Create a Scan operation starting at the specified row.
207    * <p>
208    * If the specified row does not exist, the Scanner will start from the next closest row after the
209    * specified row.
210    * @param startRow row to start scanner at or after
211    * @deprecated use {@code new Scan().withStartRow(startRow)} instead.
212    */
213   @Deprecated
214   public Scan(byte[] startRow) {
215     setStartRow(startRow);
216   }
217 
218   /**
219    * Create a Scan operation for the range of rows specified.
220    * @param startRow row to start scanner at or after (inclusive)
221    * @param stopRow row to stop scanner before (exclusive)
222    * @deprecated use {@code new Scan().withStartRow(startRow).withStopRow(stopRow)} instead.
223    */
224   @Deprecated
225   public Scan(byte[] startRow, byte[] stopRow) {
226     setStartRow(startRow);
227     setStopRow(stopRow);
228   }
229 
230   /**
231    * Creates a new instance of this class while copying all values.
232    *
233    * @param scan  The scan instance to copy from.
234    * @throws IOException When copying the values fails.
235    */
236   public Scan(Scan scan) throws IOException {
237     startRow = scan.getStartRow();
238     includeStartRow = scan.includeStartRow();
239     stopRow  = scan.getStopRow();
240     includeStopRow = scan.includeStopRow();
241     maxVersions = scan.getMaxVersions();
242     batch = scan.getBatch();
243     storeLimit = scan.getMaxResultsPerColumnFamily();
244     storeOffset = scan.getRowOffsetPerColumnFamily();
245     caching = scan.getCaching();
246     maxResultSize = scan.getMaxResultSize();
247     cacheBlocks = scan.getCacheBlocks();
248     getScan = scan.isGetScan();
249     filter = scan.getFilter(); // clone?
250     loadColumnFamiliesOnDemand = scan.getLoadColumnFamiliesOnDemandValue();
251     consistency = scan.getConsistency();
252     this.setIsolationLevel(scan.getIsolationLevel());
253     reversed = scan.isReversed();
254     small = scan.isSmall();
255     allowPartialResults = scan.getAllowPartialResults();
256     TimeRange ctr = scan.getTimeRange();
257     tr = new TimeRange(ctr.getMin(), ctr.getMax());
258     Map<byte[], NavigableSet<byte[]>> fams = scan.getFamilyMap();
259     for (Map.Entry<byte[],NavigableSet<byte[]>> entry : fams.entrySet()) {
260       byte [] fam = entry.getKey();
261       NavigableSet<byte[]> cols = entry.getValue();
262       if (cols != null && cols.size() > 0) {
263         for (byte[] col : cols) {
264           addColumn(fam, col);
265         }
266       } else {
267         addFamily(fam);
268       }
269     }
270     for (Map.Entry<String, byte[]> attr : scan.getAttributesMap().entrySet()) {
271       setAttribute(attr.getKey(), attr.getValue());
272     }
273     for (Map.Entry<byte[], TimeRange> entry : scan.getColumnFamilyTimeRange().entrySet()) {
274       TimeRange tr = entry.getValue();
275       setColumnFamilyTimeRange(entry.getKey(), tr.getMin(), tr.getMax());
276     }
277     this.mvccReadPoint = scan.getMvccReadPoint();
278     this.limit = scan.getLimit();
279     this.needCursorResult = scan.isNeedCursorResult();
280     setPriority(scan.getPriority());
281   }
282 
283   /**
284    * Builds a scan object with the same specs as get.
285    * @param get get to model scan after
286    */
287   public Scan(Get get) {
288     this.startRow = get.getRow();
289     this.includeStartRow = true;
290     this.stopRow = get.getRow();
291     this.includeStopRow = true;
292     this.filter = get.getFilter();
293     this.cacheBlocks = get.getCacheBlocks();
294     this.maxVersions = get.getMaxVersions();
295     this.storeLimit = get.getMaxResultsPerColumnFamily();
296     this.storeOffset = get.getRowOffsetPerColumnFamily();
297     this.tr = get.getTimeRange();
298     this.familyMap = get.getFamilyMap();
299     this.getScan = true;
300     this.consistency = get.getConsistency();
301     this.setIsolationLevel(get.getIsolationLevel());
302     this.loadColumnFamiliesOnDemand = get.getLoadColumnFamiliesOnDemandValue();
303     for (Map.Entry<String, byte[]> attr : get.getAttributesMap().entrySet()) {
304       setAttribute(attr.getKey(), attr.getValue());
305     }
306     for (Map.Entry<byte[], TimeRange> entry : get.getColumnFamilyTimeRange().entrySet()) {
307       TimeRange tr = entry.getValue();
308       setColumnFamilyTimeRange(entry.getKey(), tr.getMin(), tr.getMax());
309     }
310     this.mvccReadPoint = -1L;
311     setPriority(get.getPriority());
312   }
313 
314   public boolean isGetScan() {
315     return includeStartRow && includeStopRow
316         && ClientUtil.areScanStartRowAndStopRowEqual(this.startRow, this.stopRow);
317   }
318 
319   /**
320    * Get all columns from the specified family.
321    * <p>
322    * Overrides previous calls to addColumn for this family.
323    * @param family family name
324    * @return this
325    */
326   public Scan addFamily(byte [] family) {
327     familyMap.remove(family);
328     familyMap.put(family, null);
329     return this;
330   }
331 
332   /**
333    * Get the column from the specified family with the specified qualifier.
334    * <p>
335    * Overrides previous calls to addFamily for this family.
336    * @param family family name
337    * @param qualifier column qualifier
338    * @return this
339    */
340   public Scan addColumn(byte [] family, byte [] qualifier) {
341     NavigableSet<byte []> set = familyMap.get(family);
342     if(set == null) {
343       set = new TreeSet<byte []>(Bytes.BYTES_COMPARATOR);
344       familyMap.put(family, set);
345     }
346     if (qualifier == null) {
347       qualifier = HConstants.EMPTY_BYTE_ARRAY;
348     }
349     set.add(qualifier);
350     return this;
351   }
352 
353   /**
354    * Get versions of columns only within the specified timestamp range,
355    * [minStamp, maxStamp).  Note, default maximum versions to return is 1.  If
356    * your time range spans more than one version and you want all versions
357    * returned, up the number of versions beyond the default.
358    * @param minStamp minimum timestamp value, inclusive
359    * @param maxStamp maximum timestamp value, exclusive
360    * @see #setMaxVersions()
361    * @see #setMaxVersions(int)
362    * @return this
363    */
364   public Scan setTimeRange(long minStamp, long maxStamp) throws IOException {
365     tr = new TimeRange(minStamp, maxStamp);
366     return this;
367   }
368 
369   /**
370    * Get versions of columns with the specified timestamp. Note, default maximum
371    * versions to return is 1.  If your time range spans more than one version
372    * and you want all versions returned, up the number of versions beyond the
373    * defaut.
374    * @param timestamp version timestamp
375    * @see #setMaxVersions()
376    * @see #setMaxVersions(int)
377    * @return this
378    */
379   public Scan setTimeStamp(long timestamp)
380   throws IOException {
381     try {
382       tr = new TimeRange(timestamp, timestamp+1);
383     } catch(Exception e) {
384       // This should never happen, unless integer overflow or something extremely wrong...
385       LOG.error("TimeRange failed, likely caused by integer overflow. ", e);
386       throw e;
387     }
388     return this;
389   }
390 
391   @Override public Scan setColumnFamilyTimeRange(byte[] cf, long minStamp, long maxStamp) {
392     return (Scan) super.setColumnFamilyTimeRange(cf, minStamp, maxStamp);
393   }
394 
395 
396   /**
397    * Set the start row of the scan.
398    * <p>
399    * If the specified row does not exist, the Scanner will start from the next closest row after the
400    * specified row.
401    * @param startRow row to start scanner at or after
402    * @return this
403    * @throws IllegalArgumentException if startRow does not meet criteria for a row key (when length
404    *           exceeds {@link HConstants#MAX_ROW_LENGTH})
405    * @deprecated use {@link #withStartRow(byte[])} instead. This method may change the inclusive of
406    *             the stop row to keep compatible with the old behavior.
407    */
408   @Deprecated
409   public Scan setStartRow(byte[] startRow) {
410     withStartRow(startRow);
411     if (ClientUtil.areScanStartRowAndStopRowEqual(this.startRow, this.stopRow)) {
412       // for keeping the old behavior that a scan with the same start and stop row is a get scan.
413       this.includeStopRow = true;
414     }
415     return this;
416   }
417 
418   /**
419    * Set the start row of the scan.
420    * <p>
421    * If the specified row does not exist, the Scanner will start from the next closest row after the
422    * specified row.
423    * @param startRow row to start scanner at or after
424    * @return this
425    * @throws IllegalArgumentException if startRow does not meet criteria for a row key (when length
426    *           exceeds {@link HConstants#MAX_ROW_LENGTH})
427    */
428   public Scan withStartRow(byte[] startRow) {
429     return withStartRow(startRow, true);
430   }
431 
432   /**
433    * Set the start row of the scan.
434    * <p>
435    * If the specified row does not exist, or the {@code inclusive} is {@code false}, the Scanner
436    * will start from the next closest row after the specified row.
437    * @param startRow row to start scanner at or after
438    * @param inclusive whether we should include the start row when scan
439    * @return this
440    * @throws IllegalArgumentException if startRow does not meet criteria for a row key (when length
441    *           exceeds {@link HConstants#MAX_ROW_LENGTH})
442    */
443   public Scan withStartRow(byte[] startRow, boolean inclusive) {
444     if (Bytes.len(startRow) > HConstants.MAX_ROW_LENGTH) {
445       throw new IllegalArgumentException("startRow's length must be less than or equal to "
446           + HConstants.MAX_ROW_LENGTH + " to meet the criteria" + " for a row key.");
447     }
448     this.startRow = startRow;
449     this.includeStartRow = inclusive;
450     return this;
451   }
452 
453   /**
454    * Set the stop row of the scan.
455    * <p>
456    * The scan will include rows that are lexicographically less than the provided stopRow.
457    * <p>
458    * <b>Note:</b> When doing a filter for a rowKey <u>Prefix</u> use
459    * {@link #setRowPrefixFilter(byte[])}. The 'trailing 0' will not yield the desired result.
460    * </p>
461    * @param stopRow row to end at (exclusive)
462    * @return this
463    * @throws IllegalArgumentException if stopRow does not meet criteria for a row key (when length
464    *           exceeds {@link HConstants#MAX_ROW_LENGTH})
465    * @deprecated use {@link #withStartRow(byte[])} instead. This method may change the inclusive of
466    *             the stop row to keep compatible with the old behavior.
467    */
468   @Deprecated
469   public Scan setStopRow(byte[] stopRow) {
470     withStopRow(stopRow);
471     if (ClientUtil.areScanStartRowAndStopRowEqual(this.startRow, this.stopRow)) {
472       // for keeping the old behavior that a scan with the same start and stop row is a get scan.
473       this.includeStopRow = true;
474     }
475     return this;
476   }
477 
478   /**
479    * Set the stop row of the scan.
480    * <p>
481    * The scan will include rows that are lexicographically less than the provided stopRow.
482    * <p>
483    * <b>Note:</b> When doing a filter for a rowKey <u>Prefix</u> use
484    * {@link #setRowPrefixFilter(byte[])}. The 'trailing 0' will not yield the desired result.
485    * </p>
486    * @param stopRow row to end at (exclusive)
487    * @return this
488    * @throws IllegalArgumentException if stopRow does not meet criteria for a row key (when length
489    *           exceeds {@link HConstants#MAX_ROW_LENGTH})
490    */
491   public Scan withStopRow(byte[] stopRow) {
492     return withStopRow(stopRow, false);
493   }
494 
495   /**
496    * Set the stop row of the scan.
497    * <p>
498    * The scan will include rows that are lexicographically less than (or equal to if
499    * {@code inclusive} is {@code true}) the provided stopRow.
500    * @param stopRow row to end at
501    * @param inclusive whether we should include the stop row when scan
502    * @return this
503    * @throws IllegalArgumentException if stopRow does not meet criteria for a row key (when length
504    *           exceeds {@link HConstants#MAX_ROW_LENGTH})
505    */
506   public Scan withStopRow(byte[] stopRow, boolean inclusive) {
507     if (Bytes.len(stopRow) > HConstants.MAX_ROW_LENGTH) {
508       throw new IllegalArgumentException("stopRow's length must be less than or equal to "
509           + HConstants.MAX_ROW_LENGTH + " to meet the criteria" + " for a row key.");
510     }
511     this.stopRow = stopRow;
512     this.includeStopRow = inclusive;
513     return this;
514   }
515 
516   /**
517    * <p>Set a filter (using stopRow and startRow) so the result set only contains rows where the
518    * rowKey starts with the specified prefix.</p>
519    * <p>This is a utility method that converts the desired rowPrefix into the appropriate values
520    * for the startRow and stopRow to achieve the desired result.</p>
521    * <p>This can safely be used in combination with setFilter.</p>
522    * <p><b>NOTE: Doing a {@link #setStartRow(byte[])} and/or {@link #setStopRow(byte[])}
523    * after this method will yield undefined results.</b></p>
524    * @param rowPrefix the prefix all rows must start with. (Set <i>null</i> to remove the filter.)
525    * @return this
526    */
527   public Scan setRowPrefixFilter(byte[] rowPrefix) {
528     if (rowPrefix == null) {
529       setStartRow(HConstants.EMPTY_START_ROW);
530       setStopRow(HConstants.EMPTY_END_ROW);
531     } else {
532       this.setStartRow(rowPrefix);
533       this.setStopRow(calculateTheClosestNextRowKeyForPrefix(rowPrefix));
534     }
535     return this;
536   }
537 
538   /**
539    * <p>When scanning for a prefix the scan should stop immediately after the the last row that
540    * has the specified prefix. This method calculates the closest next rowKey immediately following
541    * the given rowKeyPrefix.</p>
542    * <p><b>IMPORTANT: This converts a rowKey<u>Prefix</u> into a rowKey</b>.</p>
543    * <p>If the prefix is an 'ASCII' string put into a byte[] then this is easy because you can
544    * simply increment the last byte of the array.
545    * But if your application uses real binary rowids you may run into the scenario that your
546    * prefix is something like:</p>
547    * &nbsp;&nbsp;&nbsp;<b>{ 0x12, 0x23, 0xFF, 0xFF }</b><br/>
548    * Then this stopRow needs to be fed into the actual scan<br/>
549    * &nbsp;&nbsp;&nbsp;<b>{ 0x12, 0x24 }</b> (Notice that it is shorter now)<br/>
550    * This method calculates the correct stop row value for this usecase.
551    *
552    * @param rowKeyPrefix the rowKey<u>Prefix</u>.
553    * @return the closest next rowKey immediately following the given rowKeyPrefix.
554    */
555   private byte[] calculateTheClosestNextRowKeyForPrefix(byte[] rowKeyPrefix) {
556     // Essentially we are treating it like an 'unsigned very very long' and doing +1 manually.
557     // Search for the place where the trailing 0xFFs start
558     int offset = rowKeyPrefix.length;
559     while (offset > 0) {
560       if (rowKeyPrefix[offset - 1] != (byte) 0xFF) {
561         break;
562       }
563       offset--;
564     }
565 
566     if (offset == 0) {
567       // We got an 0xFFFF... (only FFs) stopRow value which is
568       // the last possible prefix before the end of the table.
569       // So set it to stop at the 'end of the table'
570       return HConstants.EMPTY_END_ROW;
571     }
572 
573     // Copy the right length of the original
574     byte[] newStopRow = Arrays.copyOfRange(rowKeyPrefix, 0, offset);
575     // And increment the last one
576     newStopRow[newStopRow.length - 1]++;
577     return newStopRow;
578   }
579 
580   /**
581    * Get all available versions.
582    * @return this
583    */
584   public Scan setMaxVersions() {
585     this.maxVersions = Integer.MAX_VALUE;
586     return this;
587   }
588 
589   /**
590    * Get up to the specified number of versions of each column.
591    * @param maxVersions maximum versions for each column
592    * @return this
593    */
594   public Scan setMaxVersions(int maxVersions) {
595     this.maxVersions = maxVersions;
596     return this;
597   }
598 
599   /**
600    * Set the maximum number of cells to return for each call to next(). Callers should be aware
601    * that this is not equivalent to calling {@link #setAllowPartialResults(boolean)}.
602    * If you don't allow partial results, the number of cells in each Result must equal to your
603    * batch setting unless it is the last Result for current row. So this method is helpful in paging
604    * queries. If you just want to prevent OOM at client, use setAllowPartialResults(true) is better.
605    * @param batch the maximum number of values
606    * @see Result#mayHaveMoreCellsInRow()
607    */
608   public Scan setBatch(int batch) {
609     if (this.hasFilter() && this.filter.hasFilterRow()) {
610       throw new IncompatibleFilterException(
611         "Cannot set batch on a scan using a filter" +
612         " that returns true for filter.hasFilterRow");
613     }
614     this.batch = batch;
615     return this;
616   }
617 
618   /**
619    * Set the maximum number of values to return per row per Column Family
620    * @param limit the maximum number of values returned / row / CF
621    */
622   public Scan setMaxResultsPerColumnFamily(int limit) {
623     this.storeLimit = limit;
624     return this;
625   }
626 
627   /**
628    * Set offset for the row per Column Family.
629    * @param offset is the number of kvs that will be skipped.
630    */
631   public Scan setRowOffsetPerColumnFamily(int offset) {
632     this.storeOffset = offset;
633     return this;
634   }
635 
636   /**
637    * Set the number of rows for caching that will be passed to scanners.
638    * If not set, the Configuration setting {@link HConstants#HBASE_CLIENT_SCANNER_CACHING} will
639    * apply.
640    * Higher caching values will enable faster scanners but will use more memory.
641    * @param caching the number of rows for caching
642    */
643   public Scan setCaching(int caching) {
644     this.caching = caching;
645     return this;
646   }
647 
648   /**
649    * @return the maximum result size in bytes. See {@link #setMaxResultSize(long)}
650    */
651   public long getMaxResultSize() {
652     return maxResultSize;
653   }
654 
655   /**
656    * Set the maximum result size. The default is -1; this means that no specific
657    * maximum result size will be set for this scan, and the global configured
658    * value will be used instead. (Defaults to unlimited).
659    *
660    * @param maxResultSize The maximum result size in bytes.
661    */
662   public Scan setMaxResultSize(long maxResultSize) {
663     this.maxResultSize = maxResultSize;
664     return this;
665   }
666 
667   @Override
668   public Scan setFilter(Filter filter) {
669     super.setFilter(filter);
670     return this;
671   }
672 
673   /**
674    * Setting the familyMap
675    * @param familyMap map of family to qualifier
676    * @return this
677    */
678   public Scan setFamilyMap(Map<byte [], NavigableSet<byte []>> familyMap) {
679     this.familyMap = familyMap;
680     return this;
681   }
682 
683   /**
684    * Getting the familyMap
685    * @return familyMap
686    */
687   public Map<byte [], NavigableSet<byte []>> getFamilyMap() {
688     return this.familyMap;
689   }
690 
691   /**
692    * @return the number of families in familyMap
693    */
694   public int numFamilies() {
695     if(hasFamilies()) {
696       return this.familyMap.size();
697     }
698     return 0;
699   }
700 
701   /**
702    * @return true if familyMap is non empty, false otherwise
703    */
704   public boolean hasFamilies() {
705     return !this.familyMap.isEmpty();
706   }
707 
708   /**
709    * @return the keys of the familyMap
710    */
711   public byte[][] getFamilies() {
712     if(hasFamilies()) {
713       return this.familyMap.keySet().toArray(new byte[0][0]);
714     }
715     return null;
716   }
717 
718   /**
719    * @return the startrow
720    */
721   public byte [] getStartRow() {
722     return this.startRow;
723   }
724 
725   /**
726    * @return if we should include start row when scan
727    */
728   public boolean includeStartRow() {
729     return includeStartRow;
730   }
731 
732   /**
733    * @return the stoprow
734    */
735   public byte[] getStopRow() {
736     return this.stopRow;
737   }
738 
739   /**
740    * @return if we should include stop row when scan
741    */
742   public boolean includeStopRow() {
743     return includeStopRow;
744   }
745 
746   /**
747    * @return the max number of versions to fetch
748    */
749   public int getMaxVersions() {
750     return this.maxVersions;
751   }
752 
753   /**
754    * @return maximum number of values to return for a single call to next()
755    */
756   public int getBatch() {
757     return this.batch;
758   }
759 
760   /**
761    * @return maximum number of values to return per row per CF
762    */
763   public int getMaxResultsPerColumnFamily() {
764     return this.storeLimit;
765   }
766 
767   /**
768    * Method for retrieving the scan's offset per row per column
769    * family (#kvs to be skipped)
770    * @return row offset
771    */
772   public int getRowOffsetPerColumnFamily() {
773     return this.storeOffset;
774   }
775 
776   /**
777    * @return caching the number of rows fetched when calling next on a scanner
778    */
779   public int getCaching() {
780     return this.caching;
781   }
782 
783   /**
784    * @return TimeRange
785    */
786   public TimeRange getTimeRange() {
787     return this.tr;
788   }
789 
790   /**
791    * @return RowFilter
792    */
793   @Override
794   public Filter getFilter() {
795     return filter;
796   }
797 
798   /**
799    * @return true is a filter has been specified, false if not
800    */
801   public boolean hasFilter() {
802     return filter != null;
803   }
804 
805   /**
806    * Set whether blocks should be cached for this Scan.
807    * <p>
808    * This is true by default.  When true, default settings of the table and
809    * family are used (this will never override caching blocks if the block
810    * cache is disabled for that family or entirely).
811    *
812    * @param cacheBlocks if false, default settings are overridden and blocks
813    * will not be cached
814    */
815   public Scan setCacheBlocks(boolean cacheBlocks) {
816     this.cacheBlocks = cacheBlocks;
817     return this;
818   }
819 
820   /**
821    * Get whether blocks should be cached for this Scan.
822    * @return true if default caching should be used, false if blocks should not
823    * be cached
824    */
825   public boolean getCacheBlocks() {
826     return cacheBlocks;
827   }
828 
829   /**
830    * Set whether this scan is a reversed one
831    * <p>
832    * This is false by default which means forward(normal) scan.
833    *
834    * @param reversed if true, scan will be backward order
835    * @return this
836    */
837   public Scan setReversed(boolean reversed) {
838     this.reversed = reversed;
839     return this;
840   }
841 
842   /**
843    * Get whether this scan is a reversed one.
844    * @return true if backward scan, false if forward(default) scan
845    */
846   public boolean isReversed() {
847     return reversed;
848   }
849 
850   /**
851    * Setting whether the caller wants to see the partial results when server returns
852    * less-than-expected cells. It is helpful while scanning a huge row to prevent OOM at client.
853    * By default this value is false and the complete results will be assembled client side
854    * before being delivered to the caller.
855    * @param allowPartialResults
856    * @return this
857    * @see Result#mayHaveMoreCellsInRow()
858    * @see #setBatch(int)
859    */
860   public Scan setAllowPartialResults(final boolean allowPartialResults) {
861     this.allowPartialResults = allowPartialResults;
862     return this;
863   }
864 
865   /**
866    * @return true when the constructor of this scan understands that the results they will see may
867    *         only represent a partial portion of a row. The entire row would be retrieved by
868    *         subsequent calls to {@link ResultScanner#next()}
869    */
870   public boolean getAllowPartialResults() {
871     return allowPartialResults;
872   }
873 
874   @Override
875   public Scan setLoadColumnFamiliesOnDemand(boolean value) {
876     return (Scan) super.setLoadColumnFamiliesOnDemand(value);
877   }
878 
879   /**
880    * Compile the table and column family (i.e. schema) information
881    * into a String. Useful for parsing and aggregation by debugging,
882    * logging, and administration tools.
883    * @return Map
884    */
885   @Override
886   public Map<String, Object> getFingerprint() {
887     Map<String, Object> map = new HashMap<String, Object>();
888     List<String> families = new ArrayList<String>();
889     if(this.familyMap.size() == 0) {
890       map.put("families", "ALL");
891       return map;
892     } else {
893       map.put("families", families);
894     }
895     for (Map.Entry<byte [], NavigableSet<byte[]>> entry :
896         this.familyMap.entrySet()) {
897       families.add(Bytes.toStringBinary(entry.getKey()));
898     }
899     return map;
900   }
901 
902   /**
903    * Compile the details beyond the scope of getFingerprint (row, columns,
904    * timestamps, etc.) into a Map along with the fingerprinted information.
905    * Useful for debugging, logging, and administration tools.
906    * @param maxCols a limit on the number of columns output prior to truncation
907    * @return Map
908    */
909   @Override
910   public Map<String, Object> toMap(int maxCols) {
911     // start with the fingerpring map and build on top of it
912     Map<String, Object> map = getFingerprint();
913     // map from families to column list replaces fingerprint's list of families
914     Map<String, List<String>> familyColumns =
915       new HashMap<String, List<String>>();
916     map.put("families", familyColumns);
917     // add scalar information first
918     map.put("startRow", Bytes.toStringBinary(this.startRow));
919     map.put("stopRow", Bytes.toStringBinary(this.stopRow));
920     map.put("maxVersions", this.maxVersions);
921     map.put("batch", this.batch);
922     map.put("caching", this.caching);
923     map.put("maxResultSize", this.maxResultSize);
924     map.put("cacheBlocks", this.cacheBlocks);
925     map.put("loadColumnFamiliesOnDemand", this.loadColumnFamiliesOnDemand);
926     List<Long> timeRange = new ArrayList<Long>();
927     timeRange.add(this.tr.getMin());
928     timeRange.add(this.tr.getMax());
929     map.put("timeRange", timeRange);
930     int colCount = 0;
931     // iterate through affected families and list out up to maxCols columns
932     for (Map.Entry<byte [], NavigableSet<byte[]>> entry :
933       this.familyMap.entrySet()) {
934       List<String> columns = new ArrayList<String>();
935       familyColumns.put(Bytes.toStringBinary(entry.getKey()), columns);
936       if(entry.getValue() == null) {
937         colCount++;
938         --maxCols;
939         columns.add("ALL");
940       } else {
941         colCount += entry.getValue().size();
942         if (maxCols <= 0) {
943           continue;
944         }
945         for (byte [] column : entry.getValue()) {
946           if (--maxCols <= 0) {
947             continue;
948           }
949           columns.add(Bytes.toStringBinary(column));
950         }
951       }
952     }
953     map.put("totalColumns", colCount);
954     if (this.filter != null) {
955       map.put("filter", this.filter.toString());
956     }
957     // add the id if set
958     if (getId() != null) {
959       map.put("id", getId());
960     }
961     return map;
962   }
963 
964   /**
965    * Enable/disable "raw" mode for this scan.
966    * If "raw" is enabled the scan will return all
967    * delete marker and deleted rows that have not
968    * been collected, yet.
969    * This is mostly useful for Scan on column families
970    * that have KEEP_DELETED_ROWS enabled.
971    * It is an error to specify any column when "raw" is set.
972    * @param raw True/False to enable/disable "raw" mode.
973    */
974   public Scan setRaw(boolean raw) {
975     setAttribute(RAW_ATTR, Bytes.toBytes(raw));
976     return this;
977   }
978 
979   /**
980    * @return True if this Scan is in "raw" mode.
981    */
982   public boolean isRaw() {
983     byte[] attr = getAttribute(RAW_ATTR);
984     return attr == null ? false : Bytes.toBoolean(attr);
985   }
986 
987 
988 
989   /**
990    * Set whether this scan is a small scan
991    * <p>
992    * Small scan should use pread and big scan can use seek + read
993    *
994    * seek + read is fast but can cause two problem (1) resource contention (2)
995    * cause too much network io
996    *
997    * [89-fb] Using pread for non-compaction read request
998    * https://issues.apache.org/jira/browse/HBASE-7266
999    *
1000    * On the other hand, if setting it true, we would do
1001    * openScanner,next,closeScanner in one RPC call. It means the better
1002    * performance for small scan. [HBASE-9488].
1003    *
1004    * Generally, if the scan range is within one data block(64KB), it could be
1005    * considered as a small scan.
1006    *
1007    * @param small
1008    */
1009   public Scan setSmall(boolean small) {
1010     this.small = small;
1011     return this;
1012   }
1013 
1014   /**
1015    * Get whether this scan is a small scan
1016    * @return true if small scan
1017    */
1018   public boolean isSmall() {
1019     return small;
1020   }
1021 
1022   @Override
1023   public Scan setAttribute(String name, byte[] value) {
1024     return (Scan) super.setAttribute(name, value);
1025   }
1026 
1027   @Override
1028   public Scan setId(String id) {
1029     return (Scan) super.setId(id);
1030   }
1031 
1032   @Override
1033   public Scan setAuthorizations(Authorizations authorizations) {
1034     return (Scan) super.setAuthorizations(authorizations);
1035   }
1036 
1037   @Override
1038   public Scan setACL(Map<String, Permission> perms) {
1039     return (Scan) super.setACL(perms);
1040   }
1041 
1042   @Override
1043   public Scan setACL(String user, Permission perms) {
1044     return (Scan) super.setACL(user, perms);
1045   }
1046 
1047   @Override
1048   public Scan setConsistency(Consistency consistency) {
1049     return (Scan) super.setConsistency(consistency);
1050   }
1051 
1052   @Override
1053   public Scan setReplicaId(int Id) {
1054     return (Scan) super.setReplicaId(Id);
1055   }
1056 
1057   @Override
1058   public Scan setIsolationLevel(IsolationLevel level) {
1059     return (Scan) super.setIsolationLevel(level);
1060   }
1061 
1062   @Override
1063   public Scan setPriority(int priority) {
1064     return (Scan) super.setPriority(priority);
1065   }
1066 
1067   /**
1068    * Utility that creates a Scan that will do a  small scan in reverse from passed row
1069    * looking for next closest row.
1070    * @param row
1071    * @param family
1072    * @return An instance of Scan primed with passed <code>row</code> and <code>family</code> to
1073    * scan in reverse for one row only.
1074    */
1075   static Scan createGetClosestRowOrBeforeReverseScan(byte[] row) {
1076     // Below does not work if you add in family; need to add the family qualifier that is highest
1077     // possible family qualifier.  Do we have such a notion?  Would have to be magic.
1078     Scan scan = new Scan(row);
1079     scan.setSmall(true);
1080     scan.setReversed(true);
1081     scan.setCaching(1);
1082     return scan;
1083   }
1084 
1085   /**
1086    * Enable collection of {@link ScanMetrics}. For advanced users.
1087    * @param enabled Set to true to enable accumulating scan metrics
1088    */
1089   public Scan setScanMetricsEnabled(final boolean enabled) {
1090     setAttribute(Scan.SCAN_ATTRIBUTES_METRICS_ENABLE, Bytes.toBytes(Boolean.valueOf(enabled)));
1091     return this;
1092   }
1093 
1094   /**
1095    * @return True if collection of scan metrics is enabled. For advanced users.
1096    */
1097   public boolean isScanMetricsEnabled() {
1098     byte[] attr = getAttribute(Scan.SCAN_ATTRIBUTES_METRICS_ENABLE);
1099     return attr == null ? false : Bytes.toBoolean(attr);
1100   }
1101 
1102   /**
1103    * @return Metrics on this Scan, if metrics were enabled.
1104    * @see #setScanMetricsEnabled(boolean)
1105    * @deprecated Use {@link ResultScanner#getScanMetrics()} instead. And notice that, please do not
1106    *             use this method and {@link ResultScanner#getScanMetrics()} together, the metrics
1107    *             will be messed up.
1108    */
1109   @Deprecated
1110   public ScanMetrics getScanMetrics() {
1111     byte[] bytes = getAttribute(Scan.SCAN_ATTRIBUTES_METRICS_DATA);
1112     if (bytes == null) return null;
1113     return ProtobufUtil.toScanMetrics(bytes);
1114   }
1115 
1116   /**
1117    * @return the limit of rows for this scan
1118    */
1119   public int getLimit() {
1120     return limit;
1121   }
1122 
1123   /**
1124    * Set the limit of rows for this scan. We will terminate the scan if the number of returned rows
1125    * reaches this value.
1126    * <p>
1127    * This condition will be tested at last, after all other conditions such as stopRow, filter, etc.
1128    * @param limit the limit of rows for this scan
1129    * @return this
1130    */
1131   public Scan setLimit(int limit) {
1132     this.limit = limit;
1133     return this;
1134   }
1135 
1136   /**
1137    * Call this when you only want to get one row. It will set {@code limit} to {@code 1}, and also
1138    * set {@code readType} to {@link ReadType#PREAD}.
1139    * @return this
1140    */
1141   public Scan setOneRowLimit() {
1142     return setLimit(1).setReadType(ReadType.PREAD);
1143   }
1144 
1145   @InterfaceAudience.Public
1146   @InterfaceStability.Unstable
1147   public enum ReadType {
1148     DEFAULT, STREAM, PREAD
1149   }
1150 
1151   /**
1152    * @return the read type for this scan
1153    */
1154   public ReadType getReadType() {
1155     return readType;
1156   }
1157 
1158   /**
1159    * Set the read type for this scan.
1160    * <p>
1161    * Notice that we may choose to use pread even if you specific {@link ReadType#STREAM} here. For
1162    * example, we will always use pread if this is a get scan.
1163    * @return this
1164    */
1165   public Scan setReadType(ReadType readType) {
1166     this.readType = readType;
1167     return this;
1168   }
1169 
1170   /**
1171    * Get the mvcc read point used to open a scanner.
1172    */
1173   long getMvccReadPoint() {
1174     return mvccReadPoint;
1175   }
1176 
1177   /**
1178    * Set the mvcc read point used to open a scanner.
1179    */
1180   Scan setMvccReadPoint(long mvccReadPoint) {
1181     this.mvccReadPoint = mvccReadPoint;
1182     return this;
1183   }
1184 
1185   /**
1186    * Set the mvcc read point to -1 which means do not use it.
1187    */
1188   Scan resetMvccReadPoint() {
1189     return setMvccReadPoint(-1L);
1190   }
1191 
1192   /**
1193    * When the server is slow or we scan a table with many deleted data or we use a sparse filter,
1194    * the server will response heartbeat to prevent timeout. However the scanner will return a Result
1195    * only when client can do it. So if there are many heartbeats, the blocking time on
1196    * ResultScanner#next() may be very long, which is not friendly to online services.
1197    *
1198    * Set this to true then you can get a special Result whose #isCursor() returns true and is not
1199    * contains any real data. It only tells you where the server has scanned. You can call next
1200    * to continue scanning or open a new scanner with this row key as start row whenever you want.
1201    *
1202    * Users can get a cursor when and only when there is a response from the server but we can not
1203    * return a Result to users, for example, this response is a heartbeat or there are partial cells
1204    * but users do not allow partial result.
1205    *
1206    * Now the cursor is in row level which means the special Result will only contains a row key.
1207    * {@link Result#isCursor()}
1208    * {@link Result#getCursor()}
1209    * {@link Cursor}
1210    */
1211   public Scan setNeedCursorResult(boolean needCursorResult) {
1212     this.needCursorResult = needCursorResult;
1213     return this;
1214   }
1215 
1216   public boolean isNeedCursorResult() {
1217     return needCursorResult;
1218   }
1219 
1220   /**
1221    * Create a new Scan with a cursor. It only set the position information like start row key.
1222    * The others (like cfs, stop row, limit) should still be filled in by the user.
1223    * {@link Result#isCursor()}
1224    * {@link Result#getCursor()}
1225    * {@link Cursor}
1226    */
1227   public static Scan createScanFromCursor(Cursor cursor) {
1228     return new Scan().withStartRow(cursor.getRow());
1229   }
1230 }