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