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
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.hadoop.hbase.Cell;
33 import org.apache.hadoop.hbase.testclassification.MediumTests;
34 import org.apache.hadoop.hbase.client.Put;
35 import org.apache.hadoop.hbase.client.Result;
36 import org.apache.hadoop.hbase.client.ResultScanner;
37 import org.apache.hadoop.hbase.client.Scan;
38 import org.apache.hadoop.hbase.client.Table;
39 import org.apache.hadoop.hbase.util.Bytes;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.junit.experimental.categories.Category;
43
44
45
46
47 @Category(MediumTests.class)
48 public class TestFilterWithScanLimits extends FilterTestingCluster {
49 private static final Log LOG = LogFactory
50 .getLog(TestFilterWithScanLimits.class);
51
52 private static final String tableName = "scanWithLimit";
53 private static final String columnFamily = "f1";
54
55 @Test
56 public void testScanWithLimit() {
57 int kv_number = 0;
58 try {
59 Scan scan = new Scan();
60
61 scan.setBatch(2);
62 SingleColumnValueFilter filter = new SingleColumnValueFilter(
63 Bytes.toBytes(columnFamily), Bytes.toBytes("c5"),
64 CompareFilter.CompareOp.EQUAL, new SubstringComparator("2_c5"));
65
66
67 scan.setFilter(filter);
68 Table table = openTable(tableName);
69 ResultScanner scanner = table.getScanner(scan);
70
71
72
73
74
75 for (Result result : scanner) {
76 for (Cell kv : result.listCells()) {
77 kv_number++;
78 LOG.debug(kv_number + ". kv: " + kv);
79 }
80 }
81
82 scanner.close();
83 table.close();
84 } catch (Exception e) {
85
86 assertNotNull("No IncompatibleFilterException catched", e);
87 }
88 LOG.debug("check the fetched kv number");
89 assertEquals("We should not get result(s) returned.", 0, kv_number);
90 }
91
92 @BeforeClass
93 public static void prepareData() {
94 try {
95 createTable(tableName, columnFamily);
96 Table table = openTable(tableName);
97 List<Put> puts = new ArrayList<Put>();
98
99
100
101
102
103 for (int i = 1; i < 4; i++) {
104 Put put = new Put(Bytes.toBytes("row" + i));
105 for (int j = 1; j < 6; j++) {
106 put.add(Bytes.toBytes("f1"), Bytes.toBytes("c" + j),
107 Bytes.toBytes(i + "_c" + j));
108 }
109 puts.add(put);
110 }
111
112 table.put(puts);
113 table.close();
114 } catch (IOException e) {
115 assertNull("Exception found while putting data into table", e);
116 }
117 }
118
119 }