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.IOException;
23
24 import org.apache.hadoop.conf.Configuration;
25 import org.apache.hadoop.fs.Path;
26 import org.apache.hadoop.hbase.HBaseConfiguration;
27 import org.apache.hadoop.hbase.client.Result;
28 import org.apache.hadoop.hbase.client.Scan;
29 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
30 import org.apache.hadoop.mapreduce.Job;
31 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
32 import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;
33 import org.apache.hadoop.util.GenericOptionsParser;
34 import org.mortbay.log.Log;
35
36
37
38
39
40
41 public class Export {
42 final static String NAME = "export";
43
44
45
46
47 static class Exporter
48 extends TableMapper<ImmutableBytesWritable, Result> {
49
50
51
52
53
54
55
56
57 @Override
58 public void map(ImmutableBytesWritable row, Result value,
59 Context context)
60 throws IOException {
61 try {
62 context.write(row, value);
63 } catch (InterruptedException e) {
64 e.printStackTrace();
65 }
66 }
67 }
68
69
70
71
72
73
74
75
76
77 public static Job createSubmittableJob(Configuration conf, String[] args)
78 throws IOException {
79 String tableName = args[0];
80 Path outputDir = new Path(args[1]);
81 Job job = new Job(conf, NAME + "_" + tableName);
82 job.setJobName(NAME + "_" + tableName);
83 job.setJarByClass(Exporter.class);
84
85 Scan s = new Scan();
86
87 int versions = args.length > 2? Integer.parseInt(args[2]): 1;
88 s.setMaxVersions(versions);
89 long startTime = args.length > 3? Long.parseLong(args[3]): 0L;
90 long endTime = args.length > 4? Long.parseLong(args[4]): Long.MAX_VALUE;
91 s.setTimeRange(startTime, endTime);
92 Log.info("verisons=" + versions + ", starttime=" + startTime +
93 ", endtime=" + endTime);
94 TableMapReduceUtil.initTableMapperJob(tableName, s, Exporter.class, null,
95 null, job);
96
97 job.setNumReduceTasks(0);
98 job.setOutputFormatClass(SequenceFileOutputFormat.class);
99 job.setOutputKeyClass(ImmutableBytesWritable.class);
100 job.setOutputValueClass(Result.class);
101 FileOutputFormat.setOutputPath(job, outputDir);
102 return job;
103 }
104
105
106
107
108 private static void usage(final String errorMsg) {
109 if (errorMsg != null && errorMsg.length() > 0) {
110 System.err.println("ERROR: " + errorMsg);
111 }
112 System.err.println("Usage: Export <tablename> <outputdir> [<versions> " +
113 "[<starttime> [<endtime>]]]");
114 }
115
116
117
118
119
120
121
122 public static void main(String[] args) throws Exception {
123 Configuration conf = HBaseConfiguration.create();
124 String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
125 if (otherArgs.length < 2) {
126 usage("Wrong number of arguments: " + otherArgs.length);
127 System.exit(-1);
128 }
129 Job job = createSubmittableJob(conf, otherArgs);
130 System.exit(job.waitForCompletion(true)? 0 : 1);
131 }
132 }