1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.client;
20
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.fs.FileSystem;
29 import org.apache.hadoop.fs.Path;
30 import org.apache.hadoop.hbase.Cell;
31 import org.apache.hadoop.hbase.CellUtil;
32 import org.apache.hadoop.hbase.HRegionInfo;
33 import org.apache.hadoop.hbase.HTableDescriptor;
34 import org.apache.hadoop.hbase.classification.InterfaceAudience;
35 import org.apache.hadoop.hbase.client.metrics.ScanMetrics;
36 import org.apache.hadoop.hbase.regionserver.HRegion;
37 import org.apache.hadoop.hbase.regionserver.RegionScanner;
38
39
40
41
42
43 @InterfaceAudience.Private
44 public class ClientSideRegionScanner extends AbstractClientScanner {
45
46 private static final Log LOG = LogFactory.getLog(ClientSideRegionScanner.class);
47
48 private HRegion region;
49 RegionScanner scanner;
50 List<Cell> values;
51
52 public ClientSideRegionScanner(Configuration conf, FileSystem fs,
53 Path rootDir, HTableDescriptor htd, HRegionInfo hri, Scan scan, ScanMetrics scanMetrics)
54 throws IOException {
55
56 scan.setIsolationLevel(IsolationLevel.READ_UNCOMMITTED);
57
58 htd.setReadOnly(true);
59
60
61 this.region = HRegion.openHRegion(conf, fs, rootDir, hri, htd, null, null, null);
62
63
64 this.scanner = region.getScanner(scan);
65 values = new ArrayList<Cell>();
66
67 if (scanMetrics == null) {
68 initScanMetrics(scan);
69 } else {
70 this.scanMetrics = scanMetrics;
71 }
72 region.startRegionOperation();
73 }
74
75 @Override
76 public Result next() throws IOException {
77 values.clear();
78 scanner.nextRaw(values);
79 if (values.isEmpty()) {
80
81 return null;
82 }
83
84 Result result = Result.create(values);
85 if (this.scanMetrics != null) {
86 long resultSize = 0;
87 for (Cell cell : values) {
88 resultSize += CellUtil.estimatedSerializedSizeOf(cell);
89 }
90 this.scanMetrics.countOfBytesInResults.addAndGet(resultSize);
91 this.scanMetrics.countOfRowsScanned.incrementAndGet();
92 }
93
94 return result;
95 }
96
97 @Override
98 public void close() {
99 if (this.scanner != null) {
100 try {
101 this.scanner.close();
102 this.scanner = null;
103 } catch (IOException ex) {
104 LOG.warn("Exception while closing scanner", ex);
105 }
106 }
107 if (this.region != null) {
108 try {
109 this.region.closeRegionOperation();
110 this.region.close(true);
111 this.region = null;
112 } catch (IOException ex) {
113 LOG.warn("Exception while closing region", ex);
114 }
115 }
116 }
117
118 @Override
119 public boolean renewLease() {
120 throw new UnsupportedOperationException();
121 }
122 }