How to get all rows containing (or equaling) a particular ID from an HBase table?

落爺英雄遲暮 提交于 2019-12-24 07:36:18

问题


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);

回答1:


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



来源:https://stackoverflow.com/questions/23089979/how-to-get-all-rows-containing-or-equaling-a-particular-id-from-an-hbase-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!