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
57 scan.setIsolationLevel(IsolationLevel.READ_UNCOMMITTED);
58
59 htd.setReadOnly(true);
60
61
62 this.region = HRegion.openHRegion(conf, fs, rootDir, hri, htd, null, null, null);
63
64
65 this.scanner = region.getScanner(scan);
66 values = new ArrayList<Cell>();
67
68 if (scanMetrics == null) {
69 initScanMetrics(scan);
70 } else {
71 this.scanMetrics = scanMetrics;
72 }
73 region.startRegionOperation();
74 }
75
76 @Override
77 public Result next() throws IOException {
78 values.clear();
79 scanner.nextRaw(values);
80 if (values.isEmpty()) {
81
82 return null;
83 }
84
85 Result result = Result.create(values);
86 if (this.scanMetrics != null) {
87 long resultSize = 0;
88 for (Cell cell : values) {
89 resultSize += CellUtil.estimatedSerializedSizeOf(cell);
90 }
91 this.scanMetrics.countOfBytesInResults.addAndGet(resultSize);
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 }