Hbase atomic operation

1. Use check and put to guarantee the atomicity of the operation. That is, before executing put, check whether the value is consistent with the provided value. If the check passes, execute put, otherwise give up. If the value of the field does not exist before putting, just set the value to null.

	@Test
	public  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");
		else
			System.out.println("update failure");
		table.close();
	}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326425764&siteId=291194637