HBase过滤器的使用

HBase中几种常用的过滤器

示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;

import java.util.Arrays;

import static org.apache.hadoop.hbase.util.Bytes.toBytes;

/**
* @ Description:
* @ Date: Created in 15:12 04/07/2018
* @ Author: Anthony_Duan
*/
public class HBaseFilterTest {

@Test
public void createTable(){
HbaseUtil.createTable("FileTable",new String []{"fileInfo","saveInfo"});
}


@Test
public void addFileDetails() {
HbaseUtil.putRow("FileTable", "rowkey1", "fileInfo", "name", "file1.txt");
HbaseUtil.putRow("FileTable", "rowkey1", "fileInfo", "type", "txt");
HbaseUtil.putRow("FileTable", "rowkey1", "fileInfo", "size", "1024");
HbaseUtil.putRow("FileTable", "rowkey1", "saveInfo", "creator", "jixin");
HbaseUtil.putRow("FileTable", "rowkey2", "fileInfo", "name", "file2.jpg");
HbaseUtil.putRow("FileTable", "rowkey2", "fileInfo", "type", "jpg");
HbaseUtil.putRow("FileTable", "rowkey2", "fileInfo", "size", "1024");
HbaseUtil.putRow("FileTable", "rowkey2", "saveInfo", "creator", "jixin");
HbaseUtil.putRow("FileTable", "rowkey3", "fileInfo", "name", "file3.jpg");
HbaseUtil.putRow("FileTable", "rowkey3", "fileInfo", "type", "jpg");
HbaseUtil.putRow("FileTable", "rowkey3", "fileInfo", "size", "1024");
HbaseUtil.putRow("FileTable", "rowkey3", "saveInfo", "creator", "jixin");

}


@Test
public void rowFilterTest(){

Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator("rowkey1".getBytes()));

FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL,Arrays.asList(filter));

ResultScanner scanner =
HbaseUtil.getScanner("FileTable","rowkey1","rowkey3",filterList);

if (scanner != null){
scanner.forEach(result -> {
System.out.println("rowkey="+ Bytes.toString(result.getRow())); //注意这里不能携程result.getRow().toString()这里打印出来的会是一个地址
System.out.println("fileName=" +
Bytes.toString(result.getValue(toBytes("fileInfo"),"name".getBytes())));//Bytes.toBytes() 可以替换为.getBytes()
});
scanner.close(); //关闭资源 Closes the scanner and releases any resources it has allocated
}

}

/**
* 筛选出具有特定前缀行键的数据
*/
@Test
public void prefixFilterTest(){

Filter filter = new PrefixFilter(toBytes("rowkey2"));
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL, Arrays.asList(filter));
ResultScanner resultScanner =
HbaseUtil.getScanner("FileTable", "rowkey1","rowkey3",filterList);
if (resultScanner != null){
resultScanner.forEach(result -> {
System.out.println("rowkey=" + Bytes.toString(result.getRow()));
System.out.println("fileName=" + Bytes.toString(result.getValue(toBytes("fileInfo"), Bytes.toBytes("name"))));
});
resultScanner.close();
}
}

/**
*
* 只返回每行的行键,值为空
*/
@Test
public void keyOnlyFilterTest(){

Filter filter = new KeyOnlyFilter(true);
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL,Arrays.asList(filter));

ResultScanner resultScanner= HbaseUtil.getScanner("FileTable", "rowkey1", "rowkey3", filterList);

if (resultScanner != null) {
resultScanner.forEach(result -> {
System.out.println("rowkey=" + Bytes.toString(result.getRow()));
System.out.println("fileName=" + Bytes
.toString(result.getValue(toBytes("fileInfo"), toBytes("name"))));
});

resultScanner.close();
}
}

/**
* 按照列名前缀筛选单元格
*/
@Test
public void columnPrefixFilterTest() {
Filter filter = new ColumnPrefixFilter(Bytes.toBytes("nam"));
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL, Arrays.asList(filter));
ResultScanner scanner = HbaseUtil
.getScanner("FileTable", "rowkey1", "rowkey3", filterList);

if (scanner != null) {
scanner.forEach(result -> {
System.out.println("rowkey=" + Bytes.toString(result.getRow()));
System.out.println("fileName=" + Bytes
.toString(result.getValue(Bytes.toBytes("fileInfo"), Bytes.toBytes("name"))));
System.out.println("fileType=" + Bytes
.toString(result.getValue(Bytes.toBytes("fileInfo"), Bytes.toBytes("type"))));
});
scanner.close();
}
}
}
-------------End Of This ArticleThank You For Reading-------------