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