View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional information regarding
4    * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
8    * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
9    * for the specific language governing permissions and limitations under the License.
10   */
11  
12  package org.apache.hadoop.hbase.backup;
13  
14  import static org.junit.Assert.assertEquals;
15  import static org.junit.Assert.assertTrue;
16  
17  import java.util.List;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.apache.hadoop.hbase.TableName;
22  import org.apache.hadoop.hbase.client.BackupAdmin;
23  import org.apache.hadoop.hbase.client.Delete;
24  import org.apache.hadoop.hbase.client.HBaseAdmin;
25  import org.apache.hadoop.hbase.client.Table;
26  import org.apache.hadoop.hbase.testclassification.MediumTests;
27  import org.junit.Test;
28  import org.junit.experimental.categories.Category;
29  
30  import com.google.common.collect.Lists;
31  
32  @Category(MediumTests.class)
33  public class TestBackupDeleteRestore extends TestBackupBase {
34  
35    private static final Log LOG = LogFactory.getLog(TestBackupDeleteRestore.class);
36  
37    /**
38     * Verify that load data- backup - delete some data - restore
39     * works as expected - deleted data get restored.
40     * @throws Exception
41     */
42    @Test
43    public void testBackupDeleteRestore() throws Exception {
44  
45      LOG.info("test full restore on a single table empty table");
46  
47      List<TableName> tables = Lists.newArrayList(table1);
48      String backupId = fullTableBackup(tables);
49      assertTrue(checkSucceeded(backupId));
50      LOG.info("backup complete");
51      int numRows = TEST_UTIL.countRows(table1);
52      HBaseAdmin hba = TEST_UTIL.getHBaseAdmin();
53      // delete row    
54      try ( Table table = TEST_UTIL.getConnection().getTable(table1);) {      
55        Delete delete = new Delete("row0".getBytes());
56        table.delete(delete);
57        hba.flush(table1);
58      }   
59      int numRowsAfterDelete = TEST_UTIL.countRows(table1);
60      assertTrue(numRowsAfterDelete == numRows -1);
61      
62      TableName[] tableset = new TableName[] { table1 };
63      TableName[] tablemap = null;//new TableName[] { table1_restore };
64      BackupAdmin client = getBackupAdmin();
65      client.restore(createRestoreRequest(BACKUP_ROOT_DIR, backupId, true, tableset, tablemap, true));
66          
67      int numRowsAfterRestore = TEST_UTIL.countRows(table1);
68      assertEquals( numRows, numRowsAfterRestore);    
69      hba.close();
70    }    
71  }