1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.rest.client;
22  
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.hadoop.hbase.HColumnDescriptor;
28  import org.apache.hadoop.hbase.HTableDescriptor;
29  import org.apache.hadoop.hbase.KeyValue;
30  import org.apache.hadoop.hbase.client.Delete;
31  import org.apache.hadoop.hbase.client.Get;
32  import org.apache.hadoop.hbase.client.HBaseAdmin;
33  import org.apache.hadoop.hbase.client.HTable;
34  import org.apache.hadoop.hbase.client.Put;
35  import org.apache.hadoop.hbase.client.Result;
36  import org.apache.hadoop.hbase.client.ResultScanner;
37  import org.apache.hadoop.hbase.client.Scan;
38  import org.apache.hadoop.hbase.rest.HBaseRESTClusterTestBase;
39  import org.apache.hadoop.hbase.rest.client.Client;
40  import org.apache.hadoop.hbase.rest.client.Cluster;
41  import org.apache.hadoop.hbase.rest.client.RemoteHTable;
42  import org.apache.hadoop.hbase.util.Bytes;
43  
44  public class TestRemoteTable extends HBaseRESTClusterTestBase {
45  
46    static final String TABLE = "TestRemoteTable";
47    static final byte[] ROW_1 = Bytes.toBytes("testrow1");
48    static final byte[] ROW_2 = Bytes.toBytes("testrow2");
49    static final byte[] ROW_3 = Bytes.toBytes("testrow3");
50    static final byte[] ROW_4 = Bytes.toBytes("testrow4");
51    static final byte[] COLUMN_1 = Bytes.toBytes("a");
52    static final byte[] COLUMN_2 = Bytes.toBytes("b");
53    static final byte[] COLUMN_3 = Bytes.toBytes("c");
54    static final byte[] QUALIFIER_1 = Bytes.toBytes("1");
55    static final byte[] QUALIFIER_2 = Bytes.toBytes("2");
56    static final byte[] QUALIFIER_3 = Bytes.toBytes("3");
57    static final byte[] VALUE_1 = Bytes.toBytes("testvalue1");
58    static final byte[] VALUE_2 = Bytes.toBytes("testvalue2");
59    static final byte[] VALUE_3 = Bytes.toBytes("testvalue3");
60  
61    static final long ONE_HOUR = 60 * 60 * 1000;
62    static final long TS_2 = System.currentTimeMillis();
63    static final long TS_1 = TS_2 - ONE_HOUR;
64  
65    Client client;
66    HBaseAdmin admin;
67    RemoteHTable remoteTable;
68  
69    @Override
70    protected void setUp() throws Exception {
71      super.setUp();
72      
73      admin = new HBaseAdmin(conf);
74      if (!admin.tableExists(TABLE)) {
75        HTableDescriptor htd = new HTableDescriptor(TABLE);
76        htd.addFamily(new HColumnDescriptor(COLUMN_1));
77        htd.addFamily(new HColumnDescriptor(COLUMN_2));
78        htd.addFamily(new HColumnDescriptor(COLUMN_3));
79        admin.createTable(htd);
80        HTable table = new HTable(TABLE);
81        Put put = new Put(ROW_1);
82        put.add(COLUMN_1, QUALIFIER_1, TS_2, VALUE_1);
83        table.put(put);
84        put = new Put(ROW_2);
85        put.add(COLUMN_1, QUALIFIER_1, TS_1, VALUE_1);
86        put.add(COLUMN_1, QUALIFIER_1, TS_2, VALUE_2);
87        put.add(COLUMN_2, QUALIFIER_2, TS_2, VALUE_2);
88        table.put(put);
89        table.flushCommits();
90      }
91      remoteTable = new RemoteHTable(
92        new Client(new Cluster().add("localhost", testServletPort)),
93          conf, TABLE, null);
94    }
95  
96    @Override
97    protected void tearDown() throws Exception {
98      remoteTable.close();
99      super.tearDown();
100   }
101 
102   public void testGetTableDescriptor() throws IOException {
103     HTableDescriptor local = new HTable(conf, TABLE).getTableDescriptor();
104     assertEquals(remoteTable.getTableDescriptor(), local);
105   }
106 
107   public void testGet() throws IOException {
108     Get get = new Get(ROW_1);
109     Result result = remoteTable.get(get);
110     byte[] value1 = result.getValue(COLUMN_1, QUALIFIER_1);
111     byte[] value2 = result.getValue(COLUMN_2, QUALIFIER_2);
112     assertNotNull(value1);
113     assertTrue(Bytes.equals(VALUE_1, value1));
114     assertNull(value2);
115 
116     get = new Get(ROW_1);
117     get.addFamily(COLUMN_3);
118     result = remoteTable.get(get);
119     value1 = result.getValue(COLUMN_1, QUALIFIER_1);
120     value2 = result.getValue(COLUMN_2, QUALIFIER_2);
121     assertNull(value1);
122     assertNull(value2);
123 
124     get = new Get(ROW_1);
125     get.addColumn(COLUMN_1, QUALIFIER_1);
126     get.addColumn(COLUMN_2, QUALIFIER_2);
127     result = remoteTable.get(get);
128     value1 = result.getValue(COLUMN_1, QUALIFIER_1);
129     value2 = result.getValue(COLUMN_2, QUALIFIER_2);
130     assertNotNull(value1);
131     assertTrue(Bytes.equals(VALUE_1, value1));
132     assertNull(value2);
133 
134     get = new Get(ROW_2);
135     result = remoteTable.get(get);    
136     value1 = result.getValue(COLUMN_1, QUALIFIER_1);
137     value2 = result.getValue(COLUMN_2, QUALIFIER_2);
138     assertNotNull(value1);
139     assertTrue(Bytes.equals(VALUE_2, value1)); // @TS_2
140     assertNotNull(value2);
141     assertTrue(Bytes.equals(VALUE_2, value2));
142 
143     get = new Get(ROW_2);
144     get.addFamily(COLUMN_1);
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     assertNull(value2);
151 
152     get = new Get(ROW_2);
153     get.addColumn(COLUMN_1, QUALIFIER_1);
154     get.addColumn(COLUMN_2, QUALIFIER_2);
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     assertNotNull(value2);
161     assertTrue(Bytes.equals(VALUE_2, value2));
162 
163     // test timestamp
164 
165     get = new Get(ROW_2);
166     get.addFamily(COLUMN_1);
167     get.addFamily(COLUMN_2);
168     get.setTimeStamp(TS_1);
169     result = remoteTable.get(get);    
170     value1 = result.getValue(COLUMN_1, QUALIFIER_1);
171     value2 = result.getValue(COLUMN_2, QUALIFIER_2);
172     assertNotNull(value1);
173     assertTrue(Bytes.equals(VALUE_1, value1)); // @TS_1
174     assertNull(value2);
175 
176     // test timerange
177 
178     get = new Get(ROW_2);
179     get.addFamily(COLUMN_1);
180     get.addFamily(COLUMN_2);
181     get.setTimeRange(0, TS_1 + 1);
182     result = remoteTable.get(get);    
183     value1 = result.getValue(COLUMN_1, QUALIFIER_1);
184     value2 = result.getValue(COLUMN_2, QUALIFIER_2);
185     assertNotNull(value1);
186     assertTrue(Bytes.equals(VALUE_1, value1)); // @TS_1
187     assertNull(value2);
188 
189     // test maxVersions
190 
191     get = new Get(ROW_2);
192     get.addFamily(COLUMN_1);
193     get.setMaxVersions(2);
194     result = remoteTable.get(get);
195     int count = 0;
196     for (KeyValue kv: result.list()) {
197       if (Bytes.equals(COLUMN_1, kv.getFamily()) && TS_1 == kv.getTimestamp()) {
198         assertTrue(Bytes.equals(VALUE_1, kv.getValue())); // @TS_1
199         count++;
200       }
201       if (Bytes.equals(COLUMN_1, kv.getFamily()) && TS_2 == kv.getTimestamp()) {
202         assertTrue(Bytes.equals(VALUE_2, kv.getValue())); // @TS_2
203         count++;
204       }
205     }
206     assertEquals(2, count);
207   }
208 
209   public void testPut() throws IOException {
210     Put put = new Put(ROW_3);
211     put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
212     remoteTable.put(put);
213 
214     Get get = new Get(ROW_3);
215     get.addFamily(COLUMN_1);
216     Result result = remoteTable.get(get);
217     byte[] value = result.getValue(COLUMN_1, QUALIFIER_1);
218     assertNotNull(value);
219     assertTrue(Bytes.equals(VALUE_1, value));
220 
221     // multiput
222 
223     List<Put> puts = new ArrayList<Put>();
224     put = new Put(ROW_3);
225     put.add(COLUMN_2, QUALIFIER_2, VALUE_2);
226     puts.add(put);
227     put = new Put(ROW_4);
228     put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
229     puts.add(put);
230     put = new Put(ROW_4);
231     put.add(COLUMN_2, QUALIFIER_2, VALUE_2);
232     puts.add(put);
233     remoteTable.put(puts);
234 
235     get = new Get(ROW_3);
236     get.addFamily(COLUMN_2);
237     result = remoteTable.get(get);
238     value = result.getValue(COLUMN_2, QUALIFIER_2);
239     assertNotNull(value);
240     assertTrue(Bytes.equals(VALUE_2, value));
241     get = new Get(ROW_4);
242     result = remoteTable.get(get);
243     value = result.getValue(COLUMN_1, QUALIFIER_1);
244     assertNotNull(value);
245     assertTrue(Bytes.equals(VALUE_1, value));
246     value = result.getValue(COLUMN_2, QUALIFIER_2);
247     assertNotNull(value);
248     assertTrue(Bytes.equals(VALUE_2, value));
249   }
250 
251   public void testDelete() throws IOException {
252     Put put = new Put(ROW_3);
253     put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
254     put.add(COLUMN_2, QUALIFIER_2, VALUE_2);
255     remoteTable.put(put);
256 
257     Get get = new Get(ROW_3);
258     get.addFamily(COLUMN_1);
259     get.addFamily(COLUMN_2);
260     Result result = remoteTable.get(get);
261     byte[] value1 = result.getValue(COLUMN_1, QUALIFIER_1);
262     byte[] value2 = result.getValue(COLUMN_2, QUALIFIER_2);
263     assertNotNull(value1);
264     assertTrue(Bytes.equals(VALUE_1, value1));
265     assertNotNull(value2);
266     assertTrue(Bytes.equals(VALUE_2, value2));
267 
268     Delete delete = new Delete(ROW_3);
269     delete.deleteColumn(COLUMN_2, QUALIFIER_2);
270     remoteTable.delete(delete);
271     
272     get = new Get(ROW_3);
273     get.addFamily(COLUMN_1);
274     get.addFamily(COLUMN_2);
275     result = remoteTable.get(get);
276     value1 = result.getValue(COLUMN_1, QUALIFIER_1);
277     value2 = result.getValue(COLUMN_2, QUALIFIER_2);
278     assertNotNull(value1);
279     assertTrue(Bytes.equals(VALUE_1, value1));
280     assertNull(value2);
281 
282     delete = new Delete(ROW_3);
283     remoteTable.delete(delete);
284 
285     get = new Get(ROW_3);
286     get.addFamily(COLUMN_1);
287     get.addFamily(COLUMN_2);
288     result = remoteTable.get(get);
289     value1 = result.getValue(COLUMN_1, QUALIFIER_1);
290     value2 = result.getValue(COLUMN_2, QUALIFIER_2);
291     assertNull(value1);
292     assertNull(value2);
293   }
294 
295   public void testScanner() throws IOException {
296     List<Put> puts = new ArrayList<Put>();
297     Put put = new Put(ROW_1);
298     put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
299     puts.add(put);
300     put = new Put(ROW_2);
301     put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
302     puts.add(put);
303     put = new Put(ROW_3);
304     put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
305     puts.add(put);
306     put = new Put(ROW_4);
307     put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
308     puts.add(put);
309     remoteTable.put(puts);
310 
311     ResultScanner scanner = remoteTable.getScanner(new Scan());
312 
313     Result[] results = scanner.next(1);
314     assertNotNull(results);
315     assertEquals(1, results.length);
316     assertTrue(Bytes.equals(ROW_1, results[0].getRow()));
317 
318     results = scanner.next(3);
319     assertNotNull(results);
320     assertEquals(3, results.length);
321     assertTrue(Bytes.equals(ROW_2, results[0].getRow()));
322     assertTrue(Bytes.equals(ROW_3, results[1].getRow()));
323     assertTrue(Bytes.equals(ROW_4, results[2].getRow()));
324 
325     results = scanner.next(1);
326     assertNull(results);
327 
328     scanner.close();
329   }
330 }