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