1.过滤value的值,可以创建的有正则比较器RegexStringComparator方法,字符串比较器SubstringComparator方法,前缀比较器BinaryPrefixComparator方法,二进制比较器BinaryComparator方法
2.创建列值过滤器SingleColumnValueFilter,需要传入的有列簇,列名,CompareFilter.CompareOp.EQUAL,比较器对象
3.向全表扫描对象scan增加过滤器,setFilter方法
1 public void scanSex() {
2 /**
3 * 获取所有文科一班性别为女的学生
4 *
5 */
6
7 try {
8 HTableInterface student = connection.getTable("student");
9 Scan scan = new Scan();
10
11 //创建创建比较器 正则比较器
12 RegexStringComparator comparator = new RegexStringComparator("文科,");
13
14 //字符串比较器
15 SubstringComparator comparator1 = new SubstringComparator("文科");
16
17 //前缀比较器
18 BinaryPrefixComparator prefixComparator = new BinaryPrefixComparator("理科".getBytes());
19
20 //二进制比较器
21 BinaryComparator binaryComparator = new BinaryComparator("文科一班".getBytes());
22
23 //创建过滤器,列值过滤器,全表扫描
24 SingleColumnValueFilter valueFilter = new SingleColumnValueFilter("info".getBytes(), "clazz".getBytes(),
25 CompareFilter.CompareOp.EQUAL, prefixComparator);
26
27 //增加过滤器
28 scan.setFilter(valueFilter);
29 //扫描全表
30 ResultScanner scanner = student.getScanner(scan);
31
32 Result next;
33 while((next = scanner.next()) != null) {
34 String sex = Bytes.toString(next.getValue("info".getBytes(),"gender".getBytes()));
35 if ("女".equals(sex)) {
36 print(next);
37 }
38 }
39 } catch (IOException e) {
40 e.printStackTrace();
41 }
42 }