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