CAS

checkAndPut

其中,put方法的最后一个参数是你需要录入的数据的put对象;
value是与服务端check的预期值,只有服务器端对应rowkey的数据与你预期的值相同时,你的put操作才能被提交的服务端。

  1. Table table = conn.getTable(TableName.valueOf("t_user_info"));
  2. Put put = new Put(Bytes.toBytes("liu_sh_01"));
  3. put.add(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("jdxia"));
  4. //这个值是否等于helloworld,等于的话就执行put
  5. boolean b = table.checkAndPut(Bytes.toBytes("liu_sh_01"), Bytes.toBytes("base_info"),
  6. Bytes.toBytes("username"), Bytes.toBytes("helloworld"), put);
  7. //是否有值
  8. // boolean b = table.checkAndPut(Bytes.toBytes("liu_sh_01"), Bytes.toBytes("base_info"),
  9. // Bytes.toBytes("username"), null, put);
  10. //看看是否执行了put
  11. System.out.println(b);
  12. table.close();

checkAndDelete

  1. Table table = conn.getTable(TableName.valueOf("t_user_info"));
  2. Delete delete = new Delete(Bytes.toBytes("liu_sh_01"));
  3. delete.deleteColumns(Bytes.toBytes("base_info"), Bytes.toBytes("username"));
  4. //这个值是否等于helloworld,等于的话就执行
  5. boolean b = table.checkAndDelete(Bytes.toBytes("liu_sh_01"), Bytes.toBytes("base_info"),
  6. Bytes.toBytes("username"), Bytes.toBytes("jdxia"), delete);
  7. //看看是否执行了
  8. System.out.println(b);
  9. table.close();