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