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 static org.junit.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertNotNull; 022import static org.junit.jupiter.api.Assertions.assertNull; 023import static org.junit.jupiter.api.Assertions.assertTrue; 024 025import java.io.IOException; 026import java.util.ArrayList; 027import java.util.List; 028import org.apache.hadoop.conf.Configuration; 029import org.apache.hadoop.hbase.Cell; 030import org.apache.hadoop.hbase.CellUtil; 031import org.apache.hadoop.hbase.CompareOperator; 032import org.apache.hadoop.hbase.HBaseConfiguration; 033import org.apache.hadoop.hbase.HBaseTestingUtil; 034import org.apache.hadoop.hbase.HConstants; 035import org.apache.hadoop.hbase.MasterNotRunningException; 036import org.apache.hadoop.hbase.TableName; 037import org.apache.hadoop.hbase.ZooKeeperConnectionException; 038import org.apache.hadoop.hbase.client.Admin; 039import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 040import org.apache.hadoop.hbase.client.Connection; 041import org.apache.hadoop.hbase.client.ConnectionFactory; 042import org.apache.hadoop.hbase.client.Put; 043import org.apache.hadoop.hbase.client.Result; 044import org.apache.hadoop.hbase.client.ResultScanner; 045import org.apache.hadoop.hbase.client.Scan; 046import org.apache.hadoop.hbase.client.Table; 047import org.apache.hadoop.hbase.client.TableDescriptor; 048import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 049import org.apache.hadoop.hbase.testclassification.FilterTests; 050import org.apache.hadoop.hbase.testclassification.MediumTests; 051import org.apache.hadoop.hbase.util.Bytes; 052import org.junit.jupiter.api.AfterAll; 053import org.junit.jupiter.api.BeforeAll; 054import org.junit.jupiter.api.Tag; 055import org.junit.jupiter.api.Test; 056import org.slf4j.Logger; 057import org.slf4j.LoggerFactory; 058 059/** 060 * Test if the FilterWrapper retains the same semantics defined in the 061 * {@link org.apache.hadoop.hbase.filter.Filter} 062 */ 063@Tag(FilterTests.TAG) 064@Tag(MediumTests.TAG) 065public class TestFilterWrapper { 066 067 private static final Logger LOG = LoggerFactory.getLogger(TestFilterWrapper.class); 068 069 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 070 private static Configuration conf = null; 071 private static Admin admin = null; 072 private static TableName name = TableName.valueOf("test"); 073 private static Connection connection; 074 075 @Test 076 public void testFilterWrapper() { 077 int kv_number = 0; 078 int row_number = 0; 079 try { 080 Scan scan = new Scan(); 081 List<Filter> fs = new ArrayList<>(); 082 083 DependentColumnFilter f1 = new DependentColumnFilter(Bytes.toBytes("f1"), Bytes.toBytes("c5"), 084 true, CompareOperator.EQUAL, new SubstringComparator("c5")); 085 PageFilter f2 = new PageFilter(2); 086 fs.add(f1); 087 fs.add(f2); 088 FilterList filter = new FilterList(fs); 089 090 scan.setFilter(filter); 091 Table table = connection.getTable(name); 092 ResultScanner scanner = table.getScanner(scan); 093 094 // row2 (c1-c4) and row3(c1-c4) are returned 095 for (Result result : scanner) { 096 row_number++; 097 for (Cell kv : result.listCells()) { 098 LOG.debug(kv_number + ". kv: " + kv); 099 kv_number++; 100 assertEquals("row" + (row_number + 1), Bytes.toString(CellUtil.cloneRow(kv)), 101 "Returned row is not correct"); 102 } 103 } 104 105 scanner.close(); 106 table.close(); 107 } catch (Exception e) { 108 // no correct result is expected 109 assertNull(e, "Exception happens in scan"); 110 } 111 LOG.debug("check the fetched kv number"); 112 assertEquals(8, kv_number, "We should get 8 results returned."); 113 assertEquals(2, row_number, "We should get 2 rows returned"); 114 } 115 116 private static void prepareData() { 117 try { 118 Table table = connection.getTable(name); 119 assertTrue(admin.tableExists(name), "Fail to create the table"); 120 List<Put> puts = new ArrayList<>(); 121 122 // row1 => <f1:c1, 1_c1, ts=1>, <f1:c2, 1_c2, ts=2>, <f1:c3, 1_c3,ts=3>, 123 // <f1:c4,1_c4, ts=4>, <f1:c5, 1_c5, ts=5> 124 // row2 => <f1:c1, 2_c1, ts=2>, <f1,c2, 2_c2, ts=2>, <f1:c3, 2_c3,ts=2>, 125 // <f1:c4,2_c4, ts=2>, <f1:c5, 2_c5, ts=2> 126 // row3 => <f1:c1, 3_c1, ts=3>, <f1:c2, 3_c2, ts=3>, <f1:c3, 3_c3,ts=2>, 127 // <f1:c4,3_c4, ts=3>, <f1:c5, 3_c5, ts=3> 128 for (int i = 1; i < 4; i++) { 129 Put put = new Put(Bytes.toBytes("row" + i)); 130 for (int j = 1; j < 6; j++) { 131 long timestamp = j; 132 if (i != 1) timestamp = i; 133 put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("c" + j), timestamp, 134 Bytes.toBytes(i + "_c" + j)); 135 } 136 puts.add(put); 137 } 138 139 table.put(puts); 140 table.close(); 141 } catch (IOException e) { 142 assertNull(e, "Exception found while putting data into table"); 143 } 144 } 145 146 private static void createTable() { 147 assertNotNull(admin, "HBaseAdmin is not initialized successfully."); 148 if (admin != null) { 149 TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(name) 150 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(Bytes.toBytes("f1"))).build(); 151 152 try { 153 admin.createTable(tableDescriptor); 154 assertTrue(admin.tableExists(name), "Fail to create the table"); 155 } catch (IOException e) { 156 assertNull(e, "Exception found while creating table"); 157 } 158 } 159 } 160 161 private static void deleteTable() { 162 if (admin != null) { 163 try { 164 admin.disableTable(name); 165 admin.deleteTable(name); 166 } catch (IOException e) { 167 assertNull(e, "Exception found deleting the table"); 168 } 169 } 170 } 171 172 private static void initialize(Configuration conf) { 173 TestFilterWrapper.conf = HBaseConfiguration.create(conf); 174 TestFilterWrapper.conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 1); 175 try { 176 connection = ConnectionFactory.createConnection(TestFilterWrapper.conf); 177 admin = TEST_UTIL.getAdmin(); 178 } catch (MasterNotRunningException e) { 179 assertNull(e, "Master is not running"); 180 } catch (ZooKeeperConnectionException e) { 181 assertNull(e, "Cannot connect to ZooKeeper"); 182 } catch (IOException e) { 183 assertNull(e, "Caught IOException"); 184 } 185 createTable(); 186 prepareData(); 187 } 188 189 @BeforeAll 190 public static void setUp() throws Exception { 191 TEST_UTIL.startMiniCluster(1); 192 initialize(TEST_UTIL.getConfiguration()); 193 } 194 195 @AfterAll 196 public static void tearDown() throws Exception { 197 deleteTable(); 198 connection.close(); 199 TEST_UTIL.shutdownMiniCluster(); 200 } 201 202}