1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.mapreduce;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  import static org.junit.Assert.assertNull;
23  
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.fs.Path;
26  import org.apache.hadoop.hbase.HBaseTestingUtility;
27  import org.apache.hadoop.hbase.HConstants;
28  import org.apache.hadoop.hbase.LargeTests;
29  import org.apache.hadoop.hbase.MiniHBaseCluster;
30  import org.apache.hadoop.hbase.client.Delete;
31  import org.apache.hadoop.hbase.client.Get;
32  import org.apache.hadoop.hbase.client.HTable;
33  import org.apache.hadoop.hbase.client.Put;
34  import org.apache.hadoop.hbase.client.Result;
35  import org.apache.hadoop.hbase.regionserver.wal.HLog;
36  import org.apache.hadoop.hbase.util.Bytes;
37  import org.junit.AfterClass;
38  import org.junit.BeforeClass;
39  import org.junit.Test;
40  import org.junit.experimental.categories.Category;
41  
42  /**
43   * Basic test for the WALPlayer M/R tool
44   */
45  @Category(LargeTests.class)
46  public class TestWALPlayer {
47    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
48    private static MiniHBaseCluster cluster;
49  
50    @BeforeClass
51    public static void beforeClass() throws Exception {
52      cluster = TEST_UTIL.startMiniCluster();
53      TEST_UTIL.startMiniMapReduceCluster();
54    }
55  
56    @AfterClass
57    public static void afterClass() throws Exception {
58      TEST_UTIL.shutdownMiniMapReduceCluster();
59      TEST_UTIL.shutdownMiniCluster();
60    }
61  
62    /**
63     * Simple end-to-end test
64     * @throws Exception
65     */
66    @Test
67    public void testWALPlayer() throws Exception {
68      final byte[] TABLENAME1 = Bytes.toBytes("testWALPlayer1");
69      final byte[] TABLENAME2 = Bytes.toBytes("testWALPlayer2");
70      final byte[] FAMILY = Bytes.toBytes("family");
71      final byte[] COLUMN1 = Bytes.toBytes("c1");
72      final byte[] COLUMN2 = Bytes.toBytes("c2");
73      final byte[] ROW = Bytes.toBytes("row");
74      HTable t1 = TEST_UTIL.createTable(TABLENAME1, FAMILY);
75      HTable t2 = TEST_UTIL.createTable(TABLENAME2, FAMILY);
76  
77      // put a row into the first table
78      Put p = new Put(ROW);
79      p.add(FAMILY, COLUMN1, COLUMN1);
80      p.add(FAMILY, COLUMN2, COLUMN2);
81      t1.put(p);
82      // delete one column
83      Delete d = new Delete(ROW);
84      d.deleteColumns(FAMILY, COLUMN1);
85      t1.delete(d);
86  
87      // replay the WAL, map table 1 to table 2
88      HLog log = cluster.getRegionServer(0).getWAL();
89      log.rollWriter();
90      String walInputDir = new Path(cluster.getMaster().getMasterFileSystem()
91          .getRootDir(), HConstants.HREGION_LOGDIR_NAME).toString();
92  
93      WALPlayer player = new WALPlayer(TEST_UTIL.getConfiguration());
94      assertEquals(0, player.run(new String[] { walInputDir, Bytes.toString(TABLENAME1),
95          Bytes.toString(TABLENAME2) }));
96  
97      // verify the WAL was player into table 2
98      Get g = new Get(ROW);
99      Result r = t2.get(g);
100     assertEquals(1, r.size());
101     assertTrue(Bytes.equals(COLUMN2, r.raw()[0].getQualifier()));
102   }
103 }