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()));
}
}
}
}
网友评论