hbase的过滤器查询
发表于:2024-11-25 作者:热门IT资讯网编辑
编辑最后更新 2024年11月25日, hbase的过滤器有很多:大致分为两大类:比较过滤器和专用过滤器,过滤器的作用是在服务端判断数据是否满足条件,然后只将满足条件的数据返回给客户端;hbase 过滤器的比较运算符: LESS --
hbase的过滤器有很多:大致分为两大类:比较过滤器和专用过滤器,过滤器的作用是在服务端判断数据是否满足条件,然后只将满足条件的数据返回给客户端;
hbase 过滤器的比较运算符:
LESS ----- < LESS_OR_EQUAL ----- <= EQUAL ----- = NOT_EQUAL ----- <> GREATER_OR_EQUAL ----- >= GREATER ----- > NO_OP #排除所有
HBase 过滤器的比较器(指定比较机制):
BinaryComparator 按字节索引顺序比较指定字节数组,采用 Bytes.compareTo(byte[])BinaryPrefixComparator 跟前面相同,只是比较左端的数据是否相同NullComparator 判断给定的是否为空BitComparator 按位比较RegexStringComparator 提供一个正则的比较器,仅支持 EQUAL 和非 EQUALSubstringComparator 判断提供的子串是否出现在 value 中。
1. 比较过滤器
//行键过滤器 RowFilterFilter filter1 = new RowFilter(CompareOp.LESS_OR_EQUAL, newBinaryComparator(Bytes.toBytes("user0000")));scan.setFilter(filter1);
//列簇过滤器 FamilyFilterFilter filter1 = new FamilyFilter(CompareOp.LESS, newBinaryComparator(Bytes.toBytes("base_info")));scan.setFilter(filter1);
//列过滤器 QualifierFilterFilter filter = new QualifierFilter(CompareOp.LESS_OR_EQUAL, newBinaryComparator(Bytes.toBytes("name")));scan.setFilter(filter1);
//值过滤器 ValueFilterFilter filter = new ValueFilter(CompareOp.EQUAL, new SubstringComparator("zhangsan") );scan.setFilter(filter1);
//时间戳过滤器 TimestampsFilterList tss = new ArrayList();tss.add(1495398833002l);Filter filter1 = new TimestampsFilter(tss);scan.setFilter(filter1);
2.专用过滤器
//单列值过滤器 SingleColumnValueFilter ----会返回满足条件的整行SingleColumnValueFilter filter = new SingleColumnValueFilter( Bytes.toBytes("colfam1"), Bytes.toBytes("col-5"), CompareFilter.CompareOp.NOT_EQUAL, new SubstringComparator("val-5"));filter.setFilterIfMissing(true); //如果不设置为 true,则那些不包含指定 column 的行也会返回scan.setFilter(filter1);
//单列值排除器 SingleColumnValueExcludeFilter -----返回排除了该列的结果SingleColumnValueExcludeFilter filter = new SingleColumnValueExcludeFilter( Bytes.toBytes("colfam1"), Bytes.toBytes("col-5"), CompareFilter.CompareOp.NOT_EQUAL, new SubstringComparator("val-5"));filter.setFilterIfMissing(true); //如果不设置为 true,则那些不包含指定 column 的行也会返回scan.setFilter(filter1);
//前缀过滤器 PrefixFilter----针对行键Filter filter = new PrefixFilter(Bytes.toBytes("row1"));scan.setFilter(filter1);
//列前缀过滤器 ColumnPrefixFilterFilter filter = new ColumnPrefixFilter(Bytes.toBytes("qual2"));scan.setFilter(filter1);
实战案例:
public class HBase_Filter01 { private static String ZK_KEY = "hbase.zookeeper.quorum"; private static String ZK_VALUE = "hadoop01:2181,hadoop02:2181,hadoop03:2181"; private static Configuration conf; private static Connection connection; private static Admin admin; static { conf = HBaseConfiguration.create(); conf.set(ZK_KEY,ZK_VALUE); try { connection= ConnectionFactory.createConnection(conf); admin=connection.getAdmin(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { Scan scan=new Scan(); ValueFilter filter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("a")); scan.setFilter(filter); TableName tableName =TableName.valueOf("user_info"); try { Table table = connection.getTable(tableName); ResultScanner scanner = table.getScanner(scan); Iterator iterator=scanner.iterator(); while(iterator.hasNext()){ Result result = iterator.next(); System.out.println(result.list()); byte[] row = result.getRow(); System.out.println(new String(row)); } } catch (IOException e) { e.printStackTrace(); } }}