1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.regionserver;
21
22 import static junit.framework.Assert.assertEquals;
23
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Iterator;
27 import java.util.List;
28
29 import org.apache.hadoop.hbase.HBaseTestingUtility;
30 import org.apache.hadoop.hbase.HColumnDescriptor;
31 import org.apache.hadoop.hbase.HTableDescriptor;
32 import org.apache.hadoop.hbase.MediumTests;
33 import org.apache.hadoop.hbase.TableExistsException;
34 import org.apache.hadoop.hbase.TableName;
35 import org.apache.hadoop.hbase.TableNotFoundException;
36 import org.apache.hadoop.hbase.client.Durability;
37 import org.apache.hadoop.hbase.client.HBaseAdmin;
38 import org.apache.hadoop.hbase.client.HTable;
39 import org.apache.hadoop.hbase.client.Put;
40 import org.apache.hadoop.hbase.client.Result;
41 import org.apache.hadoop.hbase.client.ResultScanner;
42 import org.apache.hadoop.hbase.client.Scan;
43 import org.apache.hadoop.hbase.filter.BinaryComparator;
44 import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
45 import org.apache.hadoop.hbase.filter.Filter;
46 import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
47 import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
48 import org.apache.hadoop.hbase.util.Bytes;
49 import org.junit.AfterClass;
50 import org.junit.BeforeClass;
51 import org.junit.Test;
52 import org.junit.experimental.categories.Category;
53
54 @Category(MediumTests.class)
55
56
57
58
59 public class TestSCVFWithMiniCluster {
60 private static final String HBASE_TABLE_NAME = "TestSCVFWithMiniCluster";
61
62 private static final byte[] FAMILY_A = Bytes.toBytes("a");
63 private static final byte[] FAMILY_B = Bytes.toBytes("b");
64
65 private static final byte[] QUALIFIER_FOO = Bytes.toBytes("foo");
66 private static final byte[] QUALIFIER_BAR = Bytes.toBytes("bar");
67
68 private static HTable htable;
69
70 private static Filter scanFilter;
71
72 private int expected = 1;
73
74 @BeforeClass
75 public static void setUp() throws Exception {
76 HBaseTestingUtility util = new HBaseTestingUtility();
77
78 util.startMiniCluster(1);
79
80 HBaseAdmin admin = util.getHBaseAdmin();
81 destroy(admin, HBASE_TABLE_NAME);
82 create(admin, HBASE_TABLE_NAME, FAMILY_A, FAMILY_B);
83 admin.close();
84 htable = new HTable(util.getConfiguration(), HBASE_TABLE_NAME);
85
86
87 List<Put> puts = new ArrayList<Put>();
88
89
90 Put put = new Put(Bytes.toBytes("1"));
91 put.setDurability(Durability.SKIP_WAL);
92 put.add(FAMILY_A, QUALIFIER_FOO, Bytes.toBytes("false"));
93 put.add(FAMILY_A, QUALIFIER_BAR, Bytes.toBytes("_flag_"));
94 put.add(FAMILY_B, QUALIFIER_FOO, Bytes.toBytes("_flag_"));
95 put.add(FAMILY_B, QUALIFIER_BAR, Bytes.toBytes("_flag_"));
96 puts.add(put);
97
98
99 put = new Put(Bytes.toBytes("2"));
100 put.setDurability(Durability.SKIP_WAL);
101 put.add(FAMILY_A, QUALIFIER_FOO, Bytes.toBytes("true"));
102 put.add(FAMILY_A, QUALIFIER_BAR, Bytes.toBytes("_flag_"));
103 put.add(FAMILY_B, QUALIFIER_FOO, Bytes.toBytes("_flag_"));
104 put.add(FAMILY_B, QUALIFIER_BAR, Bytes.toBytes("_flag_"));
105 puts.add(put);
106
107
108 put = new Put(Bytes.toBytes("3"));
109 put.setDurability(Durability.SKIP_WAL);
110 put.add(FAMILY_A, QUALIFIER_BAR, Bytes.toBytes("_flag_"));
111 put.add(FAMILY_B, QUALIFIER_FOO, Bytes.toBytes("_flag_"));
112 put.add(FAMILY_B, QUALIFIER_BAR, Bytes.toBytes("_flag_"));
113 puts.add(put);
114
115 htable.put(puts);
116
117
118
119
120 scanFilter = new SingleColumnValueFilter(FAMILY_A, QUALIFIER_FOO, CompareOp.EQUAL,
121 new BinaryComparator(Bytes.toBytes("false")));
122 ((SingleColumnValueFilter) scanFilter).setFilterIfMissing(true);
123 }
124
125 @AfterClass
126 public static void tearDown() throws Exception {
127 htable.close();
128 }
129
130 private void verify(Scan scan) throws IOException {
131 ResultScanner scanner = htable.getScanner(scan);
132 Iterator<Result> it = scanner.iterator();
133
134
135 int count = 0;
136 try {
137 while (it.hasNext()) {
138 it.next();
139 count++;
140 }
141 } finally {
142 scanner.close();
143 }
144 assertEquals(expected, count);
145 }
146
147
148
149 @Test
150 public void scanWithAllQualifiersOfFamiliyA() throws IOException {
151
152 Scan scan = new Scan();
153 scan.addFamily(FAMILY_A);
154 scan.setFilter(scanFilter);
155
156 verify(scan);
157 }
158
159
160
161
162
163 @Test
164 public void scanWithAllQualifiersOfBothFamilies() throws IOException {
165
166 Scan scan = new Scan();
167 scan.setFilter(scanFilter);
168
169 verify(scan);
170 }
171
172
173
174
175
176 @Test
177 public void scanWithSpecificQualifiers1() throws IOException {
178
179 Scan scan = new Scan();
180 scan.addColumn(FAMILY_A, QUALIFIER_FOO);
181 scan.addColumn(FAMILY_A, QUALIFIER_BAR);
182 scan.addColumn(FAMILY_B, QUALIFIER_BAR);
183 scan.addColumn(FAMILY_B, QUALIFIER_FOO);
184 scan.setFilter(scanFilter);
185
186 verify(scan);
187 }
188
189
190
191
192
193 @Test
194 public void scanWithSpecificQualifiers2() throws IOException {
195
196 Scan scan = new Scan();
197 scan.addColumn(FAMILY_A, QUALIFIER_FOO);
198 scan.addColumn(FAMILY_B, QUALIFIER_BAR);
199 scan.setFilter(scanFilter);
200
201 verify(scan);
202 }
203
204
205
206
207 @Test
208 public void scanWithSpecificQualifiers3() throws IOException {
209
210 Scan scan = new Scan();
211 scan.addColumn(FAMILY_A, QUALIFIER_FOO);
212 scan.addColumn(FAMILY_A, QUALIFIER_BAR);
213 scan.setFilter(scanFilter);
214
215 verify(scan);
216 }
217
218 private static void create(HBaseAdmin admin, String tableName, byte[]... families)
219 throws IOException {
220 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
221 for (byte[] family : families) {
222 HColumnDescriptor colDesc = new HColumnDescriptor(family);
223 colDesc.setMaxVersions(1);
224 colDesc.setCompressionType(Algorithm.GZ);
225 desc.addFamily(colDesc);
226 }
227 try {
228 admin.createTable(desc);
229 } catch (TableExistsException tee) {
230
231 }
232 }
233
234 private static void destroy(HBaseAdmin admin, String tableName) throws IOException {
235 try {
236 admin.disableTable(tableName);
237 admin.deleteTable(tableName);
238 } catch (TableNotFoundException tnfe) {
239
240 }
241 }
242 }