View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.mapreduce;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.hadoop.conf.Configuration;
23  import org.apache.hadoop.conf.Configured;
24  import org.apache.hadoop.fs.Path;
25  import org.apache.hadoop.hbase.Cell;
26  import org.apache.hadoop.hbase.CellUtil;
27  import org.apache.hadoop.hbase.HBaseConfiguration;
28  import org.apache.hadoop.hbase.KeyValue;
29  import org.apache.hadoop.hbase.KeyValueUtil;
30  import org.apache.hadoop.hbase.TableName;
31  import org.apache.hadoop.hbase.classification.InterfaceAudience;
32  import org.apache.hadoop.hbase.classification.InterfaceStability;
33  import org.apache.hadoop.hbase.client.Connection;
34  import org.apache.hadoop.hbase.client.ConnectionFactory;
35  import org.apache.hadoop.hbase.client.Delete;
36  import org.apache.hadoop.hbase.client.Mutation;
37  import org.apache.hadoop.hbase.client.Put;
38  import org.apache.hadoop.hbase.client.RegionLocator;
39  import org.apache.hadoop.hbase.client.Table;
40  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
41  import org.apache.hadoop.hbase.regionserver.wal.WALCellCodec;
42  import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
43  import org.apache.hadoop.hbase.util.Bytes;
44  import org.apache.hadoop.hbase.wal.WALKey;
45  import org.apache.hadoop.mapreduce.Job;
46  import org.apache.hadoop.mapreduce.Mapper;
47  import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
48  import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
49  import org.apache.hadoop.util.GenericOptionsParser;
50  import org.apache.hadoop.util.Tool;
51  import org.apache.hadoop.util.ToolRunner;
52  
53  import java.io.IOException;
54  import java.text.ParseException;
55  import java.text.SimpleDateFormat;
56  import java.util.Map;
57  import java.util.TreeMap;
58  
59  /**
60   * A tool to replay WAL files as a M/R job.
61   * The WAL can be replayed for a set of tables or all tables,
62   * and a timerange can be provided (in milliseconds).
63   * The WAL is filtered to the passed set of tables and  the output
64   * can optionally be mapped to another set of tables.
65   *
66   * WAL replay can also generate HFiles for later bulk importing,
67   * in that case the WAL is replayed for a single table only.
68   */
69  @InterfaceAudience.Public
70  @InterfaceStability.Stable
71  public class WALPlayer extends Configured implements Tool {
72    private static final Log LOG = LogFactory.getLog(WALPlayer.class);
73    final static String NAME = "WALPlayer";
74    final static String BULK_OUTPUT_CONF_KEY = "wal.bulk.output";
75    final static String TABLES_KEY = "wal.input.tables";
76    final static String TABLE_MAP_KEY = "wal.input.tablesmap";
77  
78    // This relies on Hadoop Configuration to handle warning about deprecated configs and
79    // to set the correct non-deprecated configs when an old one shows up.
80    static {
81      Configuration.addDeprecation("hlog.bulk.output", BULK_OUTPUT_CONF_KEY);
82      Configuration.addDeprecation("hlog.input.tables", TABLES_KEY);
83      Configuration.addDeprecation("hlog.input.tablesmap", TABLE_MAP_KEY);
84      Configuration.addDeprecation(HLogInputFormat.START_TIME_KEY, WALInputFormat.START_TIME_KEY);
85      Configuration.addDeprecation(HLogInputFormat.END_TIME_KEY, WALInputFormat.END_TIME_KEY);
86    }
87  
88    /**
89     * A mapper that just writes out KeyValues.
90     * This one can be used together with {@link KeyValueSortReducer}
91     */
92    static class WALKeyValueMapper
93    extends Mapper<WALKey, WALEdit, ImmutableBytesWritable, KeyValue> {
94      private byte[] table;
95  
96      @Override
97      public void map(WALKey key, WALEdit value,
98        Context context)
99      throws IOException {
100       try {
101         // skip all other tables
102         if (Bytes.equals(table, key.getTablename().getName())) {
103           for (Cell cell : value.getCells()) {
104             KeyValue kv = KeyValueUtil.ensureKeyValueTypeForMR(cell);
105             if (WALEdit.isMetaEditFamily(kv.getFamily())) continue;
106             context.write(new ImmutableBytesWritable(kv.getRow()), kv);
107           }
108         }
109       } catch (InterruptedException e) {
110         e.printStackTrace();
111       }
112     }
113 
114     @Override
115     public void setup(Context context) throws IOException {
116       // only a single table is supported when HFiles are generated with HFileOutputFormat
117       String tables[] = context.getConfiguration().getStrings(TABLES_KEY);
118       if (tables == null || tables.length != 1) {
119         // this can only happen when WALMapper is used directly by a class other than WALPlayer
120         throw new IOException("Exactly one table must be specified for bulk HFile case.");
121       }
122       table = Bytes.toBytes(tables[0]);
123     }
124   }
125 
126   /**
127    * A mapper that writes out {@link Mutation} to be directly applied to
128    * a running HBase instance.
129    */
130   protected static class WALMapper
131   extends Mapper<WALKey, WALEdit, ImmutableBytesWritable, Mutation> {
132     private Map<TableName, TableName> tables = new TreeMap<TableName, TableName>();
133 
134     @Override
135     public void map(WALKey key, WALEdit value, Context context)
136     throws IOException {
137       try {
138         if (tables.isEmpty() || tables.containsKey(key.getTablename())) {
139           TableName targetTable = tables.isEmpty() ?
140                 key.getTablename() :
141                 tables.get(key.getTablename());
142           ImmutableBytesWritable tableOut = new ImmutableBytesWritable(targetTable.getName());
143           Put put = null;
144           Delete del = null;
145           Cell lastCell = null;
146           for (Cell cell : value.getCells()) {
147             // filtering WAL meta entries
148             if (WALEdit.isMetaEditFamily(cell.getFamily())) continue;
149 
150             // Allow a subclass filter out this cell.
151             if (filter(context, cell)) {
152               // A WALEdit may contain multiple operations (HBASE-3584) and/or
153               // multiple rows (HBASE-5229).
154               // Aggregate as much as possible into a single Put/Delete
155               // operation before writing to the context.
156               if (lastCell == null || lastCell.getTypeByte() != cell.getTypeByte()
157                   || !CellUtil.matchingRow(lastCell, cell)) {
158                 // row or type changed, write out aggregate KVs.
159                 if (put != null) context.write(tableOut, put);
160                 if (del != null) context.write(tableOut, del);
161                 if (CellUtil.isDelete(cell)) {
162                   del = new Delete(cell.getRow());
163                 } else {
164                   put = new Put(cell.getRow());
165                 }
166               }
167               if (CellUtil.isDelete(cell)) {
168                 del.addDeleteMarker(cell);
169               } else {
170                 put.add(cell);
171               }
172             }
173             lastCell = cell;
174           }
175           // write residual KVs
176           if (put != null) context.write(tableOut, put);
177           if (del != null) context.write(tableOut, del);
178         }
179       } catch (InterruptedException e) {
180         e.printStackTrace();
181       }
182     }
183 
184     /**
185      * @param cell
186      * @return Return true if we are to emit this cell.
187      */
188     protected boolean filter(Context context, final Cell cell) {
189       return true;
190     }
191 
192     @Override
193     public void setup(Context context) throws IOException {
194       String[] tableMap = context.getConfiguration().getStrings(TABLE_MAP_KEY);
195       String[] tablesToUse = context.getConfiguration().getStrings(TABLES_KEY);
196       if (tablesToUse == null && tableMap == null) {
197         // Then user wants all tables.
198       } else if (tablesToUse == null || tableMap == null || tablesToUse.length != tableMap.length) {
199         // this can only happen when WALMapper is used directly by a class other than WALPlayer
200         throw new IOException("No tables or incorrect table mapping specified.");
201       }
202       int i = 0;
203       if (tablesToUse != null) {
204         for (String table : tablesToUse) {
205           tables.put(TableName.valueOf(table),
206             TableName.valueOf(tableMap[i++]));
207         }
208       }
209     }
210   }
211 
212   /**
213    * @param conf The {@link Configuration} to use.
214    */
215   public WALPlayer(Configuration conf) {
216     super(conf);
217   }
218 
219   void setupTime(Configuration conf, String option) throws IOException {
220     String val = conf.get(option);
221     if (null == val) return;
222     long ms;
223     try {
224       // first try to parse in user friendly form
225       ms = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS").parse(val).getTime();
226     } catch (ParseException pe) {
227       try {
228         // then see if just a number of ms's was specified
229         ms = Long.parseLong(val);
230       } catch (NumberFormatException nfe) {
231         throw new IOException(option
232             + " must be specified either in the form 2001-02-20T16:35:06.99 "
233             + "or as number of milliseconds");
234       }
235     }
236     conf.setLong(option, ms);
237   }
238 
239   /**
240    * Sets up the actual job.
241    *
242    * @param args  The command line parameters.
243    * @return The newly created job.
244    * @throws IOException When setting up the job fails.
245    */
246   public Job createSubmittableJob(String[] args)
247   throws IOException {
248     Configuration conf = getConf();
249     setupTime(conf, HLogInputFormat.START_TIME_KEY);
250     setupTime(conf, HLogInputFormat.END_TIME_KEY);
251     Path inputDir = new Path(args[0]);
252     String[] tables = args[1].split(",");
253     String[] tableMap;
254     if (args.length > 2) {
255       tableMap = args[2].split(",");
256       if (tableMap.length != tables.length) {
257         throw new IOException("The same number of tables and mapping must be provided.");
258       }
259     } else {
260       // if not mapping is specified map each table to itself
261       tableMap = tables;
262     }
263     conf.setStrings(TABLES_KEY, tables);
264     conf.setStrings(TABLE_MAP_KEY, tableMap);
265     Job job = new Job(conf, NAME + "_" + inputDir);
266     job.setJarByClass(WALPlayer.class);
267     FileInputFormat.setInputPaths(job, inputDir);
268     job.setInputFormatClass(WALInputFormat.class);
269     job.setMapOutputKeyClass(ImmutableBytesWritable.class);
270     String hfileOutPath = conf.get(BULK_OUTPUT_CONF_KEY);
271     if (hfileOutPath != null) {
272       // the bulk HFile case
273       if (tables.length != 1) {
274         throw new IOException("Exactly one table must be specified for the bulk export option");
275       }
276       TableName tableName = TableName.valueOf(tables[0]);
277       job.setMapperClass(WALKeyValueMapper.class);
278       job.setReducerClass(KeyValueSortReducer.class);
279       Path outputDir = new Path(hfileOutPath);
280       FileOutputFormat.setOutputPath(job, outputDir);
281       job.setMapOutputValueClass(KeyValue.class);
282       try (Connection conn = ConnectionFactory.createConnection(conf);
283           Table table = conn.getTable(tableName);
284           RegionLocator regionLocator = conn.getRegionLocator(tableName)) {
285         HFileOutputFormat2.configureIncrementalLoad(job, table.getTableDescriptor(), regionLocator);
286       }
287       TableMapReduceUtil.addDependencyJars(job.getConfiguration(),
288           com.google.common.base.Preconditions.class);
289     } else {
290       // output to live cluster
291       job.setMapperClass(WALMapper.class);
292       job.setOutputFormatClass(MultiTableOutputFormat.class);
293       TableMapReduceUtil.addDependencyJars(job);
294       TableMapReduceUtil.initCredentials(job);
295       // No reducers.
296       job.setNumReduceTasks(0);
297     }
298     String codecCls = WALCellCodec.getWALCellCodecClass(conf).getName();
299     try {
300       TableMapReduceUtil.addDependencyJars(conf, Class.forName(codecCls));
301     } catch (Exception e) {
302       throw new IOException("Cannot determine wal codec class " + codecCls, e);
303     }
304     return job;
305   }
306 
307   /*
308    * @param errorMsg Error message.  Can be null.
309    */
310   private void usage(final String errorMsg) {
311     if (errorMsg != null && errorMsg.length() > 0) {
312       System.err.println("ERROR: " + errorMsg);
313     }
314     System.err.println("Usage: " + NAME + " [options] <wal inputdir> <tables> [<tableMappings>]");
315     System.err.println("Read all WAL entries for <tables>.");
316     System.err.println("If no tables (\"\") are specific, all tables are imported.");
317     System.err.println("(Careful, even -ROOT- and hbase:meta entries will be imported in that case.)");
318     System.err.println("Otherwise <tables> is a comma separated list of tables.\n");
319     System.err.println("The WAL entries can be mapped to new set of tables via <tableMapping>.");
320     System.err.println("<tableMapping> is a command separated list of targettables.");
321     System.err.println("If specified, each table in <tables> must have a mapping.\n");
322     System.err.println("By default " + NAME + " will load data directly into HBase.");
323     System.err.println("To generate HFiles for a bulk data load instead, pass the option:");
324     System.err.println("  -D" + BULK_OUTPUT_CONF_KEY + "=/path/for/output");
325     System.err.println("  (Only one table can be specified, and no mapping is allowed!)");
326     System.err.println("Other options: (specify time range to WAL edit to consider)");
327     System.err.println("  -D" + WALInputFormat.START_TIME_KEY + "=[date|ms]");
328     System.err.println("  -D" + WALInputFormat.END_TIME_KEY + "=[date|ms]");
329     System.err.println("For performance also consider the following options:\n"
330         + "  -Dmapreduce.map.speculative=false\n"
331         + "  -Dmapreduce.reduce.speculative=false");
332   }
333 
334   /**
335    * Main entry point.
336    *
337    * @param args  The command line parameters.
338    * @throws Exception When running the job fails.
339    */
340   public static void main(String[] args) throws Exception {
341     int ret = ToolRunner.run(new WALPlayer(HBaseConfiguration.create()), args);
342     System.exit(ret);
343   }
344 
345   @Override
346   public int run(String[] args) throws Exception {
347     String[] otherArgs = new GenericOptionsParser(getConf(), args).getRemainingArgs();
348     if (otherArgs.length < 2) {
349       usage("Wrong number of arguments: " + otherArgs.length);
350       System.exit(-1);
351     }
352     Job job = createSubmittableJob(otherArgs);
353     return job.waitForCompletion(true) ? 0 : 1;
354   }
355 }