1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.mapreduce;
20
21 import java.io.IOException;
22 import java.io.InterruptedIOException;
23 import java.net.InetAddress;
24 import java.net.InetSocketAddress;
25 import java.text.MessageFormat;
26 import java.net.UnknownHostException;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30
31 import javax.naming.NamingException;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.hadoop.hbase.classification.InterfaceAudience;
36 import org.apache.hadoop.hbase.classification.InterfaceStability;
37 import org.apache.hadoop.hbase.HConstants;
38 import org.apache.hadoop.hbase.HRegionLocation;
39 import org.apache.hadoop.hbase.client.HTable;
40 import org.apache.hadoop.hbase.client.Result;
41 import org.apache.hadoop.hbase.client.Scan;
42 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
43 import org.apache.hadoop.hbase.util.Addressing;
44 import org.apache.hadoop.hbase.util.Bytes;
45 import org.apache.hadoop.hbase.util.Pair;
46 import org.apache.hadoop.hbase.util.RegionSizeCalculator;
47 import org.apache.hadoop.hbase.util.Strings;
48 import org.apache.hadoop.mapreduce.InputFormat;
49 import org.apache.hadoop.mapreduce.InputSplit;
50 import org.apache.hadoop.mapreduce.JobContext;
51 import org.apache.hadoop.mapreduce.RecordReader;
52 import org.apache.hadoop.mapreduce.TaskAttemptContext;
53 import org.apache.hadoop.net.DNS;
54 import org.apache.hadoop.util.StringUtils;
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 @InterfaceAudience.Public
85 @InterfaceStability.Stable
86 public abstract class TableInputFormatBase
87 extends InputFormat<ImmutableBytesWritable, Result> {
88
89 final Log LOG = LogFactory.getLog(TableInputFormatBase.class);
90
91
92 private Scan scan = null;
93
94 private HTable table = null;
95
96 private TableRecordReader tableRecordReader = null;
97
98
99
100 private HashMap<InetAddress, String> reverseDNSCacheMap =
101 new HashMap<InetAddress, String>();
102
103
104 private String nameServer = null;
105
106
107
108
109
110
111
112
113
114
115
116
117
118 @Override
119 public RecordReader<ImmutableBytesWritable, Result> createRecordReader(
120 InputSplit split, TaskAttemptContext context)
121 throws IOException {
122 if (table == null) {
123 throw new IOException("Cannot create a record reader because of a" +
124 " previous error. Please look at the previous logs lines from" +
125 " the task's full log for more details.");
126 }
127 TableSplit tSplit = (TableSplit) split;
128 LOG.info("Input split length: " + StringUtils.humanReadableInt(tSplit.getLength()) + " bytes.");
129 TableRecordReader trr = this.tableRecordReader;
130
131 if (trr == null) {
132 trr = new TableRecordReader();
133 }
134 Scan sc = new Scan(this.scan);
135 sc.setStartRow(tSplit.getStartRow());
136 sc.setStopRow(tSplit.getEndRow());
137 trr.setScan(sc);
138 trr.setHTable(table);
139 return trr;
140 }
141
142
143
144
145
146
147
148
149
150
151
152 @Override
153 public List<InputSplit> getSplits(JobContext context) throws IOException {
154 if (table == null) {
155 throw new IOException("No table was provided.");
156 }
157
158 this.nameServer =
159 context.getConfiguration().get("hbase.nameserver.address", null);
160
161 RegionSizeCalculator sizeCalculator = new RegionSizeCalculator(table);
162
163 Pair<byte[][], byte[][]> keys = table.getStartEndKeys();
164 if (keys == null || keys.getFirst() == null ||
165 keys.getFirst().length == 0) {
166 HRegionLocation regLoc = table.getRegionLocation(HConstants.EMPTY_BYTE_ARRAY, false);
167 if (null == regLoc) {
168 throw new IOException("Expecting at least one region.");
169 }
170 List<InputSplit> splits = new ArrayList<InputSplit>(1);
171 long regionSize = sizeCalculator.getRegionSize(regLoc.getRegionInfo().getRegionName());
172 TableSplit split = new TableSplit(table.getName(),
173 HConstants.EMPTY_BYTE_ARRAY, HConstants.EMPTY_BYTE_ARRAY, regLoc
174 .getHostnamePort().split(Addressing.HOSTNAME_PORT_SEPARATOR)[0], regionSize);
175 splits.add(split);
176 return splits;
177 }
178 List<InputSplit> splits = new ArrayList<InputSplit>(keys.getFirst().length);
179 for (int i = 0; i < keys.getFirst().length; i++) {
180 if ( !includeRegionInSplit(keys.getFirst()[i], keys.getSecond()[i])) {
181 continue;
182 }
183 HRegionLocation location = table.getRegionLocation(keys.getFirst()[i], false);
184
185 InetSocketAddress isa = new InetSocketAddress(location.getHostname(), location.getPort());
186 if (isa.isUnresolved()) {
187 LOG.warn("Failed resolve " + isa);
188 }
189 InetAddress regionAddress = isa.getAddress();
190 String regionLocation;
191 try {
192 regionLocation = reverseDNS(regionAddress);
193 } catch (NamingException e) {
194 LOG.warn("Cannot resolve the host name for " + regionAddress + " because of " + e);
195 regionLocation = location.getHostname();
196 }
197
198 byte[] startRow = scan.getStartRow();
199 byte[] stopRow = scan.getStopRow();
200
201 if ((startRow.length == 0 || keys.getSecond()[i].length == 0 ||
202 Bytes.compareTo(startRow, keys.getSecond()[i]) < 0) &&
203 (stopRow.length == 0 ||
204 Bytes.compareTo(stopRow, keys.getFirst()[i]) > 0)) {
205 byte[] splitStart = startRow.length == 0 ||
206 Bytes.compareTo(keys.getFirst()[i], startRow) >= 0 ?
207 keys.getFirst()[i] : startRow;
208 byte[] splitStop = (stopRow.length == 0 ||
209 Bytes.compareTo(keys.getSecond()[i], stopRow) <= 0) &&
210 keys.getSecond()[i].length > 0 ?
211 keys.getSecond()[i] : stopRow;
212
213 byte[] regionName = location.getRegionInfo().getRegionName();
214 long regionSize = sizeCalculator.getRegionSize(regionName);
215 TableSplit split = new TableSplit(table.getName(),
216 splitStart, splitStop, regionLocation, regionSize);
217 splits.add(split);
218 if (LOG.isDebugEnabled()) {
219 LOG.debug("getSplits: split -> " + i + " -> " + split);
220 }
221 }
222 }
223 return splits;
224 }
225
226 public String reverseDNS(InetAddress ipAddress) throws NamingException, UnknownHostException {
227 String hostName = this.reverseDNSCacheMap.get(ipAddress);
228 if (hostName == null) {
229 String ipAddressString = null;
230 try {
231 ipAddressString = DNS.reverseDns(ipAddress, null);
232 } catch (Exception e) {
233
234
235
236 ipAddressString = InetAddress.getByName(ipAddress.getHostAddress()).getHostName();
237 }
238 if (ipAddressString == null) throw new UnknownHostException("No host found for " + ipAddress);
239 hostName = Strings.domainNamePointerToHostName(ipAddressString);
240 this.reverseDNSCacheMap.put(ipAddress, hostName);
241 }
242 return hostName;
243 }
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267 protected boolean includeRegionInSplit(final byte[] startKey, final byte [] endKey) {
268 return true;
269 }
270
271
272
273
274 protected HTable getHTable() {
275 return this.table;
276 }
277
278
279
280
281
282
283 protected void setHTable(HTable table) {
284 this.table = table;
285 }
286
287
288
289
290
291
292 public Scan getScan() {
293 if (this.scan == null) this.scan = new Scan();
294 return scan;
295 }
296
297
298
299
300
301
302 public void setScan(Scan scan) {
303 this.scan = scan;
304 }
305
306
307
308
309
310
311
312 protected void setTableRecordReader(TableRecordReader tableRecordReader) {
313 this.tableRecordReader = tableRecordReader;
314 }
315 }