Retrieve any three random qualifier in hbase using java

青春壹個敷衍的年華 提交于 2019-12-25 08:57:15

问题


My hbase table looks like this:

hbase(main):040:0> scan 'TEST' 
ROW               COLUMN+CELL                                                                                                
4                 column=data:108, timestamp=1399972960190, value=-240.0                                                     
4                 column=data:112, timestamp=1399972960138, value=-160.0                                                     
4                 column=data:12, timestamp=1399972922979, value=2                                                           
4                 column=data:120, timestamp=1399972960124, value=-152.0                                                     
4                 column=data:144, timestamp=1399972960171, value=-240.0                                                     
4                 column=data:148, timestamp=1399972960152, value=-240.0                                                     
4                 column=data:16, timestamp=1399972909606, value=9                                                           
4                 column=data:8, timestamp=1399972917978, value=6                 

where all 4s are row id and 108,112,12... are qualifiers. I want to fetch random three qualifiers from this table TEST.

I can fetch all qualifiers but not random three qualifiers. Is there any shell command or API in java through which I can achieve this?


回答1:


If it is about just getting the first three rows use scan shell command with LIMIT set to 3:

hbase(main):001:0> scan 'demo', {LIMIT => 3}

If you wish to do it using Java API, set a loop over ResultScanner which breaks after the third iteration. Simple and easy.

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

        Configuration conf = HBaseConfiguration.create();
        HTable table = new HTable(conf, "demo");
        Scan s = new Scan();
        ResultScanner rs = table.getScanner(s);
        int check = 0;
        for(Result r : rs){
            if(++check > 3)
                break;              
            for (KeyValue kv : r.raw()){                    
                System.out.println("Qualifier : " + Bytes.toString(kv.getQualifier()));
            }                           
        }
        rs.close();
        table.close();
    }
}

And if you wish to get 3 random rows, use RandomRowFilter with the above approach.

HTH



来源:https://stackoverflow.com/questions/23627847/retrieve-any-three-random-qualifier-in-hbase-using-java

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