我是HBase的新手,必须使用组合键作为主键。请告诉我
How to make composite-key in hbase?
And How to search a record using that composite-key?发布于 2013-09-06 18:44:59
只需连接密钥的各个部分并使用它即可。没有什么特别事情。假设您有一个customer表,并且希望有一个由CustID和时间戳组成的行键。然后,您希望获取特定用户的所有结果,而不考虑时间戳。你会这样做:
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
发布于 2014-02-12 23:20:56
对我来说,最好的方法就是有序地使用。它是一个库,可以帮助您创建组合键并对它们进行排序。Link to Orderly。您也可以在设置开始和停止行键的扫描操作中使用它。
https://stackoverflow.com/questions/18655256
复制相似问题