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  package org.apache.hadoop.hbase.filter;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.hadoop.hbase.HBaseTestingUtility;
24  import org.apache.hadoop.hbase.TableName;
25  import org.apache.hadoop.hbase.client.*;
26  import org.apache.hadoop.hbase.filter.FilterList.Operator;
27  import org.apache.hadoop.hbase.testclassification.FilterTests;
28  import org.apache.hadoop.hbase.testclassification.MediumTests;
29  import org.apache.hadoop.hbase.util.Bytes;
30  import org.junit.AfterClass;
31  import org.junit.Assert;
32  import org.junit.BeforeClass;
33  import org.junit.Rule;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  import org.junit.rules.TestName;
37  
38  /**
39   * Tests filter Lists in ways that rely on a MiniCluster. Where possible, favor tests in
40   * TestFilterList and TestFilterFromRegionSide instead.
41   */
42  @Category({ MediumTests.class, FilterTests.class })
43  public class TestFilterListOnMini {
44  
45    private static final Log LOG = LogFactory.getLog(TestFilterListOnMini.class);
46    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
47  
48    @Rule
49    public TestName name = new TestName();
50  
51    @BeforeClass
52    public static void setUpBeforeClass() throws Exception {
53      TEST_UTIL.startMiniCluster(1);
54    }
55  
56    @AfterClass
57    public static void tearDownAfterClass() throws Exception {
58      TEST_UTIL.shutdownMiniCluster();
59    }
60  
61    @Test
62    public void testFiltersWithOR() throws Exception {
63      TableName tn = TableName.valueOf(name.getMethodName());
64      Table table = TEST_UTIL.createTable(tn, new String[] { "cf1", "cf2" });
65      byte[] CF1 = Bytes.toBytes("cf1");
66      byte[] CF2 = Bytes.toBytes("cf2");
67      Put put1 = new Put(Bytes.toBytes("0"));
68      put1.addColumn(CF1, Bytes.toBytes("col_a"), Bytes.toBytes(0));
69      table.put(put1);
70      Put put2 = new Put(Bytes.toBytes("0"));
71      put2.addColumn(CF2, Bytes.toBytes("col_b"), Bytes.toBytes(0));
72      table.put(put2);
73      FamilyFilter filterCF1 =
74          new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(CF1));
75      FamilyFilter filterCF2 =
76          new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(CF2));
77      FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE);
78      filterList.addFilter(filterCF1);
79      filterList.addFilter(filterCF2);
80      Scan scan = new Scan();
81      scan.setFilter(filterList);
82      ResultScanner scanner = table.getScanner(scan);
83      LOG.info("Filter list: " + filterList);
84      for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
85        Assert.assertEquals(2, rr.size());
86      }
87    }
88  
89    /**
90     * Test case for HBASE-21620
91     */
92    @Test
93    public void testColumnPrefixFilterConcatWithOR() throws Exception {
94      TableName tn = TableName.valueOf(name.getMethodName());
95      byte[] cf1 = Bytes.toBytes("f1");
96      byte[] row = Bytes.toBytes("row");
97      byte[] value = Bytes.toBytes("value");
98      String[] columns = new String[]{
99        "1544768273917010001_lt",
100       "1544768273917010001_w_1",
101       "1544768723910010001_ca_1",
102       "1544768723910010001_lt",
103       "1544768723910010001_ut_1",
104       "1544768723910010001_w_5",
105       "1544769779710010001_lt",
106       "1544769779710010001_w_5",
107       "1544769883529010001_lt",
108       "1544769883529010001_w_5",
109       "1544769915805010001_lt",
110       "1544769915805010001_w_5",
111       "1544779883529010001_lt",
112       "1544770422942010001_lt",
113       "1544770422942010001_w_5"
114     };
115     Table table = TEST_UTIL.createTable(tn, cf1);
116     for (int i = 0; i < columns.length; i++) {
117       Put put = new Put(row).addColumn(cf1, Bytes.toBytes(columns[i]), value);
118       table.put(put);
119     }
120     Scan scan = new Scan();
121     scan.withStartRow(row).withStopRow(row, true)
122         .setFilter(new FilterList(Operator.MUST_PASS_ONE,
123             new ColumnPrefixFilter(Bytes.toBytes("1544770422942010001_")),
124             new ColumnPrefixFilter(Bytes.toBytes("1544769883529010001_"))));
125     ResultScanner scanner = table.getScanner(scan);
126     Result result;
127     int resultCount = 0;
128     int cellCount = 0;
129     while ((result = scanner.next()) != null) {
130       cellCount += result.listCells().size();
131       resultCount++;
132     }
133     Assert.assertEquals(resultCount, 1);
134     Assert.assertEquals(cellCount, 4);
135   }
136 }