hbase过滤查询, 感觉还是很有用的。 几率一下案例, 备用
比较器运算符 左边是代码名称,右边是 含义
CompareOp.EQUAL; =
CompareOp.LESS ;<
CompareOp.LESS_OR_EQUAL; <==
CompareOp.NOT_EQUAL ;!=
CompareOp.GREATER_OR_EQUAL; >=
CompareOp.GREATER; >
CompareOp.NO_OP ; (no operation)
本次用到了 列值过滤器 SingleColumnValueFilter
@Test
public void scanFilter() throws Exception {
FilterList filterList = new FilterList();
String start_rowkey = "20160810000000_2475_0825";
String stop_rowkey = "20160810165552_2999_0977";
String tableName = "t1";//下面为目标数据
List<String> arr = new ArrayList<String>();
arr.add("info,tone,5000");
arr.add("info,tone,5010");
for (int i=0 ;i<=1;i++) { // 各个条件之间是“与”的关系
if (i==0) {
String[] s = arr.get(i).split(",");
filterList.addFilter(new SingleColumnValueFilter(Bytes
.toBytes(s[0]), Bytes.toBytes(s[1]), CompareOp.GREATER, Bytes //tone大于5000
.toBytes(s[2])));
}else{
String[] s = arr.get(i).split(",");
filterList.addFilter(new SingleColumnValueFilter(Bytes //并且 tone小于5010的值
.toBytes(s[0]), Bytes.toBytes(s[1]), CompareOp.LESS, Bytes
.toBytes(s[2])));
}
}
Scan scan = new Scan();
scan.setFilter(filterList);
scan.setStartRow(Bytes.toBytes(start_rowkey));
scan.setStopRow(Bytes.toBytes(stop_rowkey));
ResultScanner rs = null;
HTable table = new HTable(conf, Bytes.toBytes(tableName));
try {
Date begin2 = new Date();
rs = table.getScanner(scan);
Date end2 = new Date();
long between2 = end2.getTime() - begin2.getTime();
System.out.println(" scanfilter数据 time = " + between2 + " ms!");
for (Result r : rs) {
for (KeyValue kv : r.list()) {
System.out.print(new String(kv.getRow()) + " ");
System.out.print(new String(kv.getFamily()) + ":");
System.out.print(new String(kv.getQualifier()) + " ");
System.out.print(kv.getTimestamp() + " ");
System.out.println(new String(kv.getValue()));
}
}
} finally {
rs.close();
}
System.out.println("scanfilter条件查询结束");
}
20160810165552_1887_0629 info:time 1470819341688 20160810165552
20160810165552_1887_0629 info:tone 1470819341688 501
20160810165552_1887_0629 info:val 1470819341688 901
scanfilter条件查询结束 得到想要的数据了
其中可以将
CompareOp.GREATER 替换为想要替换的 比较方式,进行值的筛选