import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; /**
* 修改数据实现原子性
*
* 重点:
* checkAndMutate
*/
public class UpdateDataWithAtomic {
public static void main(String[] args) throws IOException{
Configuration configuration = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(configuration);
//建立表的连接
Table table = connection.getTable(TableName.valueOf("testtable"));
//获取put实例
Put put = new Put(Bytes.toBytes("10086"));
put.addColumn(Bytes.toBytes("colfam1"),Bytes.toBytes("qual7"),4,Bytes.toBytes("UpdateDataWithAtomic"));
put.addColumn(Bytes.toBytes("colfam1"),Bytes.toBytes("qual8"),4,Bytes.toBytes("UpdateDataWithAtomicTest"));
//删除
Delete delete = new Delete(Bytes.toBytes("10086"));
delete.addColumn(Bytes.toBytes("colfam1"),Bytes.toBytes("qual1"));
//更新实例
RowMutations mutations = new RowMutations(Bytes.toBytes("10086"));
mutations.add(put);
mutations.add(delete);
//Mutate 1 successful: false
//checkAndMutate (行键,列族,列分隔) =>比对操作<= (期望值) ====> T mutations F not mutations
//(china mobile 1)<(val1) 为假没有进行更新
boolean res1 = table.checkAndMutate(Bytes.toBytes("10086"), Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"), CompareFilter.CompareOp.LESS, Bytes.toBytes("val1"), mutations);
System.out.println("Mutate 1 successful: " + res1);
//这儿插入了一行数据会导致10086-colfam1-qual1的val2比期望的val1大
Put put2 = new Put(Bytes.toBytes("10086"));
put2.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"), 4, Bytes.toBytes("val2"));
table.put(put2);
//Mutate 2 successful: true
//(val2)>(val1) 为真 进行了跟新
boolean res2 = table.checkAndMutate(Bytes.toBytes("10086"), Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"), CompareFilter.CompareOp.LESS, Bytes.toBytes("val1"), mutations);
System.out.println("Mutate 2 successful: " + res2);
}
}
//olddata
/**
10086 column=colfam1:qual1, timestamp=4, value=china mobile 1
10086 column=colfam1:qual4, timestamp=4, value=china mobile 4
*/
//newdata
/**
10086 column=colfam1:qual4, timestamp=4, value=china mobile 4
10086 column=colfam1:qual7, timestamp=4, value=UpdateDataWithAtomic
10086 column=colfam1:qual8, timestamp=4, value=UpdateDataWithAtomicTest
**/

最新文章

  1. User space 与 Kernel space
  2. Linux Cmd Tool 系列之—script &amp; scriptreplay
  3. 20145227&amp;20145201 《信息安全系统设计基础》实验三
  4. 2014鸟人Birdman中文字幕文件下载
  5. Struts2 简介
  6. word第一讲(0723)
  7. modelsim10.0C编译ISE14.7的xilinx库(xilinx ip核)
  8. JS面试题及答案总结
  9. [Python陷阱]os.system调用shell脚本获取返回值
  10. ORACLE的客户端如何连接到数据库
  11. [Linked List]Linked List Cycle,Linked List Cycle II
  12. 设置git账号并生成新的ssh(切换电脑用户之后)
  13. Spring同mybatis整合讲义(事物)
  14. Linux中printk()实例
  15. centos iftop iotop htop
  16. JavaScript &amp; Dom 之 基本语法
  17. 运行startup.bat的启动过程
  18. Vue 路由详解
  19. (转)Sql Server之旅——第八站 复合索引和include索引到底有多大区别?
  20. [HDU4532]湫秋系列故事——安排座位

热门文章

  1. phpcms 03
  2. 2016CCPC东北地区大学生程序设计竞赛 1001 HDU5922
  3. Android启动Activity的两种方式与四种启动模式
  4. 回车和换行在linux下和windows下
  5. SqlSever基础 isnull 将null替换成指定字符串
  6. 电量检测芯片BQ27510使用心得
  7. Spring 框架 详解 (三)-----IOC装配Bean
  8. Android-表格布局 计算器 修改版
  9. 【mark】linux 终端命令行下的快捷键(自己已验证所有)
  10. jquery之html(),text()方法详解