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 static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertNotNull;
25  import static org.junit.Assert.assertNull;
26  import static org.junit.Assert.assertTrue;
27  
28  import java.io.IOException;
29  import java.util.ArrayList;
30  import java.util.Collections;
31  import java.util.Iterator;
32  import java.util.List;
33  
34  import org.apache.commons.httpclient.Header;
35  import org.apache.hadoop.hbase.Cell;
36  import org.apache.hadoop.hbase.CellUtil;
37  import org.apache.hadoop.hbase.HBaseTestingUtility;
38  import org.apache.hadoop.hbase.HColumnDescriptor;
39  import org.apache.hadoop.hbase.HTableDescriptor;
40  import org.apache.hadoop.hbase.client.HTable;
41  import org.apache.hadoop.hbase.testclassification.MediumTests;
42  import org.apache.hadoop.hbase.TableName;
43  import org.apache.hadoop.hbase.client.Admin;
44  import org.apache.hadoop.hbase.client.Delete;
45  import org.apache.hadoop.hbase.client.Get;
46  import org.apache.hadoop.hbase.client.Put;
47  import org.apache.hadoop.hbase.client.Result;
48  import org.apache.hadoop.hbase.client.ResultScanner;
49  import org.apache.hadoop.hbase.client.Scan;
50  import org.apache.hadoop.hbase.client.Table;
51  import org.apache.hadoop.hbase.rest.HBaseRESTTestingUtility;
52  import org.apache.hadoop.hbase.util.Bytes;
53  import org.junit.After;
54  import org.junit.AfterClass;
55  import org.junit.Before;
56  import org.junit.BeforeClass;
57  import org.junit.Test;
58  import org.junit.experimental.categories.Category;
59  
60  @Category(MediumTests.class)
61  public class TestRemoteTable {
62    private static final TableName TABLE = TableName.valueOf("TestRemoteTable");
63    private static final byte[] ROW_1 = Bytes.toBytes("testrow1");
64    private static final byte[] ROW_2 = Bytes.toBytes("testrow2");
65    private static final byte[] ROW_3 = Bytes.toBytes("testrow3");
66    private static final byte[] ROW_4 = Bytes.toBytes("testrow4");
67    private static final byte[] COLUMN_1 = Bytes.toBytes("a");
68    private static final byte[] COLUMN_2 = Bytes.toBytes("b");
69    private static final byte[] COLUMN_3 = Bytes.toBytes("c");
70    private static final byte[] QUALIFIER_1 = Bytes.toBytes("1");
71    private static final byte[] QUALIFIER_2 = Bytes.toBytes("2");
72    private static final byte[] VALUE_1 = Bytes.toBytes("testvalue1");
73    private static final byte[] VALUE_2 = Bytes.toBytes("testvalue2");
74  
75    private static final long ONE_HOUR = 60 * 60 * 1000;
76    private static final long TS_2 = System.currentTimeMillis();
77    private static final long TS_1 = TS_2 - ONE_HOUR;
78  
79    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
80    private static final HBaseRESTTestingUtility REST_TEST_UTIL = 
81      new HBaseRESTTestingUtility();
82    private RemoteHTable remoteTable;
83  
84    @BeforeClass
85    public static void setUpBeforeClass() throws Exception {
86      TEST_UTIL.startMiniCluster();
87      REST_TEST_UTIL.startServletContainer(TEST_UTIL.getConfiguration());
88    }
89  
90    @Before
91    public void before() throws Exception  {
92      Admin admin = TEST_UTIL.getHBaseAdmin();
93      if (admin.tableExists(TABLE)) {
94        if (admin.isTableEnabled(TABLE)) admin.disableTable(TABLE);
95        admin.deleteTable(TABLE);
96      }
97      HTableDescriptor htd = new HTableDescriptor(TABLE);
98      htd.addFamily(new HColumnDescriptor(COLUMN_1).setMaxVersions(3));
99      htd.addFamily(new HColumnDescriptor(COLUMN_2).setMaxVersions(3));
100     htd.addFamily(new HColumnDescriptor(COLUMN_3).setMaxVersions(3));
101     admin.createTable(htd);
102     try (Table table = TEST_UTIL.getConnection().getTable(TABLE)) {
103       Put put = new Put(ROW_1);
104       put.add(COLUMN_1, QUALIFIER_1, TS_2, VALUE_1);
105       table.put(put);
106       put = new Put(ROW_2);
107       put.add(COLUMN_1, QUALIFIER_1, TS_1, VALUE_1);
108       put.add(COLUMN_1, QUALIFIER_1, TS_2, VALUE_2);
109       put.add(COLUMN_2, QUALIFIER_2, TS_2, VALUE_2);
110       table.put(put);
111     }
112     remoteTable = new RemoteHTable(
113       new Client(new Cluster().add("localhost", 
114           REST_TEST_UTIL.getServletPort())),
115         TEST_UTIL.getConfiguration(), TABLE.toBytes());
116   }
117   
118   @After
119   public void after() throws Exception {
120     remoteTable.close();
121   }
122   
123   @AfterClass
124   public static void tearDownAfterClass() throws Exception {
125     REST_TEST_UTIL.shutdownServletContainer();
126     TEST_UTIL.shutdownMiniCluster();
127   }
128 
129   @Test
130   public void testGetTableDescriptor() throws IOException {
131     Table table = null;
132     try {
133       table = new HTable(TEST_UTIL.getConfiguration(), TABLE);
134       HTableDescriptor local = table.getTableDescriptor();
135       assertEquals(remoteTable.getTableDescriptor(), local);
136     } finally {
137       if (null != table) table.close();
138     }
139   }
140 
141   @Test
142   public void testGet() throws IOException {
143     Get get = new Get(ROW_1);
144     Result result = remoteTable.get(get);
145     byte[] value1 = result.getValue(COLUMN_1, QUALIFIER_1);
146     byte[] value2 = result.getValue(COLUMN_2, QUALIFIER_2);
147     assertNotNull(value1);
148     assertTrue(Bytes.equals(VALUE_1, value1));
149     assertNull(value2);
150 
151     get = new Get(ROW_1);
152     get.addFamily(COLUMN_3);
153     result = remoteTable.get(get);
154     value1 = result.getValue(COLUMN_1, QUALIFIER_1);
155     value2 = result.getValue(COLUMN_2, QUALIFIER_2);
156     assertNull(value1);
157     assertNull(value2);
158 
159     get = new Get(ROW_1);
160     get.addColumn(COLUMN_1, QUALIFIER_1);
161     get.addColumn(COLUMN_2, QUALIFIER_2);
162     result = remoteTable.get(get);
163     value1 = result.getValue(COLUMN_1, QUALIFIER_1);
164     value2 = result.getValue(COLUMN_2, QUALIFIER_2);
165     assertNotNull(value1);
166     assertTrue(Bytes.equals(VALUE_1, value1));
167     assertNull(value2);
168 
169     get = new Get(ROW_2);
170     result = remoteTable.get(get);
171     value1 = result.getValue(COLUMN_1, QUALIFIER_1);
172     value2 = result.getValue(COLUMN_2, QUALIFIER_2);
173     assertNotNull(value1);
174     assertTrue(Bytes.equals(VALUE_2, value1)); // @TS_2
175     assertNotNull(value2);
176     assertTrue(Bytes.equals(VALUE_2, value2));
177 
178     get = new Get(ROW_2);
179     get.addFamily(COLUMN_1);
180     result = remoteTable.get(get);
181     value1 = result.getValue(COLUMN_1, QUALIFIER_1);
182     value2 = result.getValue(COLUMN_2, QUALIFIER_2);
183     assertNotNull(value1);
184     assertTrue(Bytes.equals(VALUE_2, value1)); // @TS_2
185     assertNull(value2);
186 
187     get = new Get(ROW_2);
188     get.addColumn(COLUMN_1, QUALIFIER_1);
189     get.addColumn(COLUMN_2, QUALIFIER_2);
190     result = remoteTable.get(get);
191     value1 = result.getValue(COLUMN_1, QUALIFIER_1);
192     value2 = result.getValue(COLUMN_2, QUALIFIER_2);
193     assertNotNull(value1);
194     assertTrue(Bytes.equals(VALUE_2, value1)); // @TS_2
195     assertNotNull(value2);
196     assertTrue(Bytes.equals(VALUE_2, value2));
197 
198     // test timestamp
199 
200     get = new Get(ROW_2);
201     get.addFamily(COLUMN_1);
202     get.addFamily(COLUMN_2);
203     get.setTimeStamp(TS_1);
204     result = remoteTable.get(get);
205     value1 = result.getValue(COLUMN_1, QUALIFIER_1);
206     value2 = result.getValue(COLUMN_2, QUALIFIER_2);
207     assertNotNull(value1);
208     assertTrue(Bytes.equals(VALUE_1, value1)); // @TS_1
209     assertNull(value2);
210 
211     // test timerange
212 
213     get = new Get(ROW_2);
214     get.addFamily(COLUMN_1);
215     get.addFamily(COLUMN_2);
216     get.setTimeRange(0, TS_1 + 1);
217     result = remoteTable.get(get);
218     value1 = result.getValue(COLUMN_1, QUALIFIER_1);
219     value2 = result.getValue(COLUMN_2, QUALIFIER_2);
220     assertNotNull(value1);
221     assertTrue(Bytes.equals(VALUE_1, value1)); // @TS_1
222     assertNull(value2);
223 
224     // test maxVersions
225 
226     get = new Get(ROW_2);
227     get.addFamily(COLUMN_1);
228     get.setMaxVersions(2);
229     result = remoteTable.get(get);
230     int count = 0;
231     for (Cell kv: result.listCells()) {
232       if (CellUtil.matchingFamily(kv, COLUMN_1) && TS_1 == kv.getTimestamp()) {
233         assertTrue(CellUtil.matchingValue(kv, VALUE_1)); // @TS_1
234         count++;
235       }
236       if (CellUtil.matchingFamily(kv, COLUMN_1) && TS_2 == kv.getTimestamp()) {
237         assertTrue(CellUtil.matchingValue(kv, VALUE_2)); // @TS_2
238         count++;
239       }
240     }
241     assertEquals(2, count);
242   }
243 
244   @Test
245   public void testMultiGet() throws Exception {
246     ArrayList<Get> gets = new ArrayList<Get>();
247     gets.add(new Get(ROW_1));
248     gets.add(new Get(ROW_2));
249     Result[] results = remoteTable.get(gets);
250     assertNotNull(results);
251     assertEquals(2, results.length);
252     assertEquals(1, results[0].size());
253     assertEquals(2, results[1].size());
254 
255     //Test Versions
256     gets = new ArrayList<Get>();
257     Get g = new Get(ROW_1);
258     g.setMaxVersions(3);
259     gets.add(g);
260     gets.add(new Get(ROW_2));
261     results = remoteTable.get(gets);
262     assertNotNull(results);
263     assertEquals(2, results.length);
264     assertEquals(1, results[0].size());
265     assertEquals(3, results[1].size());
266 
267     //404
268     gets = new ArrayList<Get>();
269     gets.add(new Get(Bytes.toBytes("RESALLYREALLYNOTTHERE")));
270     results = remoteTable.get(gets);
271     assertNotNull(results);
272     assertEquals(0, results.length);
273 
274     gets = new ArrayList<Get>();
275     gets.add(new Get(Bytes.toBytes("RESALLYREALLYNOTTHERE")));
276     gets.add(new Get(ROW_1));
277     gets.add(new Get(ROW_2));
278     results = remoteTable.get(gets);
279     assertNotNull(results);
280     assertEquals(2, results.length);
281   }
282 
283   @Test
284   public void testPut() throws IOException {
285     Put put = new Put(ROW_3);
286     put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
287     remoteTable.put(put);
288 
289     Get get = new Get(ROW_3);
290     get.addFamily(COLUMN_1);
291     Result result = remoteTable.get(get);
292     byte[] value = result.getValue(COLUMN_1, QUALIFIER_1);
293     assertNotNull(value);
294     assertTrue(Bytes.equals(VALUE_1, value));
295 
296     // multiput
297 
298     List<Put> puts = new ArrayList<Put>();
299     put = new Put(ROW_3);
300     put.add(COLUMN_2, QUALIFIER_2, VALUE_2);
301     puts.add(put);
302     put = new Put(ROW_4);
303     put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
304     puts.add(put);
305     put = new Put(ROW_4);
306     put.add(COLUMN_2, QUALIFIER_2, VALUE_2);
307     puts.add(put);
308     remoteTable.put(puts);
309 
310     get = new Get(ROW_3);
311     get.addFamily(COLUMN_2);
312     result = remoteTable.get(get);
313     value = result.getValue(COLUMN_2, QUALIFIER_2);
314     assertNotNull(value);
315     assertTrue(Bytes.equals(VALUE_2, value));
316     get = new Get(ROW_4);
317     result = remoteTable.get(get);
318     value = result.getValue(COLUMN_1, QUALIFIER_1);
319     assertNotNull(value);
320     assertTrue(Bytes.equals(VALUE_1, value));
321     value = result.getValue(COLUMN_2, QUALIFIER_2);
322     assertNotNull(value);
323     assertTrue(Bytes.equals(VALUE_2, value));
324     
325     assertTrue(Bytes.equals(Bytes.toBytes("TestRemoteTable"), remoteTable.getTableName()));
326   }
327 
328   @Test
329   public void testDelete() throws IOException {
330     Put put = new Put(ROW_3);
331     put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
332     put.add(COLUMN_2, QUALIFIER_2, VALUE_2);
333     remoteTable.put(put);
334 
335     Get get = new Get(ROW_3);
336     get.addFamily(COLUMN_1);
337     get.addFamily(COLUMN_2);
338     Result result = remoteTable.get(get);
339     byte[] value1 = result.getValue(COLUMN_1, QUALIFIER_1);
340     byte[] value2 = result.getValue(COLUMN_2, QUALIFIER_2);
341     assertNotNull(value1);
342     assertTrue(Bytes.equals(VALUE_1, value1));
343     assertNotNull(value2);
344     assertTrue(Bytes.equals(VALUE_2, value2));
345 
346     Delete delete = new Delete(ROW_3);
347     delete.addColumn(COLUMN_2, QUALIFIER_2);
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     assertNotNull(value1);
357     assertTrue(Bytes.equals(VALUE_1, value1));
358     assertNull(value2);
359 
360     delete = new Delete(ROW_3);
361     delete.setTimestamp(1L);
362     remoteTable.delete(delete);
363 
364     get = new Get(ROW_3);
365     get.addFamily(COLUMN_1);
366     get.addFamily(COLUMN_2);
367     result = remoteTable.get(get);
368     value1 = result.getValue(COLUMN_1, QUALIFIER_1);
369     value2 = result.getValue(COLUMN_2, QUALIFIER_2);
370     assertNotNull(value1);
371     assertTrue(Bytes.equals(VALUE_1, value1));
372     assertNull(value2);
373 
374     delete = new Delete(ROW_3);
375     remoteTable.delete(delete);
376 
377     get = new Get(ROW_3);
378     get.addFamily(COLUMN_1);
379     get.addFamily(COLUMN_2);
380     result = remoteTable.get(get);
381     value1 = result.getValue(COLUMN_1, QUALIFIER_1);
382     value2 = result.getValue(COLUMN_2, QUALIFIER_2);
383     assertNull(value1);
384     assertNull(value2);
385   }
386   
387   /**
388    * Test RemoteHTable.Scanner 
389    */
390   @Test
391   public void testScanner() throws IOException {
392     List<Put> puts = new ArrayList<Put>();
393     Put put = new Put(ROW_1);
394     put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
395     puts.add(put);
396     put = new Put(ROW_2);
397     put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
398     puts.add(put);
399     put = new Put(ROW_3);
400     put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
401     puts.add(put);
402     put = new Put(ROW_4);
403     put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
404     puts.add(put);
405     remoteTable.put(puts);
406 
407     ResultScanner scanner = remoteTable.getScanner(new Scan());
408 
409     Result[] results = scanner.next(1);
410     assertNotNull(results);
411     assertEquals(1, results.length);
412     assertTrue(Bytes.equals(ROW_1, results[0].getRow()));
413 
414     Result result = scanner.next();
415     assertNotNull(result);
416     assertTrue(Bytes.equals(ROW_2, result.getRow()));
417 
418     results = scanner.next(2);
419     assertNotNull(results);
420     assertEquals(2, results.length);
421     assertTrue(Bytes.equals(ROW_3, results[0].getRow()));
422     assertTrue(Bytes.equals(ROW_4, results[1].getRow()));
423 
424     results = scanner.next(1);
425     assertNull(results);
426     scanner.close();
427     
428     scanner = remoteTable.getScanner(COLUMN_1);
429     results = scanner.next(4);
430     assertNotNull(results);
431     assertEquals(4, results.length);
432     assertTrue(Bytes.equals(ROW_1, results[0].getRow()));
433     assertTrue(Bytes.equals(ROW_2, results[1].getRow()));
434     assertTrue(Bytes.equals(ROW_3, results[2].getRow()));
435     assertTrue(Bytes.equals(ROW_4, results[3].getRow()));
436 
437     scanner.close();
438     
439     scanner = remoteTable.getScanner(COLUMN_1,QUALIFIER_1);
440     results = scanner.next(4);
441     assertNotNull(results);
442     assertEquals(4, results.length);
443     assertTrue(Bytes.equals(ROW_1, results[0].getRow()));
444     assertTrue(Bytes.equals(ROW_2, results[1].getRow()));
445     assertTrue(Bytes.equals(ROW_3, results[2].getRow()));
446     assertTrue(Bytes.equals(ROW_4, results[3].getRow()));
447     scanner.close();
448     assertTrue(remoteTable.isAutoFlush());
449 
450   }
451   
452   @Test
453   public void testCheckAndDelete() throws IOException {
454     Get get = new Get(ROW_1);
455     Result result = remoteTable.get(get);
456     byte[] value1 = result.getValue(COLUMN_1, QUALIFIER_1);
457     byte[] value2 = result.getValue(COLUMN_2, QUALIFIER_2);
458     assertNotNull(value1);
459     assertTrue(Bytes.equals(VALUE_1, value1));
460     assertNull(value2);
461     assertTrue(remoteTable.exists(get));
462     assertEquals(1, remoteTable.existsAll(Collections.singletonList(get)).length);
463     Delete delete = new Delete(ROW_1);
464 
465     remoteTable.checkAndDelete(ROW_1, COLUMN_1, QUALIFIER_1, VALUE_1, delete);
466     assertFalse(remoteTable.exists(get));
467 
468     Put put = new Put(ROW_1);
469     put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
470     remoteTable.put(put);
471 
472     assertTrue(remoteTable.checkAndPut(ROW_1, COLUMN_1, QUALIFIER_1, VALUE_1,
473         put));
474     assertFalse(remoteTable.checkAndPut(ROW_1, COLUMN_1, QUALIFIER_1, VALUE_2,
475         put));
476   }
477   
478   /**
479    * Test RemoteHable.Scanner.iterator method  
480    */
481   @Test
482   public void testIteratorScaner() throws IOException {
483     List<Put> puts = new ArrayList<Put>();
484     Put put = new Put(ROW_1);
485     put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
486     puts.add(put);
487     put = new Put(ROW_2);
488     put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
489     puts.add(put);
490     put = new Put(ROW_3);
491     put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
492     puts.add(put);
493     put = new Put(ROW_4);
494     put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
495     puts.add(put);
496     remoteTable.put(puts);
497 
498     ResultScanner scanner = remoteTable.getScanner(new Scan());
499     Iterator<Result> iterator = scanner.iterator();
500     assertTrue(iterator.hasNext());
501     int counter = 0;
502     while (iterator.hasNext()) {
503       iterator.next();
504       counter++;
505     }
506     assertEquals(4, counter);
507   }
508   
509   /**
510    * Test a some methods of class Response.
511    */
512   @Test
513   public void testResponse(){
514     Response response = new Response(200);
515     assertEquals(200, response.getCode());
516     Header[] headers = new Header[2];
517     headers[0] = new Header("header1", "value1");
518     headers[1] = new Header("header2", "value2");
519     response = new Response(200, headers);
520     assertEquals("value1", response.getHeader("header1"));
521     assertFalse(response.hasBody());
522     response.setCode(404);
523     assertEquals(404, response.getCode());
524     headers = new Header[2];
525     headers[0] = new Header("header1", "value1.1");
526     headers[1] = new Header("header2", "value2");
527     response.setHeaders(headers);
528     assertEquals("value1.1", response.getHeader("header1"));
529     response.setBody(Bytes.toBytes("body"));
530     assertTrue(response.hasBody());    
531   }
532   
533 }
534