【问题标题】:Does Get HBase API retrieves the columnsfamily and column name if we specify the value?如果我们指定值,Get HBase API 是否会检索列族和列名?
【发布时间】:2015-04-04 14:00:36
【问题描述】:

我有一个表,可以说它的名字是“SampleTab”,有一个名为“ColumnFam1”的列族和一个名为“1”的列,一个值为“col1value”的值

我已经编写了这段代码并尝试通过传递列值来获得输出,我得到的输出为

/04/04 06:50:08 INFO zookeeper.ZooKeeper:客户端环境:java.io.tmpdir=/tmp 15/04/04 06:50:08 INFO zookeeper.ZooKeeper:客户端环境:java.compiler= 15/04/04 06:50:08 INFO zookeeper.ZooKeeper:客户端环境:os.name=Linux 15/04/04 06:50:08 INFO zookeeper.ZooKeeper:客户端环境:os.arch=i386 15/04/04 06:50:08 INFO zookeeper.ZooKeeper:客户端环境:os.version=2.6.18-238.9.1.el5 15/04/04 06:50:08 INFO zookeeper.ZooKeeper:客户端环境:user.name=training 15/04/04 06:50:08 INFO zookeeper.ZooKeeper:客户端环境:user.home=/home/training 15/04/04 06:50:08 INFO zookeeper.ZooKeeper:客户端环境:user.dir=/home/training 15/04/04 06:50:08 INFO zookeeper.ZooKeeper:启动客户端连接,connectString=localhost:2181 sessionTimeout=180000 watcher=hconnection 15/04/04 06:50:09 INFO zookeeper.ClientCnxn:打开与服务器 localhost/127.0.0.1:2181 的套接字连接 15/04/04 06:50:09 INFO zookeeper.ClientCnxn:与 localhost/127.0.0.1:2181 建立的套接字连接,正在启动会话 15/04/04 06:50:09 INFO zookeeper.ClientCnxn:服务器 localhost/127.0.0.1:2181 上的会话建立完成,sessionid = 0x14c84ae2fb30005,协商超时 = 40000 15/04/04 06:50:11 INFO zookeeper.ZooKeeper:启动客户端连接,connectString=localhost:2181 sessionTimeout=180000 watcher=hconnection 15/04/04 06:50:11 INFO zookeeper.ClientCnxn:打开与服务器 localhost/127.0.0.1:2181 的套接字连接 15/04/04 06:50:11 INFO zookeeper.ClientCnxn:与 localhost/127.0.0.1:2181 建立的套接字连接,正在启动会话 15/04/04 06:50:12 INFO zookeeper.ClientCnxn:在服务器 localhost/127.0.0.1:2181 上完成会话建立,sessionid = 0x14c84ae2fb30006,协商超时 = 40000 获取:OUTPUT keyvalues=NONE

我要获取的部分代码如下:

HTable table = new HTable(config, tablename);
        byte [] row1 = Bytes.toBytes("KeyIn");
        Put p1 = new Put(row1);
        byte [] databytes = Bytes.toBytes("ColumnFam1");
        p1.add(databytes, Bytes.toBytes("1"), Bytes.toBytes("col1value"));
        table.put(p1);
        Get g = new Get(Bytes.toBytes("col1value"));
        g.addColumn(Bytes.toBytes("ColumnFam1"), Bytes.toBytes("1"));
        Result result = table.get(g);
        System.out.println("Get:   OUTPUT     " + result);

我有任何方法可以通过在 Get 中传递值来获取列族名称和列名 我也试过不使用 g.addColumn 行..它仍然给出 NONE

【问题讨论】:

    标签: hbase


    【解决方案1】:

    Get g = new Get(Bytes.toBytes("col1value")); 不起作用,因为该行不存在。您必须提供刚刚插入到 get 的行键:

    HTable table = new HTable(config, tablename);
    byte [] row1 = Bytes.toBytes("KeyIn");
    Put p1 = new Put(row1);
    byte [] databytes = Bytes.toBytes("ColumnFam1");
    p1.add(databytes, Bytes.toBytes("1"), Bytes.toBytes("col1value"));
    table.put(p1);
    Get g = new Get(row1);
    //g.addColumn(Bytes.toBytes("ColumnFam1"), Bytes.toBytes("1")); // Not needed if you want to retrieve all families/columns
    Result result = table.get(g);
    System.out.println("Get:   OUTPUT     " + result);
    

    如果您知道列的名称(如其所示),则可以直接访问该值:

    if (result.containsColumn(databytes, Bytes.toBytes("1"))) {
        System.out.println("Value: " + Bytes.toStringBinary(result.getColumnLatest(databytes, Bytes.toBytes("1"))));
    }
    

    或者,如果您想遍历所有检索到的列:

    Result result = table.get(get);
    if (result!=null) {
        System.out.println(result);
        Set<Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>>> entries = result.getMap().entrySet();
        for(Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> familyEntry: entries) {
            byte[] family = familyEntry.getKey();
            for(Entry<byte[], NavigableMap<Long, byte[]>> columnEntry: familyEntry.getValue().entrySet()) {
                byte[] column = columnEntry.getKey();
                System.out.println("Found column "+Bytes.toStringBinary(family)+":"+Bytes.toStringBinary(column));
                if (columnEntry.getValue().size()>0) {
                    Entry<Long, byte[]> valueEntry = columnEntry.getValue().firstEntry();
                    System.out.println("  Found value "+Bytes.toStringBinary(valueEntry.getValue())+" with timestamp "+valueEntry.getKey());    
                }
            }
        }
    }
    
    • 为简单起见,我只读取每列的最新值,但如果需要所有版本,您可以迭代 columnEntry.getValue().entrySet() *

    【讨论】:

      【解决方案2】:

      我赞同鲁本的回答。万一您不知道 RowKey,您可以使用ValueFilter 并通过以下方式创建Scan-

      Scan scan = new Scan();
      byte[] value = Bytes.toBytes("col1value")
      Filter filter = new ValueFilter(CompareOp.EQUAL,new BinaryComparator(value));
      scan.setFilter(filter);
      ResultScanner scanner = table.getScanner(scan);
      

      然后您使用scanner 扫描结果。 但是,这可能太昂贵了,因为您正在执行完整的扫描。您也可以使用带有Get 的过滤器。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-15
        • 1970-01-01
        • 1970-01-01
        • 2018-07-27
        • 1970-01-01
        相关资源
        最近更新 更多