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
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hadoop.classification.InterfaceAudience;
26 import org.apache.hadoop.classification.InterfaceStability;
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.fs.FileSystem;
29 import org.apache.hadoop.fs.Path;
30 import org.apache.hadoop.hbase.HBaseConfiguration;
31 import org.apache.hadoop.hbase.client.Result;
32 import org.apache.hadoop.hbase.client.Scan;
33 import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
34 import org.apache.hadoop.hbase.filter.Filter;
35 import org.apache.hadoop.hbase.filter.IncompatibleFilterException;
36 import org.apache.hadoop.hbase.filter.PrefixFilter;
37 import org.apache.hadoop.hbase.filter.RegexStringComparator;
38 import org.apache.hadoop.hbase.filter.RowFilter;
39 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
40 import org.apache.hadoop.hbase.util.Bytes;
41 import org.apache.hadoop.mapreduce.Job;
42 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
43 import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;
44 import org.apache.hadoop.util.GenericOptionsParser;
45
46
47
48
49
50
51 @InterfaceAudience.Public
52 @InterfaceStability.Stable
53 public class Export {
54 private static final Log LOG = LogFactory.getLog(Export.class);
55 final static String NAME = "export";
56 final static String RAW_SCAN = "hbase.mapreduce.include.deleted.rows";
57 final static String EXPORT_BATCHING = "hbase.export.scanner.batch";
58
59
60
61
62
63
64
65
66
67 public static Job createSubmittableJob(Configuration conf, String[] args)
68 throws IOException {
69 String tableName = args[0];
70 Path outputDir = new Path(args[1]);
71 Job job = new Job(conf, NAME + "_" + tableName);
72 job.setJobName(NAME + "_" + tableName);
73 job.setJarByClass(Export.class);
74
75 Scan s = getConfiguredScanForJob(conf, args);
76 IdentityTableMapper.initJob(tableName, s, IdentityTableMapper.class, job);
77
78 job.setNumReduceTasks(0);
79 job.setOutputFormatClass(SequenceFileOutputFormat.class);
80 job.setOutputKeyClass(ImmutableBytesWritable.class);
81 job.setOutputValueClass(Result.class);
82 FileOutputFormat.setOutputPath(job, outputDir);
83 return job;
84 }
85
86 private static Scan getConfiguredScanForJob(Configuration conf, String[] args) throws IOException {
87 Scan s = new Scan();
88
89
90 int versions = args.length > 2? Integer.parseInt(args[2]): 1;
91 s.setMaxVersions(versions);
92
93 long startTime = args.length > 3? Long.parseLong(args[3]): 0L;
94 long endTime = args.length > 4? Long.parseLong(args[4]): Long.MAX_VALUE;
95 s.setTimeRange(startTime, endTime);
96
97 s.setCacheBlocks(false);
98
99 if (conf.get(TableInputFormat.SCAN_ROW_START) != null) {
100 s.setStartRow(Bytes.toBytes(conf.get(TableInputFormat.SCAN_ROW_START)));
101 }
102 if (conf.get(TableInputFormat.SCAN_ROW_STOP) != null) {
103 s.setStopRow(Bytes.toBytes(conf.get(TableInputFormat.SCAN_ROW_STOP)));
104 }
105
106 boolean raw = Boolean.parseBoolean(conf.get(RAW_SCAN));
107 if (raw) {
108 s.setRaw(raw);
109 }
110
111 if (conf.get(TableInputFormat.SCAN_COLUMN_FAMILY) != null) {
112 s.addFamily(Bytes.toBytes(conf.get(TableInputFormat.SCAN_COLUMN_FAMILY)));
113 }
114
115 Filter exportFilter = getExportFilter(args);
116 if (exportFilter!= null) {
117 LOG.info("Setting Scan Filter for Export.");
118 s.setFilter(exportFilter);
119 }
120
121 int batching = conf.getInt(EXPORT_BATCHING, -1);
122 if (batching != -1){
123 try {
124 s.setBatch(batching);
125 } catch (IncompatibleFilterException e) {
126 LOG.error("Batching could not be set", e);
127 }
128 }
129 LOG.info("versions=" + versions + ", starttime=" + startTime +
130 ", endtime=" + endTime + ", keepDeletedCells=" + raw);
131 return s;
132 }
133
134 private static Filter getExportFilter(String[] args) {
135 Filter exportFilter = null;
136 String filterCriteria = (args.length > 5) ? args[5]: null;
137 if (filterCriteria == null) return null;
138 if (filterCriteria.startsWith("^")) {
139 String regexPattern = filterCriteria.substring(1, filterCriteria.length());
140 exportFilter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator(regexPattern));
141 } else {
142 exportFilter = new PrefixFilter(Bytes.toBytes(filterCriteria));
143 }
144 return exportFilter;
145 }
146
147
148
149
150 private static void usage(final String errorMsg) {
151 if (errorMsg != null && errorMsg.length() > 0) {
152 System.err.println("ERROR: " + errorMsg);
153 }
154 System.err.println("Usage: Export [-D <property=value>]* <tablename> <outputdir> [<versions> " +
155 "[<starttime> [<endtime>]] [^[regex pattern] or [Prefix] to filter]]\n");
156 System.err.println(" Note: -D properties will be applied to the conf used. ");
157 System.err.println(" For example: ");
158 System.err.println(" -D mapred.output.compress=true");
159 System.err.println(" -D mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec");
160 System.err.println(" -D mapred.output.compression.type=BLOCK");
161 System.err.println(" Additionally, the following SCAN properties can be specified");
162 System.err.println(" to control/limit what is exported..");
163 System.err.println(" -D " + TableInputFormat.SCAN_COLUMN_FAMILY + "=<familyName>");
164 System.err.println(" -D " + RAW_SCAN + "=true");
165 System.err.println(" -D " + TableInputFormat.SCAN_ROW_START + "=<ROWSTART>");
166 System.err.println(" -D " + TableInputFormat.SCAN_ROW_STOP + "=<ROWSTOP>");
167 System.err.println("For performance consider the following properties:\n"
168 + " -Dhbase.client.scanner.caching=100\n"
169 + " -Dmapred.map.tasks.speculative.execution=false\n"
170 + " -Dmapred.reduce.tasks.speculative.execution=false");
171 System.err.println("For tables with very wide rows consider setting the batch size as below:\n"
172 + " -D" + EXPORT_BATCHING + "=10");
173 }
174
175
176
177
178
179
180
181 public static void main(String[] args) throws Exception {
182 Configuration conf = HBaseConfiguration.create();
183 String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
184 if (otherArgs.length < 2) {
185 usage("Wrong number of arguments: " + otherArgs.length);
186 System.exit(-1);
187 }
188 Job job = createSubmittableJob(conf, otherArgs);
189 System.exit(job.waitForCompletion(true)? 0 : 1);
190 }
191 }