Hbase原子性操作

来源:互联网 发布:手机淘宝首页海报尺寸 编辑:程序博客网 时间:2024/05/18 21:47

1. 使用检查写(check and put)可以保证操作的原子性。即执行put前先检查数值是否与提供的value一致,如果检查通过就执行put,否则就放弃。如果需要put前该字段值不存在,将value设置成null即可。

@Testpublic  void testCheckPut() throws Exception{Table table = conn.getTable(TableName.valueOf("t1"));Put put = new Put(Bytes.toBytes("row2"));put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"), Bytes.toBytes("terry2"));put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("job"), Bytes.toBytes("manager2"));boolean res =  table.checkAndPut(Bytes.toBytes("row2"), Bytes.toBytes("cf"), Bytes.toBytes("name"), Bytes.toBytes("terry"), put);if(res)System.out.println("update success");elseSystem.out.println("update failure");table.close();}