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