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