1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.mapreduce;
19
20 import java.io.IOException;
21 import java.text.MessageFormat;
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.hbase.classification.InterfaceAudience;
28 import org.apache.hadoop.hbase.classification.InterfaceStability;
29 import org.apache.hadoop.hbase.HRegionInfo;
30 import org.apache.hadoop.hbase.HRegionLocation;
31 import org.apache.hadoop.hbase.TableName;
32 import org.apache.hadoop.hbase.client.Connection;
33 import org.apache.hadoop.hbase.client.ConnectionFactory;
34 import org.apache.hadoop.hbase.client.RegionLocator;
35 import org.apache.hadoop.hbase.client.Result;
36 import org.apache.hadoop.hbase.client.Scan;
37 import org.apache.hadoop.hbase.client.Table;
38 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
39 import org.apache.hadoop.hbase.util.Bytes;
40 import org.apache.hadoop.hbase.util.Pair;
41 import org.apache.hadoop.hbase.util.RegionSizeCalculator;
42 import org.apache.hadoop.mapreduce.InputFormat;
43 import org.apache.hadoop.mapreduce.InputSplit;
44 import org.apache.hadoop.mapreduce.JobContext;
45 import org.apache.hadoop.mapreduce.RecordReader;
46 import org.apache.hadoop.mapreduce.TaskAttemptContext;
47
48 import java.util.Map;
49 import java.util.HashMap;
50 import java.util.Iterator;
51
52
53
54
55
56 @InterfaceAudience.Public
57 @InterfaceStability.Evolving
58 public abstract class MultiTableInputFormatBase extends
59 InputFormat<ImmutableBytesWritable, Result> {
60
61 private static final Log LOG = LogFactory.getLog(MultiTableInputFormatBase.class);
62
63
64 private List<Scan> scans;
65
66
67 private TableRecordReader tableRecordReader = null;
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 @Override
83 public RecordReader<ImmutableBytesWritable, Result> createRecordReader(
84 InputSplit split, TaskAttemptContext context)
85 throws IOException, InterruptedException {
86 TableSplit tSplit = (TableSplit) split;
87 LOG.info(MessageFormat.format("Input split length: {0} bytes.", tSplit.getLength()));
88
89 if (tSplit.getTable() == null) {
90 throw new IOException("Cannot create a record reader because of a"
91 + " previous error. Please look at the previous logs lines from"
92 + " the task's full log for more details.");
93 }
94 final Connection connection = ConnectionFactory.createConnection(context.getConfiguration());
95 Table table = connection.getTable(tSplit.getTable());
96
97 if (this.tableRecordReader == null) {
98 this.tableRecordReader = new TableRecordReader();
99 }
100 final TableRecordReader trr = this.tableRecordReader;
101
102 try {
103 Scan sc = tSplit.getScan();
104 sc.setStartRow(tSplit.getStartRow());
105 sc.setStopRow(tSplit.getEndRow());
106 trr.setScan(sc);
107 trr.setTable(table);
108 return new RecordReader<ImmutableBytesWritable, Result>() {
109
110 @Override
111 public void close() throws IOException {
112 trr.close();
113 connection.close();
114 }
115
116 @Override
117 public ImmutableBytesWritable getCurrentKey() throws IOException, InterruptedException {
118 return trr.getCurrentKey();
119 }
120
121 @Override
122 public Result getCurrentValue() throws IOException, InterruptedException {
123 return trr.getCurrentValue();
124 }
125
126 @Override
127 public float getProgress() throws IOException, InterruptedException {
128 return trr.getProgress();
129 }
130
131 @Override
132 public void initialize(InputSplit inputsplit, TaskAttemptContext context)
133 throws IOException, InterruptedException {
134 trr.initialize(inputsplit, context);
135 }
136
137 @Override
138 public boolean nextKeyValue() throws IOException, InterruptedException {
139 return trr.nextKeyValue();
140 }
141 };
142 } catch (IOException ioe) {
143
144
145 trr.close();
146 connection.close();
147 throw ioe;
148 }
149 }
150
151
152
153
154
155
156
157
158
159
160 @Override
161 public List<InputSplit> getSplits(JobContext context) throws IOException {
162 if (scans.isEmpty()) {
163 throw new IOException("No scans were provided.");
164 }
165
166 Map<TableName, List<Scan>> tableMaps = new HashMap<TableName, List<Scan>>();
167 for (Scan scan : scans) {
168 byte[] tableNameBytes = scan.getAttribute(Scan.SCAN_ATTRIBUTES_TABLE_NAME);
169 if (tableNameBytes == null)
170 throw new IOException("A scan object did not have a table name");
171
172 TableName tableName = TableName.valueOf(tableNameBytes);
173
174 List<Scan> scanList = tableMaps.get(tableName);
175 if (scanList == null) {
176 scanList = new ArrayList<Scan>();
177 tableMaps.put(tableName, scanList);
178 }
179 scanList.add(scan);
180 }
181
182 List<InputSplit> splits = new ArrayList<InputSplit>();
183 Iterator iter = tableMaps.entrySet().iterator();
184
185 try (Connection conn = ConnectionFactory.createConnection(context.getConfiguration())) {
186 while (iter.hasNext()) {
187 Map.Entry<TableName, List<Scan>> entry = (Map.Entry<TableName, List<Scan>>) iter.next();
188 TableName tableName = entry.getKey();
189 List<Scan> scanList = entry.getValue();
190
191 try (Table table = conn.getTable(tableName);
192 RegionLocator regionLocator = conn.getRegionLocator(tableName)) {
193 RegionSizeCalculator sizeCalculator = new RegionSizeCalculator(
194 regionLocator, conn.getAdmin());
195 Pair<byte[][], byte[][]> keys = regionLocator.getStartEndKeys();
196 for (Scan scan : scanList) {
197 if (keys == null || keys.getFirst() == null || keys.getFirst().length == 0) {
198 throw new IOException("Expecting at least one region for table : "
199 + tableName.getNameAsString());
200 }
201 int count = 0;
202
203 byte[] startRow = scan.getStartRow();
204 byte[] stopRow = scan.getStopRow();
205
206 for (int i = 0; i < keys.getFirst().length; i++) {
207 if (!includeRegionInSplit(keys.getFirst()[i], keys.getSecond()[i])) {
208 continue;
209 }
210
211 if ((startRow.length == 0 || keys.getSecond()[i].length == 0 ||
212 Bytes.compareTo(startRow, keys.getSecond()[i]) < 0) &&
213 (stopRow.length == 0 || Bytes.compareTo(stopRow,
214 keys.getFirst()[i]) > 0)) {
215 byte[] splitStart = startRow.length == 0 ||
216 Bytes.compareTo(keys.getFirst()[i], startRow) >= 0 ?
217 keys.getFirst()[i] : startRow;
218 byte[] splitStop = (stopRow.length == 0 ||
219 Bytes.compareTo(keys.getSecond()[i], stopRow) <= 0) &&
220 keys.getSecond()[i].length > 0 ?
221 keys.getSecond()[i] : stopRow;
222
223 HRegionLocation hregionLocation = regionLocator.getRegionLocation(
224 keys.getFirst()[i], false);
225 String regionHostname = hregionLocation.getHostname();
226 HRegionInfo regionInfo = hregionLocation.getRegionInfo();
227 long regionSize = sizeCalculator.getRegionSize(
228 regionInfo.getRegionName());
229
230 TableSplit split = new TableSplit(table.getName(),
231 scan, splitStart, splitStop, regionHostname, regionSize);
232
233 splits.add(split);
234
235 if (LOG.isDebugEnabled()) {
236 LOG.debug("getSplits: split -> " + (count++) + " -> " + split);
237 }
238 }
239 }
240 }
241 }
242 }
243 }
244
245 return splits;
246 }
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270 protected boolean includeRegionInSplit(final byte[] startKey,
271 final byte[] endKey) {
272 return true;
273 }
274
275
276
277
278 protected List<Scan> getScans() {
279 return this.scans;
280 }
281
282
283
284
285
286
287 protected void setScans(List<Scan> scans) {
288 this.scans = scans;
289 }
290
291
292
293
294
295
296
297 protected void setTableRecordReader(TableRecordReader tableRecordReader) {
298 this.tableRecordReader = tableRecordReader;
299 }
300 }