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.omid.transaction;
19  
20  import com.beust.jcommander.JCommander;
21  import com.beust.jcommander.Parameter;
22  import com.beust.jcommander.ParametersDelegate;
23  import org.apache.omid.tools.hbase.HBaseLogin;
24  import org.apache.omid.tools.hbase.SecureHBaseConfig;
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.HBaseConfiguration;
27  import org.apache.hadoop.hbase.HColumnDescriptor;
28  import org.apache.hadoop.hbase.HTableDescriptor;
29  import org.apache.hadoop.hbase.TableName;
30  import org.apache.hadoop.hbase.client.HBaseAdmin;
31  import org.apache.hadoop.hbase.util.Bytes;
32  
33  import java.io.IOException;
34  
35  public class CompactorUtil {
36  
37      public static void enableOmidCompaction(Configuration conf,
38                                              TableName table, byte[] columnFamily) throws IOException {
39          HBaseAdmin admin = new HBaseAdmin(conf);
40          try {
41              HTableDescriptor desc = admin.getTableDescriptor(table);
42              HColumnDescriptor cfDesc = desc.getFamily(columnFamily);
43              cfDesc.setValue(OmidCompactor.OMID_COMPACTABLE_CF_FLAG,
44                      Boolean.TRUE.toString());
45              admin.modifyColumn(table, cfDesc);
46          } finally {
47              admin.close();
48          }
49      }
50  
51      public static void disableOmidCompaction(Configuration conf,
52                                               TableName table, byte[] columnFamily) throws IOException {
53          HBaseAdmin admin = new HBaseAdmin(conf);
54          try {
55              HTableDescriptor desc = admin.getTableDescriptor(table);
56              HColumnDescriptor cfDesc = desc.getFamily(columnFamily);
57              cfDesc.setValue(OmidCompactor.OMID_COMPACTABLE_CF_FLAG,
58                      Boolean.FALSE.toString());
59              admin.modifyColumn(table, cfDesc);
60          } finally {
61              admin.close();
62          }
63      }
64  
65      static class Config {
66          @Parameter(names = "-table", required = true)
67          String table;
68  
69          @Parameter(names = "-columnFamily", required = false)
70          String columnFamily;
71  
72          @Parameter(names = "-help")
73          boolean help = false;
74  
75          @Parameter(names = "-enable")
76          boolean enable = false;
77  
78          @Parameter(names = "-disable")
79          boolean disable = false;
80  
81          @ParametersDelegate
82          private SecureHBaseConfig loginFlags = new SecureHBaseConfig();
83  
84      }
85  
86      public static void main(String[] args) throws IOException {
87          Config cmdline = new Config();
88          JCommander jcommander = new JCommander(cmdline, args);
89          if (cmdline.help) {
90              jcommander.usage("CompactorUtil");
91              System.exit(1);
92          }
93  
94          HBaseLogin.loginIfNeeded(cmdline.loginFlags);
95  
96          Configuration conf = HBaseConfiguration.create();
97          if (cmdline.enable) {
98              enableOmidCompaction(conf, TableName.valueOf(cmdline.table),
99                      Bytes.toBytes(cmdline.columnFamily));
100         } else if (cmdline.disable) {
101             disableOmidCompaction(conf, TableName.valueOf(cmdline.table),
102                     Bytes.toBytes(cmdline.columnFamily));
103         } else {
104             System.err.println("Must specify enable or disable");
105         }
106     }
107 }