View Javadoc

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