目录
概述
过滤器可以分为两种:比较过滤器和专用过滤器。过滤器的作用是在服务端判断数据是否满足条件,然后只将满足条件的数据返回给客户端。
-
比较过滤器
LESS —— 小于
LESS_OR_EQUAL —— 小于等于
EQUAL —— 等于
NOT_EQUAL —— 不等于
GREATER_OR_EQUAL —— 大于等于
GREATER —— 大于
NO_OP —— 排除所有 -
专用过滤器
BinaryComparator —— 按字节索引顺序比较指定字节数组,采用Bytes.compareTo(byte[])
BinaryPrefixComparator —— 跟前面相同,只是比较左端的数据是否相同
NullComparator —— 判断给定的是否为空
BitComparator —— 按位比较
RegexStringComparator —— 提供一个正则的比较器,仅支持 EQUAL 和非EQUAL
SubstringComparator —— 判断提供的子串是否出现在value中
代码实现
以下代码均有一个BeforeTest和一个AfterTest
/**
* 创建连接HBase服务器的方法
*/
private Connection connection;
private Table table;
@BeforeTest
public void init() throws IOException {
//获取连接
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");
connection = ConnectionFactory.createConnection(configuration);
//获取表
table = connection.getTable(TableName.valueOf("myuser"));
}
/**
* 关闭系统连接和表连接
*/
@AfterTest
public void close() throws IOException {
//关闭表
table.close();
connection.close();
}
rowKey过滤器RowFilter
/**
* 过滤rowKey比0003小的数据
*/
@Test
public void rowFilter() throws IOException {
Scan scan = new Scan();
//因为RowFilter需要一个binaryComparator参数,所以需要创建一个对象
BinaryComparator binaryComparator = new BinaryComparator("0003".getBytes());
//通过rowFilter按照rowKet进行过滤
RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.LESS, binaryComparator);
scan.setFilter(rowFilter);
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
byte[] row = result.getRow();
System.out.println("数据的rowKey为" + Bytes.toString(row));
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] family = cell.getFamily();
byte[] qualifier = cell.getQualifier();
byte[] value = cell.getValue();
//id列和age列是整型数据
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toInt(value));
} else {
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toString(value));
}
}
}
}
列族过滤器FamilyFilter
/**
* 列族过滤器,只获取f2列族的数据
*/
@Test
public void familyFilter() throws IOException {
//获取Scan对象
Scan scan = new Scan();
//FamilyFilter需要一个binaryComparator的参数,所以新建一个对象
BinaryComparator binaryComparator = new BinaryComparator("f2".getBytes());
//scan.setFilter需要的参数是FamilyFilter
FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.EQUAL, binaryComparator);
//数据装在scan中
scan.setFilter(familyFilter);
//拿到需要的所有数据
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
byte[] row = result.getRow();
System.out.println("数据的rowKey为" + Bytes.toString(row));
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] family = cell.getFamily();
byte[] qualifier = cell.getQualifier();
byte[] value = cell.getValue();
//id列和age列是整型数据
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toInt(value));
} else {
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toString(value));
}
}
}
}
列过滤器QualifierFilter
/**
* 列过滤器,只查询name这一列
*/
@Test
public void qualifierFilter() throws IOException {
//获取Scan对象
Scan scan = new Scan();
//列过滤器就是QualifierFilter,new一个
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("name"));
//将列过滤器的值设置到scan中
scan.setFilter(qualifierFilter);
//获取到所有的scan的数据
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
byte[] row = result.getRow();
System.out.println("数据的rowKey为" + Bytes.toString(row));
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] family = cell.getFamily();
byte[] qualifier = cell.getQualifier();
byte[] value = cell.getValue();
//id列和age列是整型数据
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toInt(value));
} else {
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toString(value));
}
}
}
}
列值过滤器ValueFilter
/**
* 列值过滤器,查询列中含有8的值
*/
@Test
public void valueFilter() throws IOException {
//获取Scan对象
Scan scan = new Scan();
//列值过滤器ValueFilter,new一个对象
ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("8"));
//将ValueFilter过滤后的值放到Scan中
scan.setFilter(valueFilter);
//获取所有数据
ResultScanner resultScanner = table.getScanner(scan);
//遍历循环得到每一条数据
for (Result result : resultScanner) {
//获取每一条数据的rowKey
byte[] row = result.getRow();
System.out.println("数据的rowKey为" + Bytes.toString(row));
//获取到数据中的每一个cell
List<Cell> cells = result.listCells();
//遍历循环得到每一个cell的值
for (Cell cell : cells) {
byte[] qualifier = cell.getQualifier();
byte[] family = cell.getFamily();
byte[] value = cell.getValue();
//id列和age列是整型数据
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toInt(value));
} else {
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toString(value));
}
}
}
}
专用过滤器
单列值过滤器 SingleColumnValueFilter
/**
* 单列值过滤器,查询name为刘备的人
*/
@Test
public void singleColumnValueFilter() throws IOException {
//new一个Scan对象
Scan scan = new Scan();
//单列值过滤器,new一个对象,来限定条件
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter("f1".getBytes(), "name".getBytes(), CompareFilter.CompareOp.EQUAL, "刘备".getBytes());
//获取限定条件后拿到的数据
scan.setFilter(singleColumnValueFilter);
//所有数据封装到resultScanner中
ResultScanner resultScanner = table.getScanner(scan);
//循环遍历resultScanner中的每条数据
for (Result result : resultScanner) {
//获取每条数据的rowKey值
byte[] row = result.getRow();
System.out.println("数据的rowKey为" + Bytes.toString(row));
//获取每条数据中的cell值
List<Cell> cells = result.listCells();
//遍历循环得到每一个cell
for (Cell cell : cells) {
byte[] qualifier = cell.getQualifier();
byte[] family = cell.getFamily();
byte[] value = cell.getValue();
//id列和age列是整型数据
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toInt(value));
} else {
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toString(value));
}
}
}
}
列值排除过滤器SingleColumnValueExcludeFilter
/**
* 列值排除过滤器
*/
@Test
public void singleColumnValueExcludeFilter() throws IOException {
Scan scan = new Scan();
scan.setFilter(new SingleColumnValueExcludeFilter("f1".getBytes(),"name".getBytes(), CompareFilter.CompareOp.EQUAL,"刘备".getBytes()));
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
byte[] row = result.getRow();
System.out.println("数据的rowKey为" + Bytes.toString(row));
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] qualifier = cell.getQualifier();
byte[] family = cell.getFamily();
byte[] value = cell.getValue();
//id列和age列是整型数据
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toInt(value));
} else {
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toString(value));
}
}
}
}
rowKey前缀过滤器PrefixFilter
/**
* row前缀过滤器,查询以00开头的所有前缀的rowkey
*/
@Test
public void prefixFilter() throws IOException {
Scan scan = new Scan();
PrefixFilter prefixFilter = new PrefixFilter("00".getBytes());
scan.setFilter(prefixFilter);
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
byte[] row = result.getRow();
System.out.println("数据的rowKey为" + Bytes.toString(row));
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] qualifier = cell.getQualifier();
byte[] family = cell.getFamily();
byte[] value = cell.getValue();
//id列和age列是整型数据
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toInt(value));
} else {
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toString(value));
}
}
}
}
&160;
分页过滤器PageFilter
/**
* 分页过滤器
* 分页有两个条件
* pageNum 第几页
* pageSize 每页有几条
*/
@Test
public void pageFilter() throws IOException {
int pageNum = 3;
int pageSize = 2;
/*
分为两种情况判断:
第一页
其他页
*/
if (pageNum == 1){
Scan scan = new Scan();
//设置起始rowKey
scan.setStartRow("".getBytes());
//设置最大的返回结果,返回pageSize条
scan.setMaxResultSize(pageSize);
//分页过滤器
PageFilter pageFilter = new PageFilter(pageSize);
scan.setFilter(pageFilter);
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
byte[] row = result.getRow();
System.out.println("数据的rowKey为" + Bytes.toString(row));
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] qualifier = cell.getQualifier();
byte[] family = cell.getFamily();
byte[] value = cell.getValue();
//id列和age列是整型数据
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toInt(value));
} else {
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toString(value));
}
}
}
} else {
String startRow = "";
Scan scan = new Scan();
/*
第二页的起始rowKey = 第一页的结束rowKey + 1
第三页的起始rowKey = 第二页的结束rowKey + 1
*/
int resultSize = (pageNum - 1) * pageSize + 1;
scan.setMaxResultSize(resultSize);
//设置一次性往前扫描5条,最后一个rowKey是第三页起始rowKey
PageFilter pageFilter = new PageFilter(resultSize);
scan.setFilter(pageFilter);
//resultScanner里面有5条数据
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
//获取rowKey
byte[] row = result.getRow();
//最后一次循环遍历 rowKey为0005
startRow = Bytes.toString(row);
}
Scan scan1 = new Scan();
scan1.setStartRow(startRow.getBytes());
scan1.setMaxResultSize(pageSize);
PageFilter pageFilter1 = new PageFilter(pageSize);
scan1.setFilter(pageFilter1);
ResultScanner scanner1 = table.getScanner(scan1);
for (Result result : scanner1) {
byte[] row = result.getRow();
System.out.println("数据的rowKey为" + Bytes.toString(row));
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] qualifier = cell.getQualifier();
byte[] family = cell.getFamily();
byte[] value = cell.getValue();
//id列和age列是整型数据
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toInt(value));
} else {
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toString(value));
}
}
}
}
}
多过滤器综合查询FilterList
需求: 使用 SingleColumnValueFilter 查询f1列族,name为刘备的数据,并且同时满足rowkey的前缀以00开头的数据(PrefixFilter)
/**
* 多过滤综合查询
* 需求: 使用 SingleColumnValueFilter 查询f1列族,name为刘备的数据,并且同时满足rowkey的前缀以00开头的数据(PrefixFilter)
*/
@Test
public void filterList() throws IOException {
Scan scan = new Scan();
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter("f1".getBytes(),"name".getBytes(), CompareFilter.CompareOp.EQUAL,"刘备".getBytes());
PrefixFilter prefixFilter = new PrefixFilter("00".getBytes());
FilterList filterList = new FilterList(singleColumnValueFilter, prefixFilter);
scan.setFilter(filterList);
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
byte[] row = result.getRow();
System.out.println("数据的rowKey为" + Bytes.toString(row));
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] qualifier = cell.getQualifier();
byte[] value = cell.getValue();
byte[] family = cell.getFamily();
//id列和age列是整型数据
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toInt(value));
} else {
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toString(value));
}
}
}
}
代码实现删除数据
/**
* 根据rowKey删除数据
*/
@Test
public void delete() throws IOException {
Delete delete = new Delete("0007".getBytes());
table.delete(delete);
}