1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.backup;
20
21 import static org.junit.Assert.assertTrue;
22
23 import java.util.List;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.hbase.TableName;
28 import org.apache.hadoop.hbase.client.BackupAdmin;
29 import org.apache.hadoop.hbase.client.Connection;
30 import org.apache.hadoop.hbase.client.ConnectionFactory;
31 import org.apache.hadoop.hbase.client.HBaseAdmin;
32 import org.apache.hadoop.hbase.client.HTable;
33 import org.apache.hadoop.hbase.client.Put;
34 import org.apache.hadoop.hbase.testclassification.LargeTests;
35 import org.apache.hadoop.hbase.util.Bytes;
36 import org.hamcrest.CoreMatchers;
37 import org.junit.Assert;
38 import org.junit.Test;
39 import org.junit.experimental.categories.Category;
40
41 import com.google.common.collect.Lists;
42
43
44
45
46
47
48
49
50
51
52 @Category(LargeTests.class)
53 public class TestIncrementalBackupDeleteTable extends TestBackupBase {
54 private static final Log LOG = LogFactory.getLog(TestIncrementalBackupDeleteTable.class);
55 @Test
56 public void TestIncBackupDeleteTable() throws Exception {
57
58 LOG.info("create full backup image for all tables");
59
60 List<TableName> tables = Lists.newArrayList(table1, table2);
61 HBaseAdmin admin = null;
62 Connection conn = ConnectionFactory.createConnection(conf1);
63 admin = (HBaseAdmin) conn.getAdmin();
64
65 BackupRequest request = new BackupRequest();
66 request.setBackupType(BackupType.FULL).setTableList(tables).setTargetRootDir(BACKUP_ROOT_DIR);
67 String backupIdFull = admin.getBackupAdmin().backupTables(request);
68
69 assertTrue(checkSucceeded(backupIdFull));
70
71
72 HTable t1 = (HTable) conn.getTable(table1);
73 Put p1;
74 for (int i = 0; i < NB_ROWS_IN_BATCH; i++) {
75 p1 = new Put(Bytes.toBytes("row-t1" + i));
76 p1.addColumn(famName, qualName, Bytes.toBytes("val" + i));
77 t1.put(p1);
78 }
79
80 Assert.assertThat(TEST_UTIL.countRows(t1), CoreMatchers.equalTo(NB_ROWS_IN_BATCH * 2));
81 t1.close();
82
83
84 admin.disableTable(table2);
85 admin.deleteTable(table2);
86
87
88 tables = Lists.newArrayList(table1);
89 request = new BackupRequest();
90 request.setBackupType(BackupType.INCREMENTAL).setTableList(tables)
91 .setTargetRootDir(BACKUP_ROOT_DIR);
92 String backupIdIncMultiple = admin.getBackupAdmin().backupTables(request);
93 assertTrue(checkSucceeded(backupIdIncMultiple));
94
95
96 TableName[] tablesRestoreFull =
97 new TableName[] { table1, table2};
98
99 TableName[] tablesMapFull =
100 new TableName[] { table1_restore, table2_restore };
101
102 BackupAdmin client = getBackupAdmin();
103 client.restore(createRestoreRequest(BACKUP_ROOT_DIR, backupIdFull, false,
104 tablesRestoreFull,
105 tablesMapFull, false));
106
107
108 HBaseAdmin hAdmin = TEST_UTIL.getHBaseAdmin();
109 assertTrue(hAdmin.tableExists(table1_restore));
110 assertTrue(hAdmin.tableExists(table2_restore));
111
112
113 hAdmin.close();
114
115
116 HTable hTable = (HTable) conn.getTable(table1_restore);
117 Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(NB_ROWS_IN_BATCH));
118 hTable.close();
119
120 hTable = (HTable) conn.getTable(table2_restore);
121 Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(NB_ROWS_IN_BATCH));
122 hTable.close();
123
124
125
126 TableName[] tablesRestoreIncMultiple =
127 new TableName[] { table1 };
128 TableName[] tablesMapIncMultiple =
129 new TableName[] { table1_restore };
130 client.restore(createRestoreRequest(BACKUP_ROOT_DIR, backupIdIncMultiple, false,
131 tablesRestoreIncMultiple, tablesMapIncMultiple, true));
132
133 hTable = (HTable) conn.getTable(table1_restore);
134 Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(NB_ROWS_IN_BATCH * 2));
135 hTable.close();
136 admin.close();
137 conn.close();
138 }
139
140 }