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