Hbase葱岭探秘

时间:2022-01-22 04:56:42

Hbase中提供了许多的过滤器接口,以此来对数据进行过滤,使得查询出想要的数据。

行过滤器

针对行信息进行过滤,参数中可以采用前缀匹配、按位与、或、异或以及子串匹配等匹配的方式。同时可以控制EQUAL、NOT_EQUAL选项进行控制筛选数据的条件。

/** * 行过滤器 BinaryComparator NullComparator:是不是空值 * BitComparator:通过BitwiseOp类提供的按位与、或、异或操作进行位级别比较 RegexStringComparator:正则匹配 * SubStringComparator:子串是不是包含进行匹配 */ private static void testRowFilter() { try { HTable table = new HTable(config, "testtable"); Scan scan = new Scan(); scan.addColumn("col1".getBytes(), "name".getBytes()); // 行过滤器 Filter filter = new RowFilter(CompareOp.EQUAL, new BinaryComparator("row2".getBytes())); scan.setFilter(filter); ResultScanner result = table.getScanner(scan); for (Result res : result) { log.info("行过滤器>" + res); } // 正则的行过滤器 Filter filter2 = new RowFilter(CompareOp.EQUAL, new RegexStringComparator(".*.2")); scan.setFilter(filter2); ResultScanner resultRegx = table.getScanner(scan); for (Result res : resultRegx) { log.info("正则>" + res); } Filter filterSubString = new RowFilter(CompareOp.EQUAL, new SubstringComparator("w2")); scan.setFilter(filterSubString); ResultScanner resultSubString = table.getScanner(scan); for (Result res : resultSubString) { log.info("子串>" + res); } table.close(); } catch (IOException e) { log.error(e); } } 列族过滤器

根据列族的数据进行筛选,形式和上面的行过滤器类似,通过控制相应的参数中的筛选的条件进行相应的筛选。

/** * 列族过滤器 */ private static void testFamlyFilter() { try { HTable table = new HTable(config, "testtable"); Filter filter = new FamilyFilter(CompareOp.EQUAL, new BinaryComparator("col1".getBytes())); Scan scan = new Scan("row2".getBytes(), filter); ResultScanner result = table.getScanner(scan); for (Result res : result) { log.info(res); } Filter filterNull = new FamilyFilter(CompareOp.EQUAL, new RegexStringComparator(".*.1")); Scan scanNull = new Scan("row2".getBytes(), filterNull); scanNull.addFamily("col1".getBytes()); ResultScanner resultNull = table.getScanner(scanNull); if (resultNull != null) { for (Result res : resultNull) { log.info(res); } } else { log.info("null"); } table.close(); } catch (IOException e) { log.error(e); } } 列名过滤器

和上面几个过滤器类似,这里是根据列进行筛选,设置相应的条件后就可以进行相应的筛选了。

/** * 列名过滤器 */ public static void testColumFilter() { try { HTable table = new HTable(config, "testtable"); Filter filter = new QualifierFilter(CompareOp.EQUAL, new BinaryComparator("name".getBytes())); Scan scan = new Scan("row2".getBytes(), filter); ResultScanner result = table.getScanner(scan); for (Result res : result) { log.info(res); } Get get = new Get("row2".getBytes()); get.setFilter(filter); Result resultGet = table.get(get); log.info(resultGet); table.close(); } catch (IOException e) { log.info(e); } } 参考列过滤器

参考列过滤器根据列族和列限定符进行筛选,返回与参考列相同时间戳的行的所有键值对。

/** * 参考列过滤器 */ public static void testDependentColumnFilter() { try { HTable table = new HTable(config, "testtable"); Filter filter = new DependentColumnFilter("col1".getBytes(), "name".getBytes(), false); Scan scan = new Scan(); scan.setFilter(filter); ResultScanner resu = table.getScanner(scan); for (Result result : resu) { log.info(result); } Get get = new Get("row2".getBytes()); get.setFilter(filter); Result result = table.get(get); log.info(result); table.close(); } catch (IOException e) { log.error(e); } } 单列过滤器