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