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 替換爲想要替換的 比較方式,進行值的篩選