hbase Querying HBase, Get, Put, Delete and Scans


Example

In HBase, you can use 4 types of operations

  • Get : retrieves a row
  • Put : inserts one or more row(s)
  • Delete : delete a row
  • Scan : retrieves several rows

If you simply want to retrieve a row, given its row_key you can use the Get object:

Get get = new Get(Bytes.toBytes("my_row_key"));
Table table = this.connection.getTable(TableName.valueOf("myTable"));
Result r = table.get(get);
byte[] value = r.getValue(Bytes.toBytes(columnFamily), Bytes.toBytes("myColumn"));
String valueStr = Bytes.toString(value);
System.out.println("Get result :" + valueStr);

Here we only get the value from the column we want, if you want to retrieve all the column, use the rawCell attribute from the Get object:

Get get = new Get(Bytes.toBytes(rowKey));
Table table = this.connection.getTable(TableName.valueOf(tableName));
Result r = table.get(get);
System.out.println("GET result :");
    for (Cell c : r.rawCells()) {
        System.out.println("Family : " + new String(CellUtil.cloneFamily(c)));
        System.out.println("Column Qualifier : " + new String(CellUtil.cloneQualifier(c)));
        System.out.println("Value : " + new String(CellUtil.cloneValue(c)));
        System.out.println("----------");
    }

Well, we can now retrieve data from our table, row by row, but how do we put some ? You use the Put object:

Put put = new Put("my_row_key");
put.addColumn(Bytes.toBytes("myFamily"), Bytes.toBytes("myColumn"), Bytes.toBytes("awesomeValue");
//Add as many columns as you want

Table table = connection.getTable(TableName.valueOf("myTable");
table.put(put);

NB : Table.put can also take in parameter a list of puts, which is, when you want to add a lot of rows, way more efficient than put by put.

Alright now, I can put some rows and retrieve some from my HBase, but what if I want to get several rows and if I don't know my row_keys ?

Captain here ! You can use the Scan Object:

A scan basically look all the rows and retrieve them, you can add several parameters it, such as filters and start/end row but we will see that in another example.

If you want to scan all the column values from your table, given a column use the following lines:

Table table = this.connection.getTable(TableName.valueOf("myTable"));
Scan scan = new Scan();
scan.addColumn(Bytes.toBytes("myFamily"), Bytes.toBytes("myColumn"));
ResultScanner rs = table.getScanner(scan);
    try {
        for (Result r = rs.next(); r != null; r = rs.next()) {
            byte[] value = r.getValue(Bytes.toBytes("myFamily"), Bytes.toBytes("myCOlumn"));
            String valueStr = Bytes.toString(value);
            System.out.println("row key "+new String(r.getRow()));
            System.out.println("Scan result :" + valueStr);
        }
    } finally {
        rs.close(); // always close the ResultScanner!
   }

I really want to insist on the fact that you must always close the ResultScanner (same thing than any ResultSet from a database by the way)

Nearly done ! Now let's learn how to delete a row. You have a Delete object for this:

Table table = this.connection.getTable(TableName.valueOf("myTable"));
Delete d = new Delete(Bytes.toBytes("my_weird_key"));
table.delete(d);
System.out.prinln("Row " + row_key + " from table " + tableName + " deleted");

One last thing: before executing any of the operations, always check that the table exists, or you will get an exception.

That's all for now, you can manage you data in HBase with this example.