java - HBase 中的组合键

标签 java hadoop mapreduce cloud hbase

我是 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/

相关文章:

java |整数方法 |我需要 ELSE {不返回任何内容}

java - 验证 Spring-MVC 命令的最敏捷方法是什么(加速服务器端的验证实现)? ( Spring -MVC)

hadoop - Hbase memstore 手动刷新

java - 如何解决 MapReduce 中每个 'n' 的 TOP 'entity'?

MongoDB Map Reduce : Auto-created index name too long, 可以自定义吗?

java - 如何在给定文件夹名称的情况下创建多个目录

java - 了解 *nix 中的 RES 内存并调试内存泄漏

sql - Hadoop:创建数据库管理器时出错

java - 在 Map Reduce 中计算数据集的线性回归

hadoop - 当对多个输入文件完成 mapreduce 任务时,hadoop 如何确定映射器或输入拆分的数量?