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  
23  import org.apache.hadoop.hbase.HBaseTestingUtility;
24  import org.apache.hadoop.hbase.LargeTests;
25  import org.apache.hadoop.hbase.MiniHBaseCluster;
26  import org.apache.hadoop.hbase.client.Get;
27  import org.apache.hadoop.hbase.client.HTable;
28  import org.apache.hadoop.hbase.client.Put;
29  import org.apache.hadoop.hbase.client.Result;
30  import org.apache.hadoop.hbase.util.Bytes;
31  import org.junit.AfterClass;
32  import org.junit.BeforeClass;
33  import org.junit.Test;
34  import org.junit.experimental.categories.Category;
35  
36  /**
37   * Basic test for the CopyTable M/R tool
38   */
39  @Category(LargeTests.class)
40  public class TestCopyTable {
41    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
42    private static MiniHBaseCluster cluster;
43  
44    @BeforeClass
45    public static void beforeClass() throws Exception {
46      cluster = TEST_UTIL.startMiniCluster(3);
47      TEST_UTIL.startMiniMapReduceCluster();
48    }
49  
50    @AfterClass
51    public static void afterClass() throws Exception {
52      TEST_UTIL.shutdownMiniMapReduceCluster();
53      TEST_UTIL.shutdownMiniCluster();
54    }
55  
56    /**
57     * Simple end-to-end test
58     * @throws Exception
59     */
60    @Test
61    public void testCopyTable() throws Exception {
62      final byte[] TABLENAME1 = Bytes.toBytes("testCopyTable1");
63      final byte[] TABLENAME2 = Bytes.toBytes("testCopyTable2");
64      final byte[] FAMILY = Bytes.toBytes("family");
65      final byte[] COLUMN1 = Bytes.toBytes("c1");
66  
67      HTable t1 = TEST_UTIL.createTable(TABLENAME1, FAMILY);
68      HTable t2 = TEST_UTIL.createTable(TABLENAME2, FAMILY);
69  
70      // put rows into the first table
71      for (int i = 0; i < 10; i++) {
72        Put p = new Put(Bytes.toBytes("row" + i));
73        p.add(FAMILY, COLUMN1, COLUMN1);
74        t1.put(p);
75      }
76  
77      CopyTable copy = new CopyTable(TEST_UTIL.getConfiguration());
78  
79      assertEquals(
80        0,
81        copy.run(new String[] { "--new.name=" + Bytes.toString(TABLENAME2),
82            Bytes.toString(TABLENAME1) }));
83  
84      // verify the data was copied into table 2
85      for (int i = 0; i < 10; i++) {
86        Get g = new Get(Bytes.toBytes("row" + i));
87        Result r = t2.get(g);
88        assertEquals(1, r.size());
89        assertTrue(Bytes.equals(COLUMN1, r.raw()[0].getQualifier()));
90      }
91  
92      t1.close();
93      t2.close();
94      TEST_UTIL.deleteTable(TABLENAME1);
95      TEST_UTIL.deleteTable(TABLENAME2);
96    }
97  
98    @Test
99    public void testStartStopRow() throws Exception {
100     final byte[] TABLENAME1 = Bytes.toBytes("testStartStopRow1");
101     final byte[] TABLENAME2 = Bytes.toBytes("testStartStopRow2");
102     final byte[] FAMILY = Bytes.toBytes("family");
103     final byte[] COLUMN1 = Bytes.toBytes("c1");
104     final byte[] ROW0 = Bytes.toBytes("row0");
105     final byte[] ROW1 = Bytes.toBytes("row1");
106     final byte[] ROW2 = Bytes.toBytes("row2");
107 
108     HTable t1 = TEST_UTIL.createTable(TABLENAME1, FAMILY);
109     HTable t2 = TEST_UTIL.createTable(TABLENAME2, FAMILY);
110 
111     // put rows into the first table
112     Put p = new Put(ROW0);
113     p.add(FAMILY, COLUMN1, COLUMN1);
114     t1.put(p);
115     p = new Put(ROW1);
116     p.add(FAMILY, COLUMN1, COLUMN1);
117     t1.put(p);
118     p = new Put(ROW2);
119     p.add(FAMILY, COLUMN1, COLUMN1);
120     t1.put(p);
121 
122     CopyTable copy = new CopyTable(TEST_UTIL.getConfiguration());
123     assertEquals(
124       0,
125       copy.run(new String[] { "--new.name=" + Bytes.toString(TABLENAME2), "--startrow=row1",
126           "--stoprow=row2", Bytes.toString(TABLENAME1) }));
127 
128     // verify the data was copied into table 2
129     // row1 exist, row0, row2 do not exist
130     Get g = new Get(ROW1);
131     Result r = t2.get(g);
132     assertEquals(1, r.size());
133     assertTrue(Bytes.equals(COLUMN1, r.raw()[0].getQualifier()));
134     
135     g = new Get(ROW0);
136     r = t2.get(g);
137     assertEquals(0, r.size());
138     
139     g = new Get(ROW2);
140     r = t2.get(g);
141     assertEquals(0, r.size());
142     
143     t1.close();
144     t2.close();
145     TEST_UTIL.deleteTable(TABLENAME1);
146     TEST_UTIL.deleteTable(TABLENAME2);
147   }
148 }