Search code examples
javahbasekey-value-codingkey-value-store

using hbase as key-value store, need to extract a value using java


I am working on using Hbase as a Key-Value store where we have a column family with one single value. The java filter gets the row in less than a second but when trying to retrieve the value takes 15 seconds. If someone could look into it and give me pointer it would be very helpful. Here is the code :-

        Scan scan1 = new Scan();
        scan1.addColumn(Bytes.toBytes("column_family"), Bytes.toBytes("column_name"));
        Filter filter1 = new RowFilter(CompareFilter.CompareOp.EQUAL,
          new BinaryComparator(Bytes.toBytes("hashvalue")));
        scan1.setFilter(filter1);
        long startTime = System.nanoTime();
        ResultScanner scanner1 = table.getScanner(scan1);
        System.out.println(scanner1.next().getColumnLatestCell(Bytes.toBytes("column_family"), Bytes.toBytes("column_name")));            
        long endTime = System.nanoTime();
        double seconds = (double)(endTime - startTime) / 1000000000.0;
        System.out.println("Scan with row key using scan: " + seconds);            
        scanner1.close();

Solution

  • You don't need to do a scan if you know the exact value of the key, you can do a Get instead, which should be super efficient.

    Result result = table.get(Bytes.toBytes("hashvalue")) 
    

    The reason why it's taking too long when you trigger next is because every call to next is an RPC call (a trip to HBase) by itself,you can use setCaching to get a certain number of rows in a single trip to HBase.