public class HbaseTest {
public static void main(String[] args) throws Exception {
//構建基本信息
String table = "t_userBehavior2";
final TableName tableName = TableName.valueOf(table);
final HTable hTable = new HTable(tableName, HBaseUtil.getConnection());
final Scan scan = new Scan();
//查詢出小于某個rk的全部數據
System.out.println("小于等于DD4400000047344000007497818085B0的行");
Filter filter1 = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,
new BinaryComparator("DD4400000047344000007497818085B0".getBytes()));
scan.setFilter(filter1);
ResultScanner scanner1 = hTable.getScanner(scan);
for (Result res : scanner1) {
System.out.println(res);
}
scanner1.close();
System.out.println("--------------------------------------------------------------------");
//正則匹配rk
System.out.println("正則獲取結尾為85B0的行");
final RowFilter filter2 = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator(".*85B0"));
scan.setFilter(filter2);
final ResultScanner scanner2 = hTable.getScanner(scan);
for (Result res : scanner2) {
System.out.println(res);
}
scanner2.close();
System.out.println("--------------------------------------------------------------------");
//rk包含某些字符串的所有行
System.out.println("包含有DD4400000047的行");
Filter filter3 = new RowFilter(CompareFilter.CompareOp.EQUAL,
new SubstringComparator("DD4400000047"));
scan.setFilter(filter3);
ResultScanner scanner3 = hTable.getScanner(scan);
for (Result res : scanner3) {
System.out.println(res);
}
scanner3.close();
System.out.println("--------------------------------------------------------------------");
//判斷rk的開頭信息的所有行
System.out.println("開頭是DD4400的");
Filter filter4 = new RowFilter(CompareFilter.CompareOp.EQUAL,
new BinaryPrefixComparator("DD4400".getBytes()));
scan.setFilter(filter4);
ResultScanner scanner4 = hTable.getScanner(scan);
for (Result res : scanner4) {
System.out.println(res);
}
scanner3.close();
System.out.println("----------------------------------------------------");
/***
- RowFilter用于過濾row key哨鸭,過濾的條件如下:
Operator Description
LESS 小于
LESS_OR_EQUAL 小于等于
EQUAL 等于
NOT_EQUAL 不等于
GREATER_OR_EQUAL 大于等于
GREATER 大于
NO_OP 排除所有
Comparator Description比較器:
BinaryComparator 使用Bytes.compareTo()比較
BinaryPrefixComparator 和BinaryComparator差不多娇妓,從前面開始比較
NullComparator Does not compare against an actual value but whether a given one is null, or not null.
BitComparator Performs a bitwise comparison, providing a BitwiseOp class with OR, and XORoperators.
RegexStringComparator 正則表達式
SubstringComparator 把數據當成字符串,用contains()來判斷
*scan.setFilter(new PrefixFilter(rowPrifix.getBytes()));根據rk的前綴來進行過濾出所有的行
*設定限制范圍只估,查詢出該范圍內的所有行
scan.setStartRow(getBytes(startRow));
-
scan.setStopRow(getBytes(stopRow));
*/scan.setStartRow(Bytes.toBytes("000000000"));
// scan.setStopRow(Bytes.toBytes("235959999"));
// scan.setFilter(new PrefixFilter(rowPrifix.getBytes()));
ResultScanner rs = hTable.getScanner(scan);
for (Result r : rs) {
KeyValue[] kv = r.raw();
for (int i = 0; i < kv.length; i++) {
final String rk = new String(kv[i].getRow());
final Result resut = HBaseUtil.getRow(table, rk.getBytes());
final List<Cell> cellList = resut.listCells();
for (Cell cell : cellList) {
final String value = new String(cell.getValue());
System.out.println(value);
}
System.out.print(new String(kv[i].getRow()) + " ");
System.out.print(new String(kv[i].getFamily()) + ":");
System.out.print(new String(kv[i].getQualifier()) + " ");
System.out.print(kv[i].getTimestamp() + " ");
System.out.println(new String(kv[i].getValue()));
}
}
}
}