我是 HBase 的新手,必须使用组合键作为主键。请告诉我
How to make composite-key in hbase?
And How to search a record using that composite-key?
最佳答案
只需连接您的 key 部分并使用它。没什么特别的。假设您有一个客户表,并且您想要一个由 CustID 和 Timestamp 组成的行键。然后你想获取特定用户的所有结果,而不考虑时间戳。你会做这样的事情:
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();
}
HTH
关于java - HBase 中的组合键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18655256/