2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
.regionserver
;
20 import static junit
.framework
.Assert
.assertEquals
;
22 import java
.io
.IOException
;
23 import java
.util
.ArrayList
;
24 import java
.util
.Iterator
;
25 import java
.util
.List
;
26 import org
.apache
.hadoop
.hbase
.CompareOperator
;
27 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
28 import org
.apache
.hadoop
.hbase
.HBaseTestingUtil
;
29 import org
.apache
.hadoop
.hbase
.TableExistsException
;
30 import org
.apache
.hadoop
.hbase
.TableName
;
31 import org
.apache
.hadoop
.hbase
.TableNotFoundException
;
32 import org
.apache
.hadoop
.hbase
.client
.Admin
;
33 import org
.apache
.hadoop
.hbase
.client
.ColumnFamilyDescriptor
;
34 import org
.apache
.hadoop
.hbase
.client
.ColumnFamilyDescriptorBuilder
;
35 import org
.apache
.hadoop
.hbase
.client
.Durability
;
36 import org
.apache
.hadoop
.hbase
.client
.Put
;
37 import org
.apache
.hadoop
.hbase
.client
.Result
;
38 import org
.apache
.hadoop
.hbase
.client
.ResultScanner
;
39 import org
.apache
.hadoop
.hbase
.client
.Scan
;
40 import org
.apache
.hadoop
.hbase
.client
.Table
;
41 import org
.apache
.hadoop
.hbase
.client
.TableDescriptorBuilder
;
42 import org
.apache
.hadoop
.hbase
.filter
.BinaryComparator
;
43 import org
.apache
.hadoop
.hbase
.filter
.Filter
;
44 import org
.apache
.hadoop
.hbase
.filter
.SingleColumnValueFilter
;
45 import org
.apache
.hadoop
.hbase
.io
.compress
.Compression
.Algorithm
;
46 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
47 import org
.apache
.hadoop
.hbase
.testclassification
.RegionServerTests
;
48 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
49 import org
.junit
.AfterClass
;
50 import org
.junit
.BeforeClass
;
51 import org
.junit
.ClassRule
;
52 import org
.junit
.Test
;
53 import org
.junit
.experimental
.categories
.Category
;
55 @Category({RegionServerTests
.class, MediumTests
.class})
57 * This test verifies that the scenarios illustrated by HBASE-10850 work
58 * w.r.t. essential column family optimization
60 public class TestSCVFWithMiniCluster
{
63 public static final HBaseClassTestRule CLASS_RULE
=
64 HBaseClassTestRule
.forClass(TestSCVFWithMiniCluster
.class);
66 private static final TableName HBASE_TABLE_NAME
= TableName
.valueOf("TestSCVFWithMiniCluster");
68 private static final byte[] FAMILY_A
= Bytes
.toBytes("a");
69 private static final byte[] FAMILY_B
= Bytes
.toBytes("b");
71 private static final byte[] QUALIFIER_FOO
= Bytes
.toBytes("foo");
72 private static final byte[] QUALIFIER_BAR
= Bytes
.toBytes("bar");
74 private static Table htable
;
76 private static Filter scanFilter
;
78 private int expected
= 1;
81 public static void setUp() throws Exception
{
82 HBaseTestingUtil util
= new HBaseTestingUtil();
84 util
.startMiniCluster(1);
86 Admin admin
= util
.getAdmin();
87 destroy(admin
, HBASE_TABLE_NAME
);
88 create(admin
, HBASE_TABLE_NAME
, FAMILY_A
, FAMILY_B
);
90 htable
= util
.getConnection().getTable(HBASE_TABLE_NAME
);
93 List
<Put
> puts
= new ArrayList
<>();
95 /* Add a row with 'a:foo' = false */
96 Put put
= new Put(Bytes
.toBytes("1"));
97 put
.setDurability(Durability
.SKIP_WAL
);
98 put
.addColumn(FAMILY_A
, QUALIFIER_FOO
, Bytes
.toBytes("false"));
99 put
.addColumn(FAMILY_A
, QUALIFIER_BAR
, Bytes
.toBytes("_flag_"));
100 put
.addColumn(FAMILY_B
, QUALIFIER_FOO
, Bytes
.toBytes("_flag_"));
101 put
.addColumn(FAMILY_B
, QUALIFIER_BAR
, Bytes
.toBytes("_flag_"));
104 /* Add a row with 'a:foo' = true */
105 put
= new Put(Bytes
.toBytes("2"));
106 put
.setDurability(Durability
.SKIP_WAL
);
107 put
.addColumn(FAMILY_A
, QUALIFIER_FOO
, Bytes
.toBytes("true"));
108 put
.addColumn(FAMILY_A
, QUALIFIER_BAR
, Bytes
.toBytes("_flag_"));
109 put
.addColumn(FAMILY_B
, QUALIFIER_FOO
, Bytes
.toBytes("_flag_"));
110 put
.addColumn(FAMILY_B
, QUALIFIER_BAR
, Bytes
.toBytes("_flag_"));
113 /* Add a row with 'a:foo' qualifier not set */
114 put
= new Put(Bytes
.toBytes("3"));
115 put
.setDurability(Durability
.SKIP_WAL
);
116 put
.addColumn(FAMILY_A
, QUALIFIER_BAR
, Bytes
.toBytes("_flag_"));
117 put
.addColumn(FAMILY_B
, QUALIFIER_FOO
, Bytes
.toBytes("_flag_"));
118 put
.addColumn(FAMILY_B
, QUALIFIER_BAR
, Bytes
.toBytes("_flag_"));
123 * We want to filter out from the scan all rows that do not have the column 'a:foo' with value
124 * 'false'. Only row with key '1' should be returned in the scan.
126 scanFilter
= new SingleColumnValueFilter(FAMILY_A
, QUALIFIER_FOO
, CompareOperator
.EQUAL
,
127 new BinaryComparator(Bytes
.toBytes("false")));
128 ((SingleColumnValueFilter
) scanFilter
).setFilterIfMissing(true);
132 public static void tearDown() throws Exception
{
136 private void verify(Scan scan
) throws IOException
{
137 ResultScanner scanner
= htable
.getScanner(scan
);
138 Iterator
<Result
> it
= scanner
.iterator();
143 while (it
.hasNext()) {
150 assertEquals(expected
, count
);
153 * Test the filter by adding all columns of family A in the scan. (OK)
156 public void scanWithAllQualifiersOfFamiliyA() throws IOException
{
158 Scan scan
= new Scan();
159 scan
.addFamily(FAMILY_A
);
160 scan
.setFilter(scanFilter
);
166 * Test the filter by adding all columns of family A and B in the scan. (KO: row '3' without
167 * 'a:foo' qualifier is returned)
170 public void scanWithAllQualifiersOfBothFamilies() throws IOException
{
172 Scan scan
= new Scan();
173 scan
.setFilter(scanFilter
);
179 * Test the filter by adding 2 columns of family A and 1 column of family B in the scan. (KO: row
180 * '3' without 'a:foo' qualifier is returned)
183 public void scanWithSpecificQualifiers1() throws IOException
{
185 Scan scan
= new Scan();
186 scan
.addColumn(FAMILY_A
, QUALIFIER_FOO
);
187 scan
.addColumn(FAMILY_A
, QUALIFIER_BAR
);
188 scan
.addColumn(FAMILY_B
, QUALIFIER_BAR
);
189 scan
.addColumn(FAMILY_B
, QUALIFIER_FOO
);
190 scan
.setFilter(scanFilter
);
196 * Test the filter by adding 1 column of family A (the one used in the filter) and 1 column of
197 * family B in the scan. (OK)
200 public void scanWithSpecificQualifiers2() throws IOException
{
202 Scan scan
= new Scan();
203 scan
.addColumn(FAMILY_A
, QUALIFIER_FOO
);
204 scan
.addColumn(FAMILY_B
, QUALIFIER_BAR
);
205 scan
.setFilter(scanFilter
);
211 * Test the filter by adding 2 columns of family A in the scan. (OK)
214 public void scanWithSpecificQualifiers3() throws IOException
{
216 Scan scan
= new Scan();
217 scan
.addColumn(FAMILY_A
, QUALIFIER_FOO
);
218 scan
.addColumn(FAMILY_A
, QUALIFIER_BAR
);
219 scan
.setFilter(scanFilter
);
224 private static void create(Admin admin
, TableName tableName
, byte[]... families
)
226 TableDescriptorBuilder builder
= TableDescriptorBuilder
.newBuilder(tableName
);
227 for (byte[] family
: families
) {
228 ColumnFamilyDescriptor familyDescriptor
= ColumnFamilyDescriptorBuilder
.newBuilder(family
)
229 .setMaxVersions(1).setCompressionType(Algorithm
.GZ
).build();
230 builder
.setColumnFamily(familyDescriptor
);
233 admin
.createTable(builder
.build());
234 } catch (TableExistsException tee
) {
239 private static void destroy(Admin admin
, TableName tableName
) throws IOException
{
241 admin
.disableTable(tableName
);
242 admin
.deleteTable(tableName
);
243 } catch (TableNotFoundException tnfe
) {