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.snapshot;
20  
21  import org.apache.commons.cli.CommandLine;
22  import org.apache.hadoop.hbase.TableName;
23  import org.apache.hadoop.hbase.client.HBaseAdmin;
24  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
25  import org.apache.hadoop.hbase.util.AbstractHBaseTool;
26  
27  import java.util.Arrays;
28  
29  
30  /**
31   * This is a command line class that will snapshot a given table.
32   */
33  public class CreateSnapshot extends AbstractHBaseTool {
34      private String tableName = null;
35      private String snapshotName = null;
36      private String snapshotType = null;
37  
38      public static void main(String[] args) {
39          new CreateSnapshot().doStaticMain(args);
40      }
41  
42      @Override
43      protected void addOptions() {
44          this.addRequiredOptWithArg("t", "table", "The name of the table");
45          this.addRequiredOptWithArg("n", "name", "The name of the created snapshot");
46          this.addOptWithArg("s", "snapshot_type",
47                  "Snapshot Type. FLUSH is default. Posible values are "
48                  + Arrays.toString(HBaseProtos.SnapshotDescription.Type.values()));
49      }
50  
51      @Override
52      protected void processOptions(CommandLine cmd) {
53          this.tableName = cmd.getOptionValue('t');
54          this.snapshotName = cmd.getOptionValue('n');
55          this.snapshotType = cmd.getOptionValue('s');
56  
57      }
58  
59      @Override
60      protected int doWork() throws Exception {
61          HBaseAdmin admin = null;
62          try {
63              admin = new HBaseAdmin(conf);
64              HBaseProtos.SnapshotDescription.Type type = HBaseProtos.SnapshotDescription.Type.FLUSH;
65              if (snapshotType != null) {
66                  type = HBaseProtos.SnapshotDescription.Type.valueOf(snapshotName.toUpperCase());
67              }
68  
69              admin.snapshot(snapshotName, TableName.valueOf(tableName), type);
70          } catch (Exception e) {
71              return -1;
72          } finally {
73              if (admin != null) {
74                  admin.close();
75              }
76          }
77          return 0;
78      }
79  
80  }