Search code examples
hbase

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


I have a Table lets say its name is "SampleTab" having a columnfamily named "ColumnFam1" and column named "1" an value as "col1value"

I have written this code and trying to get a output by passing the column value I am getting output as

/04/04 06:50:08 INFO zookeeper.ZooKeeper: Client environment:java.io.tmpdir=/tmp 15/04/04 06:50:08 INFO zookeeper.ZooKeeper: Client environment:java.compiler= 15/04/04 06:50:08 INFO zookeeper.ZooKeeper: Client environment:os.name=Linux 15/04/04 06:50:08 INFO zookeeper.ZooKeeper: Client environment:os.arch=i386 15/04/04 06:50:08 INFO zookeeper.ZooKeeper: Client environment:os.version=2.6.18-238.9.1.el5 15/04/04 06:50:08 INFO zookeeper.ZooKeeper: Client environment:user.name=training 15/04/04 06:50:08 INFO zookeeper.ZooKeeper: Client environment:user.home=/home/training 15/04/04 06:50:08 INFO zookeeper.ZooKeeper: Client environment:user.dir=/home/training 15/04/04 06:50:08 INFO zookeeper.ZooKeeper: Initiating client connection, connectString=localhost:2181 sessionTimeout=180000 watcher=hconnection 15/04/04 06:50:09 INFO zookeeper.ClientCnxn: Opening socket connection to server localhost/127.0.0.1:2181 15/04/04 06:50:09 INFO zookeeper.ClientCnxn: Socket connection established to localhost/127.0.0.1:2181, initiating session 15/04/04 06:50:09 INFO zookeeper.ClientCnxn: Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x14c84ae2fb30005, negotiated timeout = 40000 15/04/04 06:50:11 INFO zookeeper.ZooKeeper: Initiating client connection, connectString=localhost:2181 sessionTimeout=180000 watcher=hconnection 15/04/04 06:50:11 INFO zookeeper.ClientCnxn: Opening socket connection to server localhost/127.0.0.1:2181 15/04/04 06:50:11 INFO zookeeper.ClientCnxn: Socket connection established to localhost/127.0.0.1:2181, initiating session 15/04/04 06:50:12 INFO zookeeper.ClientCnxn: Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x14c84ae2fb30006, negotiated timeout = 40000 Get: OUTPUT keyvalues=NONE

Part of my code to Get is below :

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);

I there any way to get columnfamily name and column name by passing the value in Get I have also tried without using the g.addColumn line .. it stills gives NONE


Solution

  • Get g = new Get(Bytes.toBytes("col1value")); is not going to work because that row doesn't exist. You have to provide the rowkey you've just inserted to the 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 you know the name of the column (as it appears) you can just access the value directly:

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

    Or, in case you want to iterate through all retrieved columns:

    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());    
                }
            }
        }
    }
    
    • For simplicity, I only read the most recent value for each column but you can iterate columnEntry.getValue().entrySet() if you want all versions *