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

StackOverflow https://stackoverflow.com/questions/23089979

  •  04-07-2023
  •  | 
  •  

سؤال

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top