如何从HBase表中获取包含(或等于)特定ID的所有行? [英] How to get all rows containing (or equaling) a particular ID from an HBase table?
问题描述
HTable table = new HTable(Bytes)我有一个方法可以选择rowkey包含传入参数的行。 .toBytes(objectsTableName),连接);
public List< ObjectId> lookUp(String partialId){
if(partialId.matches([a-fA-F0-9] +)){
//从partialId创建一个正则表达式,它可以是
//将任何包含partialId的rowkey作为子字符串,
//然后获得具有指定rowkey的所有行
} else {
throw new IllegalArgumentException(
query must只能用十六进制值完成);
}
}
我不知道如何完成上面的代码。
我只知道以下代码可以在Hbase中获得具有指定rowkey的行。
字符串rowkey =123;
Get get = new Get(Bytes.toBytes(rowkey));
结果结果= table.get(get);
您可以使用 RowFilter 使用 RegexStringComparator 来做到这一点。或者,如果只是获取匹配给定子字符串的行,您可以将RowFilter与 SubstringComparator 。这是你如何使用HBase过滤器:
public static void main(String [] args)throws IOException {
Configuration conf = HBaseConfiguration.create();
HTable表=新的HTable(conf,demo);
扫描s =新扫描();
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()));
//其余的逻辑
}
rs.close();
table.close();
}
上面这段代码会给你所有包含 abc 作为其行键的一部分。
HTH
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<ObjectId> 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 RowFilter filter with RegexStringComparator to do that. Or, if it is just to fetch the rows which match a given substring you can use RowFilter with SubstringComparator. This is how you use HBase filters :
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
这篇关于如何从HBase表中获取包含(或等于)特定ID的所有行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!