如果我们指定值,Get HBase API 是否检索列族和列名?

Does Get HBase API retrieves the columnsfamily and column name if we specify the value?

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

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

/04/04 06:50:08 信息 zookeeper.ZooKeeper:客户端 environment:java.io.tmpdir=/tmp 15/04/04 06:50:08 信息 zookeeper.ZooKeeper:客户端 environment:java.compiler= 15/04/04 06:50:08 信息 zookeeper.ZooKeeper:客户 environment:os.name=Linux 15/04/04 06:50:08 信息 zookeeper.ZooKeeper:客户端 environment:os.arch=i386 15/04/04 06:50:08 信息 zookeeper.ZooKeeper:客户端 environment:os.version=2.6.18-238.9.1.el5 15/04/04 06:50:08 信息 zookeeper.ZooKeeper:客户 environment:user.name=training 15/04/04 06:50:08 信息 zookeeper.ZooKeeper:客户 environment:user.home=/home/training 15/04/04 06:50:08 信息 zookeeper.ZooKeeper:客户 environment:user.dir=/home/training 15/04/04 06:50:08 信息 zookeeper.ZooKeeper:正在启动客户端连接,connectString=localhost:2181 sessionTimeout=180000 watcher=hconnection 15/04/04 06:50:09 信息 zookeeper.ClientCnxn:打开与服务器 localhost/127.0.0.1:2181 的套接字连接 15/04/04 06:50:09 信息 zookeeper.ClientCnxn:已建立到 localhost/127.0.0.1:2181 的套接字连接,正在启动会话 15/04/04 06:50:09 信息 zookeeper.ClientCnxn:服务器上的会话建立完成 localhost/127.0.0.1:2181,sessionid = 0x14c84ae2fb30005,协商超时 = 40000 15/04/04 06:50:11 信息 zookeeper.ZooKeeper:正在启动客户端连接,connectString=localhost:2181 sessionTimeout=180000 watcher=hconnection 15/04/04 06:50:11 信息 zookeeper.ClientCnxn:打开与服务器 localhost/127.0.0.1:2181 的套接字连接 15/04/04 06:50:11 信息 zookeeper.ClientCnxn:建立到 localhost/127.0.0.1:2181 的套接字连接,正在启动会话 15/04/04 06:50:12 信息 zookeeper.ClientCnxn:在服务器 localhost/127.0.0.1:2181 上完成会话建立,sessionid = 0x14c84ae2fb30006,协商超时 = 40000 获取:输出键值=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

Get g = new Get(Bytes.toBytes("col1value")); 将不起作用,因为该行不存在。您必须提供刚刚插入的 rowkey 到 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() *

我支持 Rubén 的回答。以防万一您不知道 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 一起使用。