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.filter;
21
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.conf.Configuration;
29 import org.apache.hadoop.hbase.Cell;
30 import org.apache.hadoop.hbase.CellUtil;
31 import org.apache.hadoop.hbase.HBaseConfiguration;
32 import org.apache.hadoop.hbase.HBaseTestingUtility;
33 import org.apache.hadoop.hbase.HColumnDescriptor;
34 import org.apache.hadoop.hbase.HConstants;
35 import org.apache.hadoop.hbase.HTableDescriptor;
36 import org.apache.hadoop.hbase.TableName;
37 import org.apache.hadoop.hbase.MasterNotRunningException;
38 import org.apache.hadoop.hbase.ZooKeeperConnectionException;
39 import org.apache.hadoop.hbase.client.HBaseAdmin;
40 import org.apache.hadoop.hbase.client.HTable;
41 import org.apache.hadoop.hbase.client.Put;
42 import org.apache.hadoop.hbase.client.Result;
43 import org.apache.hadoop.hbase.client.ResultScanner;
44 import org.apache.hadoop.hbase.client.Scan;
45 import org.apache.hadoop.hbase.util.Bytes;
46
47 import org.junit.AfterClass;
48 import org.junit.BeforeClass;
49 import org.junit.Test;
50 import static org.junit.Assert.*;
51
52 import org.apache.hadoop.hbase.testclassification.MediumTests;
53 import org.junit.experimental.categories.Category;
54
55
56
57
58
59 @Category(MediumTests.class)
60 public class TestFilterWrapper {
61 private static final Log LOG = LogFactory.getLog(TestFilterWrapper.class);
62
63 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
64 private static Configuration conf = null;
65 private static HBaseAdmin admin = null;
66 private static byte[] name = Bytes.toBytes("test");
67
68 @Test
69 public void testFilterWrapper() {
70 int kv_number = 0;
71 int row_number = 0;
72 try {
73 Scan scan = new Scan();
74 List<Filter> fs = new ArrayList<Filter>();
75
76 DependentColumnFilter f1 = new DependentColumnFilter(Bytes.toBytes("f1"),
77 Bytes.toBytes("c5"), true, CompareFilter.CompareOp.EQUAL,
78 new SubstringComparator("c5"));
79 PageFilter f2 = new PageFilter(2);
80 fs.add(f1);
81 fs.add(f2);
82 FilterList filter = new FilterList(fs);
83
84 scan.setFilter(filter);
85 HTable table = new HTable(conf, name);
86 ResultScanner scanner = table.getScanner(scan);
87
88
89 for (Result result : scanner) {
90 row_number++;
91 for (Cell kv : result.listCells()) {
92 LOG.debug(kv_number + ". kv: " + kv);
93 kv_number++;
94 assertEquals("Returned row is not correct", new String(CellUtil.cloneRow(kv)),
95 "row" + ( row_number + 1 ));
96 }
97 }
98
99 scanner.close();
100 table.close();
101 } catch (Exception e) {
102
103 assertNull("Exception happens in scan", e);
104 }
105 LOG.debug("check the fetched kv number");
106 assertEquals("We should get 8 results returned.", 8, kv_number);
107 assertEquals("We should get 2 rows returned", 2, row_number);
108 }
109
110 private static void prepareData() {
111 try {
112 HTable table = new HTable(TestFilterWrapper.conf, name);
113 assertTrue("Fail to create the table", admin.tableExists(name));
114 List<Put> puts = new ArrayList<Put>();
115
116
117
118
119
120
121
122 for (int i = 1; i < 4; i++) {
123 Put put = new Put(Bytes.toBytes("row" + i));
124 for (int j = 1; j < 6; j++) {
125 long timestamp = j;
126 if (i != 1)
127 timestamp = i;
128 put.add(Bytes.toBytes("f1"), Bytes.toBytes("c" + j), timestamp,
129 Bytes.toBytes(i + "_c" + j));
130 }
131 puts.add(put);
132 }
133
134 table.put(puts);
135 table.close();
136 } catch (IOException e) {
137 assertNull("Exception found while putting data into table", e);
138 }
139 }
140
141 private static void createTable() {
142 assertNotNull("HBaseAdmin is not initialized successfully.", admin);
143 if (admin != null) {
144
145 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(name));
146 HColumnDescriptor coldef = new HColumnDescriptor(Bytes.toBytes("f1"));
147 desc.addFamily(coldef);
148
149 try {
150 admin.createTable(desc);
151 assertTrue("Fail to create the table", admin.tableExists(name));
152 } catch (IOException e) {
153 assertNull("Exception found while creating table", e);
154 }
155
156 }
157 }
158
159 private static void deleteTable() {
160 if (admin != null) {
161 try {
162 admin.disableTable(name);
163 admin.deleteTable(name);
164 } catch (IOException e) {
165 assertNull("Exception found deleting the table", e);
166 }
167 }
168 }
169
170 private static void initialize(Configuration conf) {
171 TestFilterWrapper.conf = HBaseConfiguration.create(conf);
172 TestFilterWrapper.conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 1);
173 try {
174 admin = new HBaseAdmin(conf);
175 } catch (MasterNotRunningException e) {
176 assertNull("Master is not running", e);
177 } catch (ZooKeeperConnectionException e) {
178 assertNull("Cannot connect to Zookeeper", e);
179 } catch (IOException e) {
180 assertNull("Caught IOException", e);
181 }
182 createTable();
183 prepareData();
184 }
185
186 @BeforeClass
187 public static void setUp() throws Exception {
188 Configuration config = TEST_UTIL.getConfiguration();
189 TEST_UTIL.startMiniCluster(1);
190 initialize(TEST_UTIL.getConfiguration());
191 }
192
193 @AfterClass
194 public static void tearDown() throws Exception {
195 deleteTable();
196 TEST_UTIL.shutdownMiniCluster();
197 }
198
199 }