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.hbase.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.classification.InterfaceStability;
32  import org.apache.hadoop.hbase.client.HBaseAdmin;
33  import org.apache.hadoop.hbase.util.Bytes;
34  import org.apache.hadoop.util.Tool;
35  import org.apache.hadoop.util.ToolRunner;
36  import org.apache.zookeeper.KeeperException;
37  
38  import com.google.protobuf.ServiceException;
39  
40  /**
41   * The sweep tool. It deletes the mob files that are not used and merges the small mob files to
42   * bigger ones. Each run of this sweep tool only handles one column family. The runs on
43   * the same column family are mutually exclusive. And the major compaction and sweep tool on the
44   * same column family are mutually exclusive too.
45   */
46  @InterfaceAudience.Public
47  @InterfaceStability.Evolving
48  public class Sweeper extends Configured implements Tool {
49  
50    /**
51     * Sweeps the mob files on one column family. It deletes the unused mob files and merges
52     * the small mob files into bigger ones.
53     * @param tableName The current table name in string format.
54     * @param familyName The column family name.
55     * @return 0 if success, 2 if job aborted with an exception, 3 if unable to start due to
56     *   other compaction,4 if mr job was unsuccessful
57     * @throws IOException
58     * @throws InterruptedException
59     * @throws ClassNotFoundException
60     * @throws KeeperException
61     * @throws ServiceException
62     */
63    int sweepFamily(String tableName, String familyName) throws IOException, InterruptedException,
64        ClassNotFoundException, KeeperException, ServiceException {
65      Configuration conf = getConf();
66      // make sure the target HBase exists.
67      HBaseAdmin.checkHBaseAvailable(conf);
68      HBaseAdmin admin = new HBaseAdmin(conf);
69      try {
70        FileSystem fs = FileSystem.get(conf);
71        TableName tn = TableName.valueOf(tableName);
72        HTableDescriptor htd = admin.getTableDescriptor(tn);
73        HColumnDescriptor family = htd.getFamily(Bytes.toBytes(familyName));
74        if (family == null || !family.isMobEnabled()) {
75            throw new IOException("Column family " + familyName + " is not a MOB column family");
76        }
77        SweepJob job = new SweepJob(conf, fs);
78        // Run the sweeping
79        return job.sweep(tn, family);
80      } catch (Exception e) {
81        System.err.println("Job aborted due to exception " + e);
82        return 2; // job failed
83      } finally {
84        try {
85          admin.close();
86        } catch (IOException e) {
87          System.out.println("Failed to close the HBaseAdmin: " + e.getMessage());
88        }
89      }
90    }
91  
92    public static void main(String[] args) throws Exception {
93      Configuration conf = HBaseConfiguration.create();
94      int ret = ToolRunner.run(conf, new Sweeper(), args);
95      System.exit(ret);
96    }
97  
98    private void printUsage() {
99      System.err.println("Usage:\n" + "--------------------------\n" + Sweeper.class.getName()
100         + " tableName familyName");
101     System.err.println(" tableName        The table name");
102     System.err.println(" familyName       The column family name");
103   }
104 
105   /**
106    * Main method for the tool.
107    * @return 0 if success, 1 for bad args. 2 if job aborted with an exception,
108    *   3 if unable to start due to other compaction, 4 if mr job was unsuccessful
109    */
110   public int run(String[] args) throws Exception {
111     if (args.length != 2) {
112       printUsage();
113       return 1;
114     }
115     String table = args[0];
116     String family = args[1];
117     return sweepFamily(table, family);
118   }
119 }