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