Question

I am new to HBase and have to use composite-key as row-key. Please tell me

And How to scan or(get) some records using that composite-key?(in 1st and 2nd and the other parts of R.K).(for e.g. Rowkey: <123-tom-1991> scan on tom)

Edit : Row key is Byte Array (This is equally important in the question)

Was it helpful?

Solution

    Configuration conf = HBaseConfiguration.create();
    HTable table = new HTable(conf, "t1");
    Scan s = new Scan ();
    RegexStringComparator keyRegEx = new RegexStringComparator(".*tom.*");
    RowFilter rowFilter = new RowFilter(CompareOp.EQUAL, new  RegexStringComparator(".*tom.*"));
    s.setFilter(rowFilter);
    ResultScanner scanner = table.getScanner(s);
    Iterator<Result> it = scanner.iterator();
    while(it.hasNext()){
        // ... your code ...

}

Just alter the regex as per your requirement. .*?-tom-.* would hold good here.

As per the edit, the above answer would not hold good. If its extremely important to have the row key as byte array, please use the FuzzyRowFilter.

Quoting the javadocs

**Filters data based on fuzzy row key. Performs fast-forwards during scanning. It takes pairs (row key, fuzzy info) to match row keys. Where fuzzy info is a byte array with 0 or 1 as its values:

0 - means that this byte in provided row key is fixed, i.e. row key's byte at same position must match

1 - means that this byte in provided row key is NOT fixed, i.e. row key's byte at this position can be different from the one in provided row key

Example: Let's assume row key format is userId_actionId_year_month. Length of userId is fixed and is 4, length of actionId is 2 and year and month are 4 and 2 bytes long respectively. Let's assume that we need to fetch all users that performed certain action (encoded as "99") in Jan of any year. Then the pair (row key, fuzzy info) would be the following: row key = "????99????_01" (one can use any value instead of "?") fuzzy info = "\x01\x01\x01\x01\x00\x00\x00\x00\x01\x01\x01\x01\x00\x00\x00" I.e. fuzzy info tells the matching mask is "????99????_01", where at ? can be any value.**

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top