1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.mapreduce;
21
22 import java.io.IOException;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.hbase.DoNotRetryIOException;
27 import org.apache.hadoop.hbase.client.HTable;
28 import org.apache.hadoop.hbase.client.Result;
29 import org.apache.hadoop.hbase.client.ResultScanner;
30 import org.apache.hadoop.hbase.client.Scan;
31 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
32 import org.apache.hadoop.util.StringUtils;
33
34
35
36
37
38 public class TableRecordReaderImpl {
39
40
41 static final Log LOG = LogFactory.getLog(TableRecordReader.class);
42
43 private ResultScanner scanner = null;
44 private Scan scan = null;
45 private HTable htable = null;
46 private byte[] lastSuccessfulRow = null;
47 private ImmutableBytesWritable key = null;
48 private Result value = null;
49
50
51
52
53
54
55
56 public void restart(byte[] firstRow) throws IOException {
57 Scan newScan = new Scan(scan);
58 newScan.setStartRow(firstRow);
59 this.scanner = this.htable.getScanner(newScan);
60 }
61
62
63
64
65
66
67 public void init() throws IOException {
68 restart(scan.getStartRow());
69 }
70
71
72
73
74
75
76 public void setHTable(HTable htable) {
77 this.htable = htable;
78 }
79
80
81
82
83
84
85 public void setScan(Scan scan) {
86 this.scan = scan;
87 }
88
89
90
91
92
93
94 public void close() {
95 this.scanner.close();
96 }
97
98
99
100
101
102
103
104
105 public ImmutableBytesWritable getCurrentKey() throws IOException,
106 InterruptedException {
107 return key;
108 }
109
110
111
112
113
114
115
116
117 public Result getCurrentValue() throws IOException, InterruptedException {
118 return value;
119 }
120
121
122
123
124
125
126
127
128
129 public boolean nextKeyValue() throws IOException, InterruptedException {
130 if (key == null) key = new ImmutableBytesWritable();
131 if (value == null) value = new Result();
132 try {
133 value = this.scanner.next();
134 } catch (DoNotRetryIOException e) {
135 throw e;
136 } catch (IOException e) {
137 LOG.debug("recovered from " + StringUtils.stringifyException(e));
138 if (lastSuccessfulRow == null) {
139 LOG.warn("We are restarting the first next() invocation," +
140 " if your mapper's restarted a few other times like this" +
141 " then you should consider killing this job and investigate" +
142 " why it's taking so long.");
143 }
144 if (lastSuccessfulRow == null) {
145 restart(scan.getStartRow());
146 } else {
147 restart(lastSuccessfulRow);
148 scanner.next();
149 }
150 value = scanner.next();
151 }
152 if (value != null && value.size() > 0) {
153 key.set(value.getRow());
154 lastSuccessfulRow = key.get();
155 return true;
156 }
157 return false;
158 }
159
160
161
162
163
164
165 public float getProgress() {
166
167 return 0;
168 }
169
170 }