1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
59
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
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
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
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
130
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 }