001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.filter; 019 020import org.apache.hadoop.hbase.CompareOperator; 021import org.apache.hadoop.hbase.HBaseClassTestRule; 022import org.apache.hadoop.hbase.HBaseTestingUtil; 023import org.apache.hadoop.hbase.TableName; 024import org.apache.hadoop.hbase.client.Put; 025import org.apache.hadoop.hbase.client.Result; 026import org.apache.hadoop.hbase.client.ResultScanner; 027import org.apache.hadoop.hbase.client.Scan; 028import org.apache.hadoop.hbase.client.Table; 029import org.apache.hadoop.hbase.filter.FilterList.Operator; 030import org.apache.hadoop.hbase.testclassification.FilterTests; 031import org.apache.hadoop.hbase.testclassification.MediumTests; 032import org.apache.hadoop.hbase.util.Bytes; 033import org.junit.AfterClass; 034import org.junit.Assert; 035import org.junit.BeforeClass; 036import org.junit.ClassRule; 037import org.junit.Rule; 038import org.junit.Test; 039import org.junit.experimental.categories.Category; 040import org.junit.rules.TestName; 041import org.slf4j.Logger; 042import org.slf4j.LoggerFactory; 043 044/** 045 * Tests filter Lists in ways that rely on a MiniCluster. Where possible, favor tests in 046 * TestFilterList and TestFilterFromRegionSide instead. 047 */ 048@Category({ MediumTests.class, FilterTests.class }) 049public class TestFilterListOnMini { 050 051 @ClassRule 052 public static final HBaseClassTestRule CLASS_RULE = 053 HBaseClassTestRule.forClass(TestFilterListOnMini.class); 054 055 private static final Logger LOG = LoggerFactory.getLogger(TestFilterListOnMini.class); 056 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 057 058 @Rule 059 public TestName name = new TestName(); 060 061 @BeforeClass 062 public static void setUpBeforeClass() throws Exception { 063 TEST_UTIL.startMiniCluster(1); 064 } 065 066 @AfterClass 067 public static void tearDownAfterClass() throws Exception { 068 TEST_UTIL.shutdownMiniCluster(); 069 } 070 071 @Test 072 public void testFiltersWithOR() throws Exception { 073 TableName tn = TableName.valueOf(name.getMethodName()); 074 Table table = TEST_UTIL.createTable(tn, new String[] { "cf1", "cf2" }); 075 byte[] CF1 = Bytes.toBytes("cf1"); 076 byte[] CF2 = Bytes.toBytes("cf2"); 077 Put put1 = new Put(Bytes.toBytes("0")); 078 put1.addColumn(CF1, Bytes.toBytes("col_a"), Bytes.toBytes(0)); 079 table.put(put1); 080 Put put2 = new Put(Bytes.toBytes("0")); 081 put2.addColumn(CF2, Bytes.toBytes("col_b"), Bytes.toBytes(0)); 082 table.put(put2); 083 FamilyFilter filterCF1 = new FamilyFilter(CompareOperator.EQUAL, new BinaryComparator(CF1)); 084 FamilyFilter filterCF2 = new FamilyFilter(CompareOperator.EQUAL, new BinaryComparator(CF2)); 085 FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE); 086 filterList.addFilter(filterCF1); 087 filterList.addFilter(filterCF2); 088 Scan scan = new Scan(); 089 scan.setFilter(filterList); 090 ResultScanner scanner = table.getScanner(scan); 091 LOG.info("Filter list: " + filterList); 092 for (Result rr = scanner.next(); rr != null; rr = scanner.next()) { 093 Assert.assertEquals(2, rr.size()); 094 } 095 } 096 097 /** 098 * Test case for HBASE-21620 099 */ 100 @Test 101 public void testColumnPrefixFilterConcatWithOR() throws Exception { 102 TableName tn = TableName.valueOf(name.getMethodName()); 103 byte[] cf1 = Bytes.toBytes("f1"); 104 byte[] row = Bytes.toBytes("row"); 105 byte[] value = Bytes.toBytes("value"); 106 String[] columns = new String[] { "1544768273917010001_lt", "1544768273917010001_w_1", 107 "1544768723910010001_ca_1", "1544768723910010001_lt", "1544768723910010001_ut_1", 108 "1544768723910010001_w_5", "1544769779710010001_lt", "1544769779710010001_w_5", 109 "1544769883529010001_lt", "1544769883529010001_w_5", "1544769915805010001_lt", 110 "1544769915805010001_w_5", "1544779883529010001_lt", "1544770422942010001_lt", 111 "1544770422942010001_w_5" }; 112 Table table = TEST_UTIL.createTable(tn, cf1); 113 for (int i = 0; i < columns.length; i++) { 114 Put put = new Put(row).addColumn(cf1, Bytes.toBytes(columns[i]), value); 115 table.put(put); 116 } 117 Scan scan = new Scan(); 118 scan.withStartRow(row).withStopRow(row, true) 119 .setFilter(new FilterList(Operator.MUST_PASS_ONE, 120 new ColumnPrefixFilter(Bytes.toBytes("1544770422942010001_")), 121 new ColumnPrefixFilter(Bytes.toBytes("1544769883529010001_")))); 122 ResultScanner scanner = table.getScanner(scan); 123 Result result; 124 int resultCount = 0; 125 int cellCount = 0; 126 while ((result = scanner.next()) != null) { 127 cellCount += result.listCells().size(); 128 resultCount++; 129 } 130 Assert.assertEquals(resultCount, 1); 131 Assert.assertEquals(cellCount, 4); 132 } 133}