hbase CheckAndPut example – java client api

Checkandput allows us to compare and then set the value of a row . Using the checkandput we can only update if another value is not already present. This is achieved by setting the value parameter to null . In that case, the operation would succeed when the specified column is nonexistent. If we specify the column value then a check would be made to verify whether the row is present with the value and the put will be successful only if its already existing.

Below is an example


import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class CheckAndPut {

public static void main(String[] args) throws IOException {

Configuration conf = HBaseConfiguration.create();

Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("test"));

try {

Put put1 = new Put(Bytes.toBytes("row1"));

put1.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("qual1"),
Bytes.toBytes("ValueOneForPut1Qual1"));

table.put(put1);

// Returns False as the row is already existing

boolean isSuccessfull = table.checkAndPut(Bytes.toBytes("row1"),
Bytes.toBytes("cf"), Bytes.toBytes("qual1"), null, put1);

System.out.println(isSuccessfull);

// Returns true as the row is not existing

Put put2 = new Put(Bytes.toBytes("row2"));

put2.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("qual1"),
Bytes.toBytes("ValueOneForPut2Qual1"));

boolean isSuccessfull2 = table.checkAndPut(Bytes.toBytes("row2"),
Bytes.toBytes("cf"), Bytes.toBytes("new-qualifier"), null,
put2);

System.out.println(isSuccessfull2);

// Returns true as the row is existing as of now

boolean isSuccessfull3 = table.checkAndPut(Bytes.toBytes("row1"),
Bytes.toBytes("cf"), Bytes.toBytes("qual1"),
Bytes.toBytes("ValueOneForPut1Qual1"), put1);

System.out.println(isSuccessfull3);

// Returns false as the row is not existing as of now

Put put3 = new Put(Bytes.toBytes("row3"));

put3.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("qual1"),
Bytes.toBytes("ValueOneForPut2Qual1"));

boolean isSuccessfull4 = table.checkAndPut(Bytes.toBytes("row3"),
Bytes.toBytes("cf"), Bytes.toBytes("qual1"),
Bytes.toBytes("ValueOneForPut1Qual123"), put3);

System.out.println(isSuccessfull4);

} finally {
table.close();
connection.close();
}

}

}

The above code prints false,true,true and false . The first checkandput prints false as we have added the put1 and we are trying to add the same again so here the check fails so the put command is not executed. The second checkpoint prints true as the column is not present and put executes successfully. The Third one is the second version of the checkandput command where we are passing the value and here the check passes if the data is already present and this prints true as the data is already existing as the check is successful. The last operation prints false as the check fails as we are trying to put a column value which does not exits.