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
60 this.region = HRegion.openHRegion(conf, fs, rootDir, hri, htd, null, null, null);
61
62
63 this.scanner = region.getScanner(scan);
64 values = new ArrayList<Cell>();
65
66 if (scanMetrics == null) {
67 initScanMetrics(scan);
68 } else {
69 this.scanMetrics = scanMetrics;
70 }
71 region.startRegionOperation();
72 }
73
74 @Override
75 public Result next() throws IOException {
76 values.clear();
77 scanner.nextRaw(values);
78 if (values.isEmpty()) {
79
80 return null;
81 }
82
83 Result result = Result.create(values);
84 if (this.scanMetrics != null) {
85 long resultSize = 0;
86 for (Cell cell : values) {
87 resultSize += CellUtil.estimatedSerializedSizeOf(cell);
88 }
89 this.scanMetrics.countOfBytesInResults.addAndGet(resultSize);
90 }
91
92 return result;
93 }
94
95 @Override
96 public void close() {
97 if (this.scanner != null) {
98 try {
99 this.scanner.close();
100 this.scanner = null;
101 } catch (IOException ex) {
102 LOG.warn("Exception while closing scanner", ex);
103 }
104 }
105 if (this.region != null) {
106 try {
107 this.region.closeRegionOperation();
108 this.region.close(true);
109 this.region = null;
110 } catch (IOException ex) {
111 LOG.warn("Exception while closing region", ex);
112 }
113 }
114 }
115
116 @Override
117 public boolean renewLease() {
118 throw new UnsupportedOperationException();
119 }
120 }