View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.rest.client;
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.hbase.*;
29  import org.apache.hadoop.hbase.client.Delete;
30  import org.apache.hadoop.hbase.client.Get;
31  import org.apache.hadoop.hbase.client.HBaseAdmin;
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.client.ResultScanner;
36  import org.apache.hadoop.hbase.client.Scan;
37  import org.apache.hadoop.hbase.rest.HBaseRESTTestingUtility;
38  import org.apache.hadoop.hbase.rest.client.Client;
39  import org.apache.hadoop.hbase.rest.client.Cluster;
40  import org.apache.hadoop.hbase.rest.client.RemoteHTable;
41  import org.apache.hadoop.hbase.util.Bytes;
42  
43  import static org.junit.Assert.*;
44  import org.junit.AfterClass;
45  import org.junit.BeforeClass;
46  import org.junit.Test;
47  import org.junit.experimental.categories.Category;
48  
49  @Category(MediumTests.class)
50  public class TestRemoteTable {
51    private static final Log LOG = LogFactory.getLog(TestRemoteTable.class);
52    private static final String TABLE = "TestRemoteTable";
53    private static final byte[] ROW_1 = Bytes.toBytes("testrow1");
54    private static final byte[] ROW_2 = Bytes.toBytes("testrow2");
55    private static final byte[] ROW_3 = Bytes.toBytes("testrow3");
56    private static final byte[] ROW_4 = Bytes.toBytes("testrow4");
57    private static final byte[] COLUMN_1 = Bytes.toBytes("a");
58    private static final byte[] COLUMN_2 = Bytes.toBytes("b");
59    private static final byte[] COLUMN_3 = Bytes.toBytes("c");
60    private static final byte[] QUALIFIER_1 = Bytes.toBytes("1");
61    private static final byte[] QUALIFIER_2 = Bytes.toBytes("2");
62    private static final byte[] VALUE_1 = Bytes.toBytes("testvalue1");
63    private static final byte[] VALUE_2 = Bytes.toBytes("testvalue2");
64  
65    private static final long ONE_HOUR = 60 * 60 * 1000;
66    private static final long TS_2 = System.currentTimeMillis();
67    private static final long TS_1 = TS_2 - ONE_HOUR;
68  
69    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
70    private static final HBaseRESTTestingUtility REST_TEST_UTIL = 
71      new HBaseRESTTestingUtility();
72    private static RemoteHTable remoteTable;
73  
74    @BeforeClass
75    public static void setUpBeforeClass() throws Exception {
76      TEST_UTIL.startMiniCluster();
77      REST_TEST_UTIL.startServletContainer(TEST_UTIL.getConfiguration());
78      HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
79      if (!admin.tableExists(TABLE)) {
80        HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(TABLE));
81        htd.addFamily(new HColumnDescriptor(COLUMN_1).setMaxVersions(3));
82        htd.addFamily(new HColumnDescriptor(COLUMN_2).setMaxVersions(3));
83        htd.addFamily(new HColumnDescriptor(COLUMN_3).setMaxVersions(3));
84        admin.createTable(htd);
85        HTable table = new HTable(TEST_UTIL.getConfiguration(), TABLE);
86        Put put = new Put(ROW_1);
87        put.add(COLUMN_1, QUALIFIER_1, TS_2, VALUE_1);
88        table.put(put);
89        put = new Put(ROW_2);
90        put.add(COLUMN_1, QUALIFIER_1, TS_1, VALUE_1);
91        put.add(COLUMN_1, QUALIFIER_1, TS_2, VALUE_2);
92        put.add(COLUMN_2, QUALIFIER_2, TS_2, VALUE_2);
93        table.put(put);
94        table.flushCommits();
95      }
96      remoteTable = new RemoteHTable(
97        new Client(new Cluster().add("localhost", 
98            REST_TEST_UTIL.getServletPort())),
99          TEST_UTIL.getConfiguration(), TABLE);
100   }
101 
102   @AfterClass
103   public static void tearDownAfterClass() throws Exception {
104     remoteTable.close();
105     REST_TEST_UTIL.shutdownServletContainer();
106     TEST_UTIL.shutdownMiniCluster();
107   }
108 
109   @Test
110   public void testGetTableDescriptor() throws IOException {
111     HTableDescriptor local = new HTable(TEST_UTIL.getConfiguration(),
112       TABLE).getTableDescriptor();
113     assertEquals(remoteTable.getTableDescriptor(), local);
114   }
115 
116   @Test
117   public void testGet() throws IOException {
118     Get get = new Get(ROW_1);
119     Result result = remoteTable.get(get);
120     byte[] value1 = result.getValue(COLUMN_1, QUALIFIER_1);
121     byte[] value2 = result.getValue(COLUMN_2, QUALIFIER_2);
122     assertNotNull(value1);
123     assertTrue(Bytes.equals(VALUE_1, value1));
124     assertNull(value2);
125 
126     get = new Get(ROW_1);
127     get.addFamily(COLUMN_3);
128     result = remoteTable.get(get);
129     value1 = result.getValue(COLUMN_1, QUALIFIER_1);
130     value2 = result.getValue(COLUMN_2, QUALIFIER_2);
131     assertNull(value1);
132     assertNull(value2);
133 
134     get = new Get(ROW_1);
135     get.addColumn(COLUMN_1, QUALIFIER_1);
136     get.addColumn(COLUMN_2, QUALIFIER_2);
137     result = remoteTable.get(get);
138     value1 = result.getValue(COLUMN_1, QUALIFIER_1);
139     value2 = result.getValue(COLUMN_2, QUALIFIER_2);
140     assertNotNull(value1);
141     assertTrue(Bytes.equals(VALUE_1, value1));
142     assertNull(value2);
143 
144     get = new Get(ROW_2);
145     result = remoteTable.get(get);    
146     value1 = result.getValue(COLUMN_1, QUALIFIER_1);
147     value2 = result.getValue(COLUMN_2, QUALIFIER_2);
148     assertNotNull(value1);
149     assertTrue(Bytes.equals(VALUE_2, value1)); // @TS_2
150     assertNotNull(value2);
151     assertTrue(Bytes.equals(VALUE_2, value2));
152 
153     get = new Get(ROW_2);
154     get.addFamily(COLUMN_1);
155     result = remoteTable.get(get);    
156     value1 = result.getValue(COLUMN_1, QUALIFIER_1);
157     value2 = result.getValue(COLUMN_2, QUALIFIER_2);
158     assertNotNull(value1);
159     assertTrue(Bytes.equals(VALUE_2, value1)); // @TS_2
160     assertNull(value2);
161 
162     get = new Get(ROW_2);
163     get.addColumn(COLUMN_1, QUALIFIER_1);
164     get.addColumn(COLUMN_2, QUALIFIER_2);
165     result = remoteTable.get(get);    
166     value1 = result.getValue(COLUMN_1, QUALIFIER_1);
167     value2 = result.getValue(COLUMN_2, QUALIFIER_2);
168     assertNotNull(value1);
169     assertTrue(Bytes.equals(VALUE_2, value1)); // @TS_2
170     assertNotNull(value2);
171     assertTrue(Bytes.equals(VALUE_2, value2));
172 
173     // test timestamp
174 
175     get = new Get(ROW_2);
176     get.addFamily(COLUMN_1);
177     get.addFamily(COLUMN_2);
178     get.setTimeStamp(TS_1);
179     result = remoteTable.get(get);    
180     value1 = result.getValue(COLUMN_1, QUALIFIER_1);
181     value2 = result.getValue(COLUMN_2, QUALIFIER_2);
182     assertNotNull(value1);
183     assertTrue(Bytes.equals(VALUE_1, value1)); // @TS_1
184     assertNull(value2);
185 
186     // test timerange
187 
188     get = new Get(ROW_2);
189     get.addFamily(COLUMN_1);
190     get.addFamily(COLUMN_2);
191     get.setTimeRange(0, TS_1 + 1);
192     result = remoteTable.get(get);    
193     value1 = result.getValue(COLUMN_1, QUALIFIER_1);
194     value2 = result.getValue(COLUMN_2, QUALIFIER_2);
195     assertNotNull(value1);
196     assertTrue(Bytes.equals(VALUE_1, value1)); // @TS_1
197     assertNull(value2);
198 
199     // test maxVersions
200 
201     get = new Get(ROW_2);
202     get.addFamily(COLUMN_1);
203     get.setMaxVersions(2);
204     result = remoteTable.get(get);
205     int count = 0;
206     for (KeyValue kv: result.list()) {
207       if (Bytes.equals(COLUMN_1, kv.getFamily()) && TS_1 == kv.getTimestamp()) {
208         assertTrue(Bytes.equals(VALUE_1, kv.getValue())); // @TS_1
209         count++;
210       }
211       if (Bytes.equals(COLUMN_1, kv.getFamily()) && TS_2 == kv.getTimestamp()) {
212         assertTrue(Bytes.equals(VALUE_2, kv.getValue())); // @TS_2
213         count++;
214       }
215     }
216     assertEquals(2, count);
217   }
218 
219   @Test
220   public void testMultiGet() throws Exception {
221     ArrayList<Get> gets = new ArrayList<Get>();
222     gets.add(new Get(ROW_1));
223     gets.add(new Get(ROW_2));
224     Result[] results = remoteTable.get(gets);
225     assertNotNull(results);
226     assertEquals(2, results.length);
227     assertEquals(1, results[0].size());
228     assertEquals(2, results[1].size());
229 
230     //Test Versions
231     gets = new ArrayList<Get>();
232     Get g = new Get(ROW_1);
233     g.setMaxVersions(3);
234     gets.add(g);
235     gets.add(new Get(ROW_2));
236     results = remoteTable.get(gets);
237     assertNotNull(results);
238     assertEquals(2, results.length);
239     assertEquals(1, results[0].size());
240     assertEquals(3, results[1].size());
241 
242     //404
243     gets = new ArrayList<Get>();
244     gets.add(new Get(Bytes.toBytes("RESALLYREALLYNOTTHERE")));
245     results = remoteTable.get(gets);
246     assertNotNull(results);
247     assertEquals(0, results.length);
248 
249     gets = new ArrayList<Get>();
250     gets.add(new Get(Bytes.toBytes("RESALLYREALLYNOTTHERE")));
251     gets.add(new Get(ROW_1));
252     gets.add(new Get(ROW_2));
253     results = remoteTable.get(gets);
254     assertNotNull(results);
255     assertEquals(0, results.length);
256   }
257 
258   @Test
259   public void testPut() throws IOException {
260     Put put = new Put(ROW_3);
261     put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
262     remoteTable.put(put);
263 
264     Get get = new Get(ROW_3);
265     get.addFamily(COLUMN_1);
266     Result result = remoteTable.get(get);
267     byte[] value = result.getValue(COLUMN_1, QUALIFIER_1);
268     assertNotNull(value);
269     assertTrue(Bytes.equals(VALUE_1, value));
270 
271     // multiput
272 
273     List<Put> puts = new ArrayList<Put>();
274     put = new Put(ROW_3);
275     put.add(COLUMN_2, QUALIFIER_2, VALUE_2);
276     puts.add(put);
277     put = new Put(ROW_4);
278     put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
279     puts.add(put);
280     put = new Put(ROW_4);
281     put.add(COLUMN_2, QUALIFIER_2, VALUE_2);
282     puts.add(put);
283     remoteTable.put(puts);
284 
285     get = new Get(ROW_3);
286     get.addFamily(COLUMN_2);
287     result = remoteTable.get(get);
288     value = result.getValue(COLUMN_2, QUALIFIER_2);
289     assertNotNull(value);
290     assertTrue(Bytes.equals(VALUE_2, value));
291     get = new Get(ROW_4);
292     result = remoteTable.get(get);
293     value = result.getValue(COLUMN_1, QUALIFIER_1);
294     assertNotNull(value);
295     assertTrue(Bytes.equals(VALUE_1, value));
296     value = result.getValue(COLUMN_2, QUALIFIER_2);
297     assertNotNull(value);
298     assertTrue(Bytes.equals(VALUE_2, value));
299   }
300 
301   @Test
302   public void testDelete() throws IOException {
303     Put put = new Put(ROW_3);
304     put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
305     put.add(COLUMN_2, QUALIFIER_2, VALUE_2);
306     remoteTable.put(put);
307 
308     Get get = new Get(ROW_3);
309     get.addFamily(COLUMN_1);
310     get.addFamily(COLUMN_2);
311     Result result = remoteTable.get(get);
312     byte[] value1 = result.getValue(COLUMN_1, QUALIFIER_1);
313     byte[] value2 = result.getValue(COLUMN_2, QUALIFIER_2);
314     assertNotNull(value1);
315     assertTrue(Bytes.equals(VALUE_1, value1));
316     assertNotNull(value2);
317     assertTrue(Bytes.equals(VALUE_2, value2));
318 
319     Delete delete = new Delete(ROW_3);
320     delete.deleteColumn(COLUMN_2, QUALIFIER_2);
321     remoteTable.delete(delete);
322     
323     get = new Get(ROW_3);
324     get.addFamily(COLUMN_1);
325     get.addFamily(COLUMN_2);
326     result = remoteTable.get(get);
327     value1 = result.getValue(COLUMN_1, QUALIFIER_1);
328     value2 = result.getValue(COLUMN_2, QUALIFIER_2);
329     assertNotNull(value1);
330     assertTrue(Bytes.equals(VALUE_1, value1));
331     assertNull(value2);
332 
333     delete = new Delete(ROW_3);
334     delete.setTimestamp(1L);
335     remoteTable.delete(delete);
336 
337     get = new Get(ROW_3);
338     get.addFamily(COLUMN_1);
339     get.addFamily(COLUMN_2);
340     result = remoteTable.get(get);
341     value1 = result.getValue(COLUMN_1, QUALIFIER_1);
342     value2 = result.getValue(COLUMN_2, QUALIFIER_2);
343     assertNotNull(value1);
344     assertTrue(Bytes.equals(VALUE_1, value1));
345     assertNull(value2);
346     
347     delete = new Delete(ROW_3);
348     remoteTable.delete(delete);
349 
350     get = new Get(ROW_3);
351     get.addFamily(COLUMN_1);
352     get.addFamily(COLUMN_2);
353     result = remoteTable.get(get);
354     value1 = result.getValue(COLUMN_1, QUALIFIER_1);
355     value2 = result.getValue(COLUMN_2, QUALIFIER_2);
356     assertNull(value1);
357     assertNull(value2);            
358   }
359 
360   public void testScanner() throws IOException {
361     List<Put> puts = new ArrayList<Put>();
362     Put put = new Put(ROW_1);
363     put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
364     puts.add(put);
365     put = new Put(ROW_2);
366     put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
367     puts.add(put);
368     put = new Put(ROW_3);
369     put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
370     puts.add(put);
371     put = new Put(ROW_4);
372     put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
373     puts.add(put);
374     remoteTable.put(puts);
375 
376     ResultScanner scanner = remoteTable.getScanner(new Scan());
377 
378     Result[] results = scanner.next(1);
379     assertNotNull(results);
380     assertEquals(1, results.length);
381     assertTrue(Bytes.equals(ROW_1, results[0].getRow()));
382 
383     results = scanner.next(3);
384     assertNotNull(results);
385     assertEquals(3, results.length);
386     assertTrue(Bytes.equals(ROW_2, results[0].getRow()));
387     assertTrue(Bytes.equals(ROW_3, results[1].getRow()));
388     assertTrue(Bytes.equals(ROW_4, results[2].getRow()));
389 
390     results = scanner.next(1);
391     assertNull(results);
392 
393     scanner.close();
394   }
395 
396 }
397