1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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 }