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