四种过滤器 1、行键 过滤器 2、 前缀过滤器 3、 时间戳过滤器 4、 列族过滤器
package com.bw.hbasefilter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.filter.BinaryComparator; import org.apache.hadoop.hbase.filter.CompareFilter; import org.apache.hadoop.hbase.filter.Filter; import org.apache.hadoop.hbase.filter.PrefixFilter; import org.apache.hadoop.hbase.filter.QualifierFilter; import org.apache.hadoop.hbase.filter.RegexStringComparator; import org.apache.hadoop.hbase.filter.RowFilter; import org.apache.hadoop.hbase.filter.TimestampsFilter; public class HbaseFilter { //静态块 连接zk 并且配置操作的表 static HTable table; static { try { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "linux04,linux05,linux06"); table = new HTable(conf, "Nurse"); } catch (IOException e) { e.printStackTrace(); } } //2、前缀过滤器 public static void prefixFilter() throws Exception { Scan scan = new Scan(); //过滤以 Nurse00009999 为前缀的行键 PrefixFilter filter = new PrefixFilter("Nurse00009999".getBytes()); scan.setFilter(filter); ResultScanner scanner = table.getScanner(scan); for (Result r : scanner) { System.out.println("行键row:" + new String(r.getRow()));// 行键 System.out.println("查询数据的结果:" + new String(r.getValue("info".getBytes(), "name".getBytes()))); } } //3、时间戳过滤器 两个时间之间的数据 public static void timeFilter() throws Exception { Scan scan = new Scan(); List<Long> list = new ArrayList<Long>(); list.add(1557323272643L); list.add(1558146272160L); TimestampsFilter filter = new TimestampsFilter(list); scan.setFilter(filter); ResultScanner scanner = table.getScanner(scan); for (Result r : scanner) { System.out.println("行键row:" + new String(r.getRow()));// 行键 System.out.println("查询数据的结果:" + new String(r.getValue("info".getBytes(), "name".getBytes()))); } } //4、列族过滤器 每行数据含有这一列的 public static void qualifierFilter() throws Exception { Scan scan = new Scan(); Filter filter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("age")); scan.setFilter(filter); ResultScanner scanner = table.getScanner(scan); for (Result r : scanner) { System.out.println("行键row:" + new String(r.getRow()));// 行键 System.out.println("查询数据的结果:" + new String(r.getValue("info".getBytes(), "name".getBytes()))); } } // 1、行键过滤器 public static void rowFilter() throws Exception { Scan scan = new Scan(); Filter filter = new RowFilter(CompareFilter.CompareOp.LESS, // 要的是小于 Nurse00000100 值的行键数据 new BinaryComparator("Nurse0000100".getBytes())); scan.setFilter(filter); ResultScanner scanner = table.getScanner(scan); for (Result r : scanner) { System.out.println("行键row:" + new String(r.getRow()));// 行键 System.out.println("查询数据的结果:" + new String(r.getValue("info".getBytes(), "name".getBytes()))); } } //调用 过滤器方法 public static void main(String[] args) throws Exception { //rowFilter(); //prefixFilter(); //timeFilter(); qualifierFilter(); } }