本次操作的hbase的t1表的数据是:
hbase(main):015:0> scan 't1' ROW COLUMN+CELL 1 column=f1:age, timestamp=1468824267106, value=10 1 column=f1:gender, timestamp=1468824289990, value=male 1 column=f1:name, timestamp=1468824137463, value=zhangsan 2 column=f1:name, timestamp=1468824236014, value=lisi 3 column=f1:name, timestamp=1468824247109, value=wangwu 4 column=f1:birthday, timestamp=1468825870158, value=1993 4 column=f1:name, timestamp=1468825659207, value=zhaoliu a1 column=f1:name, timestamp=1469000782113, value=a1 a2 column=f1:name, timestamp=1469000805242, value=a2 a3 column=f1:name, timestamp=1469000813427, value=a3 7 row(s) in 0.0530 seconds
需求1:查询rk为数字的全部记录
public class TestFilter { public static void main(String[] args) throws Exception { new TestFilter().test1(); } public void test1() throws Exception{ Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "hadoop26:2181"); conf.set("hbase.rootdir", "hdfs://hadoop26:9000/hbase"); HTable hTable = new HTable(conf, "t1"); Scan scan = new Scan(); scan.setStartRow("/".getBytes());//重点是在这里,因为rk是按照字节顺序排序的,/的asc在数字之前 scan.setStopRow(":".getBytes()); ResultScanner scanner = hTable.getScanner(scan); for (Result result : scanner) { System.out.println(new String(result.getRow())); Cell[] rawCells = result.rawCells(); for (Cell cell : rawCells) { System.out.println(new String(CellUtil.cloneFamily(cell))+" "+new String(CellUtil.cloneQualifier(cell))+" "+new String(CellUtil.cloneValue(cell))); } } hTable.close(); } }
需求2:查询以字母a开头的数据
public class TestFilter { public static void main(String[] args) throws Exception { new TestFilter().test2(); } public void test2() throws Exception{ Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "hadoop26:2181"); conf.set("hbase.rootdir", "hdfs://hadoop26:9000/hbase"); HTable hTable = new HTable(conf, "t1"); Scan scan = new Scan(); Filter filter = new RowFilter(CompareOp.EQUAL,new RegexStringComparator("^a")); scan.setFilter(filter); ResultScanner scanner = hTable.getScanner(scan); for (Result result : scanner) { System.out.println(new String(result.getRow())); Cell[] rawCells = result.rawCells(); for (Cell cell : rawCells) { System.out.println(new String(CellUtil.cloneFamily(cell))+" "+new String(CellUtil.cloneQualifier(cell))+" "+new String(CellUtil.cloneValue(cell))); } } hTable.close(); } }