中的rowkey 获取hbase,如何从HBase表中获取包含(或等于)特定ID的所有行?

该博客介绍了如何在HBase中通过编程方式查询包含特定子串的行键。它展示了创建一个正则表达式来匹配含有指定部分ID的行键,并使用`RowFilter`和`SubstringComparator`从HTable获取这些行的方法。
摘要由CSDN通过智能技术生成

I have a method which select the row whose rowkey contains the parameter passed into.

HTable table = new HTable(Bytes.toBytes(objectsTableName), connection);

public List lookUp(String partialId) {

if (partialId.matches("[a-fA-F0-9]+")) {

// create a regular expression from partialId, which can

//match any rowkey that contains partialId as a substring,

//and then get all the row with the specified rowkey

} else {

throw new IllegalArgumentException(

"query must be done with hexadecimal values only");

}

}

I don't know how to finish code above.

I just know the following code can get the row with specified rowkey in Hbase.

String rowkey = "123";

Get get = new Get(Bytes.toBytes(rowkey));

Result result = table.get(get);

解决方案

You can use

public static void main(String[] args) throws IOException {

Configuration conf = HBaseConfiguration.create();

HTable table = new HTable(conf, "demo");

Scan s = new Scan();

Filter f = new RowFilter(CompareOp.EQUAL, new SubstringComparator("abc"));

s.setFilter(f);

ResultScanner rs = table.getScanner(s);

for(Result r : rs){

System.out.println("RowKey : " + Bytes.toString(r.getRow()));

//rest of your logic

}

rs.close();

table.close();

}

The above piece of code will give you all the rows which contain abc as a part of their rowkeys.

HTH

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值