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.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
24 import java.io.DataInputStream;
25 import java.io.DataOutputStream;
26 import java.io.IOException;
27 import java.net.URL;
28 import java.net.URLDecoder;
29 import java.util.Enumeration;
30 import java.util.HashSet;
31 import java.util.Set;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.hadoop.conf.Configuration;
36 import org.apache.hadoop.fs.FileSystem;
37 import org.apache.hadoop.fs.Path;
38 import org.apache.hadoop.hbase.HConstants;
39 import org.apache.hadoop.hbase.client.HTable;
40 import org.apache.hadoop.hbase.client.Scan;
41 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
42 import org.apache.hadoop.hbase.util.Base64;
43 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
44 import org.apache.hadoop.io.Writable;
45 import org.apache.hadoop.io.WritableComparable;
46 import org.apache.hadoop.mapreduce.Job;
47 import org.apache.hadoop.util.StringUtils;
48
49
50
51
52 @SuppressWarnings("unchecked")
53 public class TableMapReduceUtil {
54 static Log LOG = LogFactory.getLog(TableMapReduceUtil.class);
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public static void initTableMapperJob(String table, Scan scan,
70 Class<? extends TableMapper> mapper,
71 Class<? extends WritableComparable> outputKeyClass,
72 Class<? extends Writable> outputValueClass, Job job)
73 throws IOException {
74 initTableMapperJob(table, scan, mapper, outputKeyClass, outputValueClass,
75 job, true);
76 }
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 public static void initTableMapperJob(String table, Scan scan,
94 Class<? extends TableMapper> mapper,
95 Class<? extends WritableComparable> outputKeyClass,
96 Class<? extends Writable> outputValueClass, Job job,
97 boolean addDependencyJars)
98 throws IOException {
99 job.setInputFormatClass(TableInputFormat.class);
100 if (outputValueClass != null) job.setMapOutputValueClass(outputValueClass);
101 if (outputKeyClass != null) job.setMapOutputKeyClass(outputKeyClass);
102 job.setMapperClass(mapper);
103 job.getConfiguration().set(TableInputFormat.INPUT_TABLE, table);
104 job.getConfiguration().set(TableInputFormat.SCAN,
105 convertScanToString(scan));
106 if (addDependencyJars) {
107 addDependencyJars(job);
108 }
109 }
110
111
112
113
114
115
116
117
118 static String convertScanToString(Scan scan) throws IOException {
119 ByteArrayOutputStream out = new ByteArrayOutputStream();
120 DataOutputStream dos = new DataOutputStream(out);
121 scan.write(dos);
122 return Base64.encodeBytes(out.toByteArray());
123 }
124
125
126
127
128
129
130
131
132 static Scan convertStringToScan(String base64) throws IOException {
133 ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decode(base64));
134 DataInputStream dis = new DataInputStream(bis);
135 Scan scan = new Scan();
136 scan.readFields(dis);
137 return scan;
138 }
139
140
141
142
143
144
145
146
147
148
149 public static void initTableReducerJob(String table,
150 Class<? extends TableReducer> reducer, Job job)
151 throws IOException {
152 initTableReducerJob(table, reducer, job, null);
153 }
154
155
156
157
158
159
160
161
162
163
164
165
166 public static void initTableReducerJob(String table,
167 Class<? extends TableReducer> reducer, Job job,
168 Class partitioner) throws IOException {
169 initTableReducerJob(table, reducer, job, partitioner, null, null, null);
170 }
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195 public static void initTableReducerJob(String table,
196 Class<? extends TableReducer> reducer, Job job,
197 Class partitioner, String quorumAddress, String serverClass,
198 String serverImpl) throws IOException {
199 initTableReducerJob(table, reducer, job, partitioner, quorumAddress,
200 serverClass, serverImpl, true);
201 }
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
227
228 public static void initTableReducerJob(String table,
229 Class<? extends TableReducer> reducer, Job job,
230 Class partitioner, String quorumAddress, String serverClass,
231 String serverImpl, boolean addDependencyJars) throws IOException {
232
233 Configuration conf = job.getConfiguration();
234 job.setOutputFormatClass(TableOutputFormat.class);
235 if (reducer != null) job.setReducerClass(reducer);
236 conf.set(TableOutputFormat.OUTPUT_TABLE, table);
237
238 if (quorumAddress != null) {
239
240 ZKUtil.transformClusterKey(quorumAddress);
241 conf.set(TableOutputFormat.QUORUM_ADDRESS,quorumAddress);
242 }
243 if (serverClass != null && serverImpl != null) {
244 conf.set(TableOutputFormat.REGION_SERVER_CLASS, serverClass);
245 conf.set(TableOutputFormat.REGION_SERVER_IMPL, serverImpl);
246 }
247 job.setOutputKeyClass(ImmutableBytesWritable.class);
248 job.setOutputValueClass(Writable.class);
249 if (partitioner == HRegionPartitioner.class) {
250 job.setPartitionerClass(HRegionPartitioner.class);
251 HTable outputTable = new HTable(conf, table);
252 int regions = outputTable.getRegionsInfo().size();
253 if (job.getNumReduceTasks() > regions) {
254 job.setNumReduceTasks(outputTable.getRegionsInfo().size());
255 }
256 } else if (partitioner != null) {
257 job.setPartitionerClass(partitioner);
258 }
259
260 if (addDependencyJars) {
261 addDependencyJars(job);
262 }
263 }
264
265
266
267
268
269
270
271
272
273 public static void limitNumReduceTasks(String table, Job job)
274 throws IOException {
275 HTable outputTable = new HTable(job.getConfiguration(), table);
276 int regions = outputTable.getRegionsInfo().size();
277 if (job.getNumReduceTasks() > regions)
278 job.setNumReduceTasks(regions);
279 }
280
281
282
283
284
285
286
287
288
289 public static void setNumReduceTasks(String table, Job job)
290 throws IOException {
291 HTable outputTable = new HTable(job.getConfiguration(), table);
292 int regions = outputTable.getRegionsInfo().size();
293 job.setNumReduceTasks(regions);
294 }
295
296
297
298
299
300
301
302
303
304
305 public static void setScannerCaching(Job job, int batchSize) {
306 job.getConfiguration().setInt("hbase.client.scanner.caching", batchSize);
307 }
308
309
310
311
312
313
314 public static void addDependencyJars(Job job) throws IOException {
315 try {
316 addDependencyJars(job.getConfiguration(),
317 org.apache.zookeeper.ZooKeeper.class,
318 job.getMapOutputKeyClass(),
319 job.getMapOutputValueClass(),
320 job.getInputFormatClass(),
321 job.getOutputKeyClass(),
322 job.getOutputValueClass(),
323 job.getOutputFormatClass(),
324 job.getPartitionerClass(),
325 job.getCombinerClass());
326 } catch (ClassNotFoundException e) {
327 throw new IOException(e);
328 }
329 }
330
331
332
333
334
335
336 public static void addDependencyJars(Configuration conf,
337 Class... classes) throws IOException {
338
339 FileSystem localFs = FileSystem.getLocal(conf);
340
341 Set<String> jars = new HashSet<String>();
342
343
344 jars.addAll( conf.getStringCollection("tmpjars") );
345
346
347 for (Class clazz : classes) {
348 if (clazz == null) continue;
349
350 String pathStr = findContainingJar(clazz);
351 if (pathStr == null) {
352 LOG.warn("Could not find jar for class " + clazz +
353 " in order to ship it to the cluster.");
354 continue;
355 }
356 Path path = new Path(pathStr);
357 if (!localFs.exists(path)) {
358 LOG.warn("Could not validate jar file " + path + " for class "
359 + clazz);
360 continue;
361 }
362 jars.add(path.makeQualified(localFs).toString());
363 }
364 if (jars.isEmpty()) return;
365
366 conf.set("tmpjars",
367 StringUtils.arrayToString(jars.toArray(new String[0])));
368 }
369
370
371
372
373
374
375
376
377
378
379
380
381 private static String findContainingJar(Class my_class) {
382 ClassLoader loader = my_class.getClassLoader();
383 String class_file = my_class.getName().replaceAll("\\.", "/") + ".class";
384 try {
385 for(Enumeration itr = loader.getResources(class_file);
386 itr.hasMoreElements();) {
387 URL url = (URL) itr.nextElement();
388 if ("jar".equals(url.getProtocol())) {
389 String toReturn = url.getPath();
390 if (toReturn.startsWith("file:")) {
391 toReturn = toReturn.substring("file:".length());
392 }
393
394
395
396
397
398
399 toReturn = toReturn.replaceAll("\\+", "%2B");
400 toReturn = URLDecoder.decode(toReturn, "UTF-8");
401 return toReturn.replaceAll("!.*$", "");
402 }
403 }
404 } catch (IOException e) {
405 throw new RuntimeException(e);
406 }
407 return null;
408 }
409
410
411 }