HBase中的复合键

Fak*_*man 2 java cloud hadoop hbase mapreduce

我是HBase的新手,必须使用复合键作为主键.请告诉我

How to make composite-key in hbase?
And How to search a record using that composite-key?
Run Code Online (Sandbox Code Playgroud)

Tar*_*riq 5

只需加入密钥的各个部分即可使用它.没什么特别的.假设您有一个customer表,并且您希望拥有一个由CustID和Timestamp组成的rowkey.然后,您想要获取特定用户的所有结果,而不考虑时间戳.你会做这样的事情:

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

    Configuration conf = HBaseConfiguration.create();
    HTable table = new HTable(conf, "demo");
    String CustID = "tar024";
    byte[] rowKey = Bytes.add(Bytes.toBytes(CustID), Bytes.toBytes(System.currentTimeMillis()));

    //Put the data
    Put p = new Put(rowKey);
    System.out.println(Bytes.toString(rowKey));
    p.add(Bytes.toBytes("cf"), Bytes.toBytes("c1"), Bytes.toBytes("VALUE"));
    table.put(p);

    //Get the data
    Scan s = new Scan();
    Filter filter = new PrefixFilter(Bytes.toBytes("tar024"));
    s.setFilter(filter);
    ResultScanner rs = table.getScanner(s);
    for(Result r : rs){
        System.out.println("VALUE : " + Bytes.toString(r.getValue(Bytes.toBytes("cf"), Bytes.toBytes("c1"))));
    }
    rs.close();
    table.close();
}
Run Code Online (Sandbox Code Playgroud)

HTH