在hbase上使用过滤器查询可参考:
(73条消息) 在Hbase使用过滤器(行键过滤器、列族与列过滤器、值过滤器)_小镭敲代码的博客-CSDN博客
目录
3、查询在info1的列族中name的值为python的数据
4、查询在info1的列族中name的值为python的数据
5、查询以n开头的列,并且值为python的数据(过滤器不止一个)
1、 查询 值里面有的数据
// 查询 值里面有的数据
public static void ScanValueFilter_1(String str) throws IOException {
// 获取表
Table table = HbaseHelper.GetConnection().getTable(TableName.valueOf("booksystem:bookinfo"));
// 查询valueFilter
Scan scan = new Scan();
// 创建值过滤器
ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes(str)));
// 为Scan 对象 设置过滤器
scan.setFilter(valueFilter);
// 在表上进行查询
ResultScanner scanner = table.getScanner(scan);
for(Result result:scanner){
for(Cell cell: result.rawCells()){
System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell))+":"+ Bytes.toString(CellUtil.cloneValue(cell)));
}
}
}
2、 查询值里面包含a的数据
// 查询值里面包含a的数据
public static void ScanValueFilter_2(String bookname) throws IOException {
// 获取表
Table table = HbaseHelper.GetConnection().getTable(TableName.valueOf("booksystem:bookinfo"));
// 查询valueFilter
Scan scan = new Scan();
// 创建值过滤器
ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.EQUAL,new SubstringComparator(bookname));
// 为Scan 对象 设置过滤器
scan.setFilter(valueFilter);
// 在表上进行查询
ResultScanner scanner = table.getScanner(scan);
for(Result result:scanner){
for(Cell cell: result.rawCells()){
System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell))+":"+ Bytes.toString(CellUtil.cloneValue(cell)));
}
}
}
3、查询在info1的列族中name的值为python的数据
//查询在info1的列族中name的值为python的数据
public static void ScanSingleColumnValueFilter_1(String family,String columnName,String value) throws IOException {
Scan scan = new Scan();
SingleColumnValueExcludeFilter singleColumnValueExcludeFilter = new SingleColumnValueExcludeFilter(Bytes.toBytes(family), Bytes.toBytes(columnName), CompareFilter.CompareOp.EQUAL, Bytes.toBytes(value));
scan.setFilter(singleColumnValueExcludeFilter);
output(scan);
}
4、查询在info1的列族中name的值为python的数据
public static void ScanColumnAndValueFilter_1(String columnName,String value) throws IOException{
Scan scan = new Scan();
//多个过滤器
ColumnPrefixFilter columnPrefixFilter = new ColumnPrefixFilter(Bytes.toBytes(columnName));
ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator(value));
//用一个集合装上多个过滤器
ArrayList<Filter> filters = new ArrayList<>();
filters.add(columnPrefixFilter);
filters.add(valueFilter);
//And
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL,filters); //FilterList.Operator.MUST_PASS_ALL满足所有过滤器,FilterList.Operator.MUST_PASS_ONE满足一个就可以
//为scan设置过滤器
scan.setFilter(filterList);
//用scan扫描数据
output(scan);
}
5、查询以n开头的列,并且值为python的数据(过滤器不止一个)
//查询以n开头的列,并且值为python的数据(过滤器不止一个)
public static void ScanColumnAndValueFilter_1(String columnName,String value) throws IOException{
Scan scan = new Scan();
//多个过滤器
ColumnPrefixFilter columnPrefixFilter = new ColumnPrefixFilter(Bytes.toBytes(columnName));
ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator(value));
//用一个集合装上多个过滤器
ArrayList<Filter> filters = new ArrayList<>();
filters.add(columnPrefixFilter);
filters.add(valueFilter);
//And
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL,filters); //FilterList.Operator.MUST_PASS_ALL满足所有过滤器,FilterList.Operator.MUST_PASS_ONE满足一个就可以
//为scan设置过滤器
scan.setFilter(filterList);
//用scan扫描数据
output(scan);
}
6、封装输出方法
public static void output(Scan scan) throws IOException {
Table table = HbaseHelper.GetConnection().getTable(TableName.valueOf("booksystem:bookinfo"));
ResultScanner scanner = table.getScanner(scan);
for(Result result:scanner){
for(Cell cell: result.rawCells()){
System.out.println(Bytes.toString(CellUtil.cloneRow(cell))+","+Bytes.toString(CellUtil.cloneFamily(cell))+","+Bytes.toString(CellUtil.cloneQualifier(cell))+":"+ Bytes.toString(CellUtil.cloneValue(cell)));
}
}
}
7、主函数
public static void main(String[] args) throws IOException{
System.out.println("请输入书名");
Scanner scanner = new Scanner(System.in);
String bookname = scanner.next();
Demo_1.ScanValueFilter_1(bookname);
//Demo_1.ScanSingleColumnValueFilter_1("info1","name",bookname);
//Demo_1.ScanColumnAndValueFilter_1("n",bookname);
}