在Hive 0.14版本后,ORC文件能够确保Hive在工作时的ACID性质被正确地得到使用,使得对数据进行更新操作成为可能,但Hive的事务仍被设计成每个事务适用于更新大批量的数据,而不建议用事务频繁地更新小批量的数据。

-- (1) 设置hive环境参数
-- 开启并发支持,支持插入、删除和更新的事务
set hive.support.concurrency=true;
-- 支持事务的表必须为分桶表
set hive.enfore.bucketing=true;
-- 开启事务需要开启动态分区非严格模式
set hive.exec.dynamic.partition.mode=nonstrict;
-- 设置事务所管理类型为org.apache.hive.ql.lockmgr.DbTxnManager
-- 原有的org.apache.hive.ql.lockmgr.DummyTxnManager不支持事务
set hive.txn.manager=org.apache.hive.ql.lockmgr.DbTxnManager;
-- 开启在相同的一个metastore实例运行初始化和清理的线程
set hive.compactor.initiator.on=true;
-- 设置每个metastore实例运行的线程数
set hive.compactor.worker.threads=1;

-- (2)创建表
create table test(
id int,
name string)
-- 必须分桶
clustered by (id) into 2 buckets
stored as orc
-- 表属性中添加支持事务
TBLPROPERTIES ('transactional'='true');

-- (3)插入数据
insert into table test values(101,'zhangsan'); 

-- (4)更新数据
update test
set name = 'lisi'
where id = 101;
posted on 2022-10-22 17:08  ADataer  阅读(532)  评论(0编辑  收藏  举报