View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.mob.mapreduce;
20  
21  import java.io.IOException;
22  
23  import org.apache.hadoop.classification.InterfaceAudience;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.conf.Configured;
26  import org.apache.hadoop.fs.FileSystem;
27  import org.apache.hadoop.hbase.HBaseConfiguration;
28  import org.apache.hadoop.hbase.HColumnDescriptor;
29  import org.apache.hadoop.hbase.HTableDescriptor;
30  import org.apache.hadoop.hbase.TableName;
31  import org.apache.hadoop.hbase.client.HBaseAdmin;
32  import org.apache.hadoop.hbase.util.Bytes;
33  import org.apache.hadoop.util.Tool;
34  import org.apache.hadoop.util.ToolRunner;
35  import org.apache.zookeeper.KeeperException;
36  
37  import com.google.protobuf.ServiceException;
38  
39  /**
40   * The sweep tool. It deletes the mob files that are not used and merges the small mob files to
41   * bigger ones. Each run of this sweep tool only handles one column family. The runs on
42   * the same column family are mutually exclusive. And the major compaction and sweep tool on the
43   * same column family are mutually exclusive too.
44   */
45  @InterfaceAudience.Public
46  public class Sweeper extends Configured implements Tool {
47  
48    /**
49     * Sweeps the mob files on one column family. It deletes the unused mob files and merges
50     * the small mob files into bigger ones.
51     * @param tableName The current table name in string format.
52     * @param familyName The column family name.
53     * @return 0 if success, 2 if job aborted with an exception, 3 if unable to start due to
54     *   other compaction,4 if mr job was unsuccessful
55     * @throws IOException
56     * @throws InterruptedException
57     * @throws ClassNotFoundException
58     * @throws KeeperException
59     * @throws ServiceException
60     */
61    int sweepFamily(String tableName, String familyName) throws IOException, InterruptedException,
62        ClassNotFoundException, KeeperException, ServiceException {
63      Configuration conf = getConf();
64      // make sure the target HBase exists.
65      HBaseAdmin.checkHBaseAvailable(conf);
66      HBaseAdmin admin = new HBaseAdmin(conf);
67      try {
68        FileSystem fs = FileSystem.get(conf);
69        TableName tn = TableName.valueOf(tableName);
70        HTableDescriptor htd = admin.getTableDescriptor(tn);
71        HColumnDescriptor family = htd.getFamily(Bytes.toBytes(familyName));
72        if (family == null || !family.isMobEnabled()) {
73            throw new IOException("Column family " + familyName + " is not a MOB column family");
74        }
75        SweepJob job = new SweepJob(conf, fs);
76        // Run the sweeping
77        return job.sweep(tn, family);
78      } catch (Exception e) {
79        System.err.println("Job aborted due to exception " + e);
80        return 2; // job failed
81      } finally {
82        try {
83          admin.close();
84        } catch (IOException e) {
85          System.out.println("Fail to close the HBaseAdmin: " + e.getMessage());
86        }
87      }
88    }
89  
90    public static void main(String[] args) throws Exception {
91      Configuration conf = HBaseConfiguration.create();
92      int ret = ToolRunner.run(conf, new Sweeper(), args);
93      System.exit(ret);
94    }
95  
96    private void printUsage() {
97      System.err.println("Usage:\n" + "--------------------------\n" + Sweeper.class.getName()
98          + " tableName familyName");
99      System.err.println(" tableName        The table name");
100     System.err.println(" familyName       The column family name");
101   }
102 
103   /**
104    * Main method for the tool.
105    * @return 0 if success, 1 for bad args. 2 if job aborted with an exception,
106    *   3 if unable to start due to other compaction, 4 if mr job was unsuccessful
107    */
108   public int run(String[] args) throws Exception {
109     if (args.length != 2) {
110       printUsage();
111       return 1;
112     }
113     String table = args[0];
114     String family = args[1];
115     return sweepFamily(table, family);
116   }
117 }