View Javadoc

1   /**
2    * Copyright 2009 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
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   * Export an HBase table.
38   * Writes content to sequence files up in HDFS.  Use {@link Import} to read it
39   * back in again.
40   */
41  public class Export {
42    final static String NAME = "export";
43  
44    /**
45     * Mapper.
46     */
47    static class Exporter
48    extends TableMapper<ImmutableBytesWritable, Result> {
49      /**
50       * @param row  The current table row key.
51       * @param value  The columns.
52       * @param context  The current context.
53       * @throws IOException When something is broken with the data.
54       * @see org.apache.hadoop.mapreduce.Mapper#map(KEYIN, VALUEIN,
55       *   org.apache.hadoop.mapreduce.Mapper.Context)
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     * Sets up the actual job.
71     *
72     * @param conf  The current configuration.
73     * @param args  The command line parameters.
74     * @return The newly created job.
75     * @throws IOException When setting up the job fails.
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      // TODO: Allow passing filter and subset of rows/columns.
85      Scan s = new Scan();
86      // Optional arguments.
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      // No reducers.  Just write straight to output files.
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    * @param errorMsg Error message.  Can be null.
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    * Main entry point.
118    *
119    * @param args  The command line parameters.
120    * @throws Exception When running the job fails.
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 }