本文主要是介绍hbase scan的java操作,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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("--------------------------------------------------------------------");//正则匹配rkSystem.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 DescriptionLESS 小于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()));}}}}作者:会飞的蜗牛66666链接:https://www.jianshu.com/p/1da9fe1b2afe来源:简书简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
这篇关于hbase scan的java操作的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!