专用过滤器
1、单列值过滤器 SingleColumnValueFilter ----会返回满足条件的整行
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter( "info".getBytes(), //列簇 "name".getBytes(), //列 CompareOp.EQUAL, new SubstringComparator("刘晨")); //如果不设置为 true,则那些不包含指定 column 的行也会返回 singleColumnValueFilter.setFilterIfMissing(true); scan.setFilter(singleColumnValueFilter);
public class HbaseFilterTest2 { private static final String ZK_CONNECT_KEY = "hbase.zookeeper.quorum"; private static final String ZK_CONNECT_VALUE = "hadoop1:2181,hadoop2:2181,hadoop3:2181"; private static Connection conn = null; private static Admin admin = null; public static void main(String[] args) throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set(ZK_CONNECT_KEY, ZK_CONNECT_VALUE); conn = ConnectionFactory.createConnection(conf); admin = conn.getAdmin(); Table table = conn.getTable(TableName.valueOf("student")); Scan scan = new Scan(); SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter( "info".getBytes(), "name".getBytes(), CompareOp.EQUAL, new SubstringComparator("刘晨")); singleColumnValueFilter.setFilterIfMissing(true); scan.setFilter(singleColumnValueFilter); ResultScanner resultScanner = table.getScanner(scan); for(Result result : resultScanner) { List<Cell> cells = result.listCells(); for(Cell cell : cells) { System.out.println(Bytes.toString(cell.getRow()) + "\t" + Bytes.toString(cell.getFamily()) + "\t" + Bytes.toString(cell.getQualifier()) + "\t" + Bytes.toString(cell.getValue()) + "\t" + cell.getTimestamp()); } } } }
2、单列值排除器 SingleColumnValueExcludeFilter
SingleColumnValueExcludeFilter singleColumnValueExcludeFilter = new SingleColumnValueExcludeFilter( "info".getBytes(), "name".getBytes(), CompareOp.EQUAL, new SubstringComparator("刘晨")); singleColumnValueExcludeFilter.setFilterIfMissing(true); scan.setFilter(singleColumnValueExcludeFilter);
public class HbaseFilterTest2 { private static final String ZK_CONNECT_KEY = "hbase.zookeeper.quorum"; private static final String ZK_CONNECT_VALUE = "hadoop1:2181,hadoop2:2181,hadoop3:2181"; private static Connection conn = null; private static Admin admin = null; public static void main(String[] args) throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set(ZK_CONNECT_KEY, ZK_CONNECT_VALUE); conn = ConnectionFactory.createConnection(conf); admin = conn.getAdmin(); Table table = conn.getTable(TableName.valueOf("student")); Scan scan = new Scan(); SingleColumnValueExcludeFilter singleColumnValueExcludeFilter = new SingleColumnValueExcludeFilter( "info".getBytes(), "name".getBytes(), CompareOp.EQUAL, new SubstringComparator("刘晨")); singleColumnValueExcludeFilter.setFilterIfMissing(true); scan.setFilter(singleColumnValueExcludeFilter); ResultScanner resultScanner = table.getScanner(scan); for(Result result : resultScanner) { List<Cell> cells = result.listCells(); for(Cell cell : cells) { System.out.println(Bytes.toString(cell.getRow()) + "\t" + Bytes.toString(cell.getFamily()) + "\t" + Bytes.toString(cell.getQualifier()) + "\t" + Bytes.toString(cell.getValue()) + "\t" + cell.getTimestamp()); } } } }
3、前缀过滤器 PrefixFilter----针对行键
PrefixFilter prefixFilter = new PrefixFilter("9501".getBytes()); scan.setFilter(prefixFilter);
public class HbaseFilterTest2 { private static final String ZK_CONNECT_KEY = "hbase.zookeeper.quorum"; private static final String ZK_CONNECT_VALUE = "hadoop1:2181,hadoop2:2181,hadoop3:2181"; private static Connection conn = null; private static Admin admin = null; public static void main(String[] args) throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set(ZK_CONNECT_KEY, ZK_CONNECT_VALUE); conn = ConnectionFactory.createConnection(conf); admin = conn.getAdmin(); Table table = conn.getTable(TableName.valueOf("student")); Scan scan = new Scan(); PrefixFilter prefixFilter = new PrefixFilter("9501".getBytes()); scan.setFilter(prefixFilter); ResultScanner resultScanner = table.getScanner(scan); for(Result result : resultScanner) { List<Cell> cells = result.listCells(); for(Cell cell : cells) { System.out.println(Bytes.toString(cell.getRow()) + "\t" + Bytes.toString(cell.getFamily()) + "\t" + Bytes.toString(cell.getQualifier()) + "\t" + Bytes.toString(cell.getValue()) + "\t" + cell.getTimestamp()); } } } }
4、列前缀过滤器 ColumnPrefixFilter
ColumnPrefixFilter columnPrefixFilter = new ColumnPrefixFilter("name".getBytes()); scan.setFilter(columnPrefixFilter);
public class HbaseFilterTest2 { private static final String ZK_CONNECT_KEY = "hbase.zookeeper.quorum"; private static final String ZK_CONNECT_VALUE = "hadoop1:2181,hadoop2:2181,hadoop3:2181"; private static Connection conn = null; private static Admin admin = null; public static void main(String[] args) throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set(ZK_CONNECT_KEY, ZK_CONNECT_VALUE); conn = ConnectionFactory.createConnection(conf); admin = conn.getAdmin(); Table table = conn.getTable(TableName.valueOf("student")); Scan scan = new Scan(); ColumnPrefixFilter columnPrefixFilter = new ColumnPrefixFilter("name".getBytes()); scan.setFilter(columnPrefixFilter); ResultScanner resultScanner = table.getScanner(scan); for(Result result : resultScanner) { List<Cell> cells = result.listCells(); for(Cell cell : cells) { System.out.println(Bytes.toString(cell.getRow()) + "\t" + Bytes.toString(cell.getFamily()) + "\t" + Bytes.toString(cell.getQualifier()) + "\t" + Bytes.toString(cell.getValue()) + "\t" + cell.getTimestamp()); } } } }