View Javadoc

1   /*
2    * Copyright 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  package org.apache.hadoop.hbase.filter;
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.conf.Configuration;
29  import org.apache.hadoop.hbase.Cell;
30  import org.apache.hadoop.hbase.CellUtil;
31  import org.apache.hadoop.hbase.HBaseConfiguration;
32  import org.apache.hadoop.hbase.HBaseTestingUtility;
33  import org.apache.hadoop.hbase.HColumnDescriptor;
34  import org.apache.hadoop.hbase.HConstants;
35  import org.apache.hadoop.hbase.HTableDescriptor;
36  import org.apache.hadoop.hbase.TableName;
37  import org.apache.hadoop.hbase.MasterNotRunningException;
38  import org.apache.hadoop.hbase.ZooKeeperConnectionException;
39  import org.apache.hadoop.hbase.client.HBaseAdmin;
40  import org.apache.hadoop.hbase.client.HTable;
41  import org.apache.hadoop.hbase.client.Put;
42  import org.apache.hadoop.hbase.client.Result;
43  import org.apache.hadoop.hbase.client.ResultScanner;
44  import org.apache.hadoop.hbase.client.Scan;
45  import org.apache.hadoop.hbase.util.Bytes;
46  
47  import org.junit.AfterClass;
48  import org.junit.BeforeClass;
49  import org.junit.Test;
50  import static org.junit.Assert.*;
51  
52  import org.apache.hadoop.hbase.testclassification.MediumTests;
53  import org.junit.experimental.categories.Category;
54  
55  /**
56   * Test if the FilterWrapper retains the same semantics defined in the
57   * {@link org.apache.hadoop.hbase.filter.Filter}
58   */
59  @Category(MediumTests.class)
60  public class TestFilterWrapper {
61    private static final Log LOG = LogFactory.getLog(TestFilterWrapper.class);
62  
63    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
64    private static Configuration conf = null;
65    private static HBaseAdmin admin = null;
66    private static byte[] name = Bytes.toBytes("test");
67  
68    @Test
69    public void testFilterWrapper() {
70      int kv_number = 0;
71      int row_number = 0;
72      try {
73        Scan scan = new Scan();
74        List<Filter> fs = new ArrayList<Filter>();
75  
76        DependentColumnFilter f1 = new DependentColumnFilter(Bytes.toBytes("f1"),
77            Bytes.toBytes("c5"), true, CompareFilter.CompareOp.EQUAL,
78            new SubstringComparator("c5"));
79        PageFilter f2 = new PageFilter(2);
80        fs.add(f1);
81        fs.add(f2);
82        FilterList filter = new FilterList(fs);
83  
84        scan.setFilter(filter);
85        HTable table = new HTable(conf, name);
86        ResultScanner scanner = table.getScanner(scan);
87  
88        // row2 (c1-c4) and row3(c1-c4) are returned
89        for (Result result : scanner) {
90          row_number++;
91          for (Cell kv : result.listCells()) {
92            LOG.debug(kv_number + ". kv: " + kv);
93            kv_number++;
94            assertEquals("Returned row is not correct", new String(CellUtil.cloneRow(kv)),
95                "row" + ( row_number + 1 ));
96          }
97        }
98  
99        scanner.close();
100       table.close();
101     } catch (Exception e) {
102       // no correct result is expected
103       assertNull("Exception happens in scan", e);
104     }
105     LOG.debug("check the fetched kv number");
106     assertEquals("We should get 8 results returned.", 8, kv_number);
107     assertEquals("We should get 2 rows returned", 2, row_number);
108   }
109 
110   private static void prepareData() {
111     try {
112       HTable table = new HTable(TestFilterWrapper.conf, name);
113       assertTrue("Fail to create the table", admin.tableExists(name));
114       List<Put> puts = new ArrayList<Put>();
115 
116       // row1 => <f1:c1, 1_c1, ts=1>, <f1:c2, 1_c2, ts=2>, <f1:c3, 1_c3,ts=3>,
117       // <f1:c4,1_c4, ts=4>, <f1:c5, 1_c5, ts=5>
118       // row2 => <f1:c1, 2_c1, ts=2>, <f1,c2, 2_c2, ts=2>, <f1:c3, 2_c3,ts=2>,
119       // <f1:c4,2_c4, ts=2>, <f1:c5, 2_c5, ts=2>
120       // row3 => <f1:c1, 3_c1, ts=3>, <f1:c2, 3_c2, ts=3>, <f1:c3, 3_c3,ts=2>,
121       // <f1:c4,3_c4, ts=3>, <f1:c5, 3_c5, ts=3>
122       for (int i = 1; i < 4; i++) {
123         Put put = new Put(Bytes.toBytes("row" + i));
124         for (int j = 1; j < 6; j++) {
125           long timestamp = j;
126           if (i != 1)
127             timestamp = i;
128           put.add(Bytes.toBytes("f1"), Bytes.toBytes("c" + j), timestamp,
129               Bytes.toBytes(i + "_c" + j));
130         }
131         puts.add(put);
132       }
133 
134       table.put(puts);
135       table.close();
136     } catch (IOException e) {
137       assertNull("Exception found while putting data into table", e);
138     }
139   }
140 
141   private static void createTable() {
142     assertNotNull("HBaseAdmin is not initialized successfully.", admin);
143     if (admin != null) {
144 
145       HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(name));
146       HColumnDescriptor coldef = new HColumnDescriptor(Bytes.toBytes("f1"));
147       desc.addFamily(coldef);
148 
149       try {
150         admin.createTable(desc);
151         assertTrue("Fail to create the table", admin.tableExists(name));
152       } catch (IOException e) {
153         assertNull("Exception found while creating table", e);
154       }
155 
156     }
157   }
158 
159   private static void deleteTable() {
160     if (admin != null) {
161       try {
162         admin.disableTable(name);
163         admin.deleteTable(name);
164       } catch (IOException e) {
165         assertNull("Exception found deleting the table", e);
166       }
167     }
168   }
169 
170   private static void initialize(Configuration conf) {
171     TestFilterWrapper.conf = HBaseConfiguration.create(conf);
172     TestFilterWrapper.conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 1);
173     try {
174       admin = new HBaseAdmin(conf);
175     } catch (MasterNotRunningException e) {
176       assertNull("Master is not running", e);
177     } catch (ZooKeeperConnectionException e) {
178       assertNull("Cannot connect to Zookeeper", e);
179     } catch (IOException e) {
180       assertNull("Caught IOException", e);
181     }
182     createTable();
183     prepareData();
184   }
185 
186   @BeforeClass
187   public static void setUp() throws Exception {
188     Configuration config = TEST_UTIL.getConfiguration();
189     TEST_UTIL.startMiniCluster(1);
190     initialize(TEST_UTIL.getConfiguration());
191   }
192 
193   @AfterClass
194   public static void tearDown() throws Exception {
195     deleteTable();
196     TEST_UTIL.shutdownMiniCluster();
197   }
198 
199 }