Hive Transaction(Hive 事务管理)

2023-11-17

Hive 事务在 Hive 3 得到增强。

hive-site.xml 配置

<property>
   <name>hive.txn.manager</name>
   <value>org.apache.hadoop.hive.ql.lockmgr.DbTxnManager</value>
   <description>
     Set to org.apache.hadoop.hive.ql.lockmgr.DbTxnManager as part of turning on Hive
     transactions, which also requires appropriate settings for hive.compactor.initiator.on,
     hive.compactor.worker.threads, hive.support.concurrency (true),
     and hive.exec.dynamic.partition.mode (nonstrict).
     The default DummyTxnManager replicates pre-Hive-0.13 behavior and provides
     no transactions.
   </description>
 </property>
 <property>
   <name>hive.support.concurrency</name>
   <value>true</value>
 </property>
 <property>
   <name>hive.compactor.initiator.on</name>
   <value>true</value>
   <description>
     Whether to run the initiator and cleaner threads on this metastore instance or not.
     Set this to true on one instance of the Thrift metastore service as part of turning
     on Hive transactions. For a complete list of parameters required for turning on
     transactions, see hive.txn.manager.
   </description>
 </property>
 <property>
   <name>hive.compactor.worker.threads</name>
   <value>2</value>
   <description>
     How many compactor worker threads to run on this metastore instance. Set this to a
     positive number on one or more instances of the Thrift metastore service as part of
     turning on Hive transactions. For a complete list of parameters required for turning
     on transactions, see hive.txn.manager.
     Worker threads spawn MapReduce jobs to do compactions. They do not do the compactions
     themselves. Increasing the number of worker threads will decrease the time it takes
     tables or partitions to be compacted once they are determined to need compaction.
     It will also increase the background load on the Hadoop cluster as more MapReduce jobs
     will be running in the background.
   </description>
 </property>
 <property>
   <name>hive.exec.dynamic.partition.mode</name>
   <value>nonstrict</value>
   <description>
     In strict mode, the user must specify at least one static partition
     in case the user accidentally overwrites all partitions.
     In nonstrict mode all partitions are allowed to be dynamic.
   </description>
 </property>

Hive 创建的表自动是事务表的配置。

<property>
   <name>metastore.strict.managed.tables</name>
   <value>false</value>
   <description>
     Whether strict managed tables mode is enabled. With this mode enabled, only transactional tables (both full and insert-only) are allowed to be created as managed tables
   </description>
 </property>
 <property>
   <name>hive.create.as.insert.only</name>
   <value>false</value>
   <description>
     Whether the eligible tables should be created as ACID insert-only by default. Does not apply to external tables, the ones using storage handlers, etc.
   </description>
 </property>
  <property>
   <name>metastore.create.as.acid</name>
   <value>false</value>
   <description>
     Whether the eligible tables should be created as full ACID by default. Does not apply to external tables, the ones using storage handlers, etc.
   </description>
 </property>

测试

创建事务表

create table t1(c1 int,c2 int) stored as orc tblproperties('transactional'='true');

执行以下操作。执行之后,可以看到每个操作在表的目录下生成相应的 delta 目录。

insert into t1 values(1,1),(2,2),(3,3);
insert into t1 values(4,4);
insert into t1 values(5,5);
insert into t1 values(6,6);
insert into t1 values(7,7);
insert into t1 values(8,8);
insert into t1 values(9,9);
insert into t1 values(10,10);
insert into t1 values(11,11);
insert into t1 values(12,12);
insert into t1 values(13,13);

delete from t1 where c1=13;
insert into t1 values(13,14);
delete from t1 where c1=13;
insert into t1 values(13,15);

insert_only 事务

insert_only 事务不要求表必须是 orc 格式,可以是任何格式,如 parquet。
t2 表仅支持 insert,不支持 delete, update。
insert 语句成功, delete 失败。

create table t2(c1 int,c2 int) stored as orc tblproperties('transactional'='true','transactional_properties'='insert_only');

insert into t2 values(1,1),(2,2),(3,3);
delete from t2 where c1=3;

修改现有表支持事务

全部事务支持

表 t3 从创建的时候没有支持事务,用 alter table 支持事务,之后可以执行 insert, delete 操作。

create table t3(c1 int,c2 int) stored as orc;
alter table t3 set  tblproperties('transactional'='true','transactional_properties'='default');
insert into t3 values(1,1),(2,2),(3,3);
delete from t3 where c1=3;

insert_only 事务支持

create table t4(c1 int,c2 int) stored as orc;
alter table t4 set  tblproperties('transactional'='true','transactional_properties'='insert_only');
insert into t4 values(1,1),(2,2),(3,3);

Hive 官方文档https://cwiki.apache.org/confluence/display/Hive/Hive+Transactions

常见问题

问题原因总结:

  1. 一个表,可以同时运行多个 SQL 进行 insert 和 select 操作。但是不允许同时运行多个 SQL 进行 UPDATE, DELETE 或者 MERGE操作, 因为他们都会生成 delete_delta 目录。如两个 SQL,它们同时 update 不同的记录也会报错。
  2. SQL 解析阶段获取 一个表的 write_id list,但是在执行之前获取锁,在执行之后释放锁,导致SQL 解析和执行之前会出现并发错误。

问题列表

  1. 作业报错
    如果在执行的时候,发生了 compaction,compaction 之后删除了原来的文件,导致抛出 FileNotFoundException: File does not exist:.
  2. 2 个会话同时执行 insert overwrite 抛出 LockException
  • 创建表
create table t1(c1 int) stored as orc tblproperties('transactional'='true');

在两个窗口里分别用 beeline 连接 hiveserver。

  • 在会话1,执行以下命令,函数 timesleep 会 sleep 10秒钟,返回 10001。
insert overwrite table t1 select default.timesleep(10000);
  • 在会话2,执行以下命令,会话2 的命令在会话1之前执行完毕。
insert overwrite table t1 select 1;

会话1抛出以下异常

ERROR : FAILED: Hive Internal Error: org.apache.hadoop.hive.ql.lockmgr.LockException(Transaction manager has aborted the transaction txnid:126.  Reason: Aborting [txnid:126,128] due to a write conflict on test/t1 committed by [txnid:127,127] u/u)
org.apache.hadoop.hive.ql.lockmgr.LockException: Transaction manager has aborted the transaction txnid:126.  Reason: Aborting [txnid:126,128] due to a write conflict on test/t1 committed by [txnid:127,127] u/u
	at org.apache.hadoop.hive.ql.lockmgr.DbTxnManager.commitTxn(DbTxnManager.java:670)

由于函数在编译阶段给出具体值。

select * from t1;
+--------+
| t1.c1  |
+--------+
| 1      |
| 2      |
| 3      |
+--------+

以下两个语句:
会话1:

insert overwrite table t2 select c1,default.timesleep(10000 * c1) from t1;

会话2:

insert overwrite table t2 select c1,default.timesleep(10 * c1) from t1;

会话2 会在 commit 的时候失败。

ERROR : FAILED: Hive Internal Error: org.apache.hadoop.hive.ql.lockmgr.LockException(Transaction manager has aborted the transaction txnid:163.  Reason: Aborting [txnid:163,163] due to a write conflict on test/t2 committed by [txnid:162,163] u/u)
org.apache.hadoop.hive.ql.lockmgr.LockException: Transaction manager has aborted the transaction txnid:163.  Reason: Aborting [txnid:163,163] due to a write conflict on test/t2 committed by [txnid:162,163] u/u
	at org.apache.hadoop.hive.ql.lockmgr.DbTxnManager.commitTxn(DbTxnManager.java:670)

  1. 在执行 insert overwrite 时,在另外的会话执行drop table,导致第 1 个会话抛出表找不到异常。示例如下:
0: jdbc:hive2://localhost:10000/default> insert overwrite table t1 select default.timesleep(10000);
Error: Error while compiling statement: FAILED: SemanticException [Error 10001]: Line 1:23 Table not found 't1' (state=42S02,code=10001)

  • show transactions 可以显示 aborted 的 transaction。
show transactions;
+-----------------+--------------------+----------------+----------------------+-------------+------------------------+
|      txnid      |       state        |  startedtime   |  lastheartbeattime   |    user     |          host          |
+-----------------+--------------------+----------------+----------------------+-------------+------------------------+
| Transaction ID  | Transaction State  | Started Time   | Last Heartbeat Time  | User        | Hostname               |
| 126             | ABORTED            | 1646189665000  | 1646189665000        | houzhizhen  | localhost.localdomain  |
| 130             | OPEN               | 1646189960000  | 1646189973000        | houzhizhen  | localhost.localdomain  |
+-----------------+--------------------+----------------+----------------------+-------------+------------------------+

  1. 两个会话同时 insert 不会有冲突。

参考资料:
官方文档:Hive+Transactions
PPT Transactional Operations in Apache Hive:Present and Future

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Hive Transaction(Hive 事务管理) 的相关文章

随机推荐

  • arcpy导入报错 “ImportRrror: No module named arcpy”

    在使用ArcGIS自带的Python IDLE处理数据的时候 导入arcpy报错 ImportError No module named arcpy 我遍历了各解决方法依然无法成功导入arcpy 后经过查询 探索 通过如下方法得以成功解决
  • aoj1303

    继续python系列 python能够自动推断类型这个太好用了 根本不用声明类型 自己根据运行情况推断出所用的类型 所以在定义函数的时候根本不用声明参数的类型 下面这个题目aoj1303 求2的指数 如下 def gethex a li w
  • 关于飞书的告警通知,这里有个更好的办法

    飞书 是字节跳动于2016年自研的新一代一站式协作平台 是保障字节跳动全球五万人高效协作的办公工具 飞书将即时沟通 日历 云文档 云盘和工作台深度整合 通过开放兼容的平台 让成员在一处即可实现高效的沟通和流畅的协作 全方位提升企业效率 20
  • Git 使用

    Git 一 Git基础 1 Git介绍 Git是目前世界上最先进的分布式版本控制系统 2 Git与Github 2 1 两者区别 Git是一个分布式版本控制系统 简单的说其就是一个软件 用于记录一个或若干文件内容变化 以便将来查阅特定版本修
  • 模板类、模板函数的模板类型显式实例化及其用途(转载)

    转载自 C 11模板隐式实例化 显式实例化声明 定义 简单易懂 云飞扬 Dylan的博客 CSDN博客 模板隐式实例化 1 隐式实例化 在代码中实际使用模板类构造对象或者调用模板函数时 编译器会根据调用者传给模板的实参进行模板类型推导然后对
  • 【LAMMPS系列】LAMMPS软件安装资料包

    大家好 我是粥粥 LAMMPS 是一种经典的分子动力学代码 专注于材料建模 它是大型原子 分子大规模并行模拟器的首字母缩略词 LAMMPS 具有固态材料 金属 半导体 和软物质 生物分子 聚合物 以及粗粒或中等系统的势函数 它可用于模拟原子
  • 自定义多数据源JDBC连接池

    背景 公司需要对各个客户的数据库进行统一管理 故涉及到对多个不同数据库进行连接 传统的数据库连接池无法满足需求 故结合网上的自定义数据库连接池 进行的改进 代码如下 注意 由于代码处于公司环境 有直接使用肯定是会有报错 相信这种简单的修补是
  • android Stopwatch实例

    Stopwatch 实例 package net baisoft stopwatch import java util ArrayList import java util Date import java util HashMap imp
  • electron vue 打开新窗口

    1 主进程 background js文件 const winURL process env NODE ENV development http localhost 8080 file dirname index html 事件名 open
  • 网页设计期末大作业-景点旅游网站(含导航栏,轮播图,样式精美)

    景点旅游网站 资源链接在文末 页设计期末结课的作业 样式很精美 链接基本正常 详细情况入下图所示 资源下载链接 https download csdn net download weixin 43474701 85514120
  • AIX显示版本的最高全包含版本原则

    复杂度2 5 机密度4 5 最后更新2021 05 02 专题其它章节说过AIX对所有程序包管理会检验完整性 并且内置了一个验证列表 包含其所能识别的最新版应当包含的各个程序包的版本 如果当前安装的TL Patch不完整 则只会显示可以实现
  • CSS transform属性的简单应用——双开门动画效果

    1 效果演示 CSS transform属性有许多效果 平移 旋转 缩放等 这里简单应用平移效果 实现双开门动画 以下为效果图 2 设计思路 设置一张居中的需要隐藏的底图 设置封面图 平分成左右两部分 鼠标悬浮在封面图上 触发 开门 效果
  • 在C/C++代码中使用SSE等指令集的指令(4)SSE指令集Intrinsic函数使用

    在http blog csdn net gengshenghong article details 7008682里面列举了一些手册 其中Intel Intrinsic Guide可以查询到所有的Intrinsic函数 对应的汇编指令以及如
  • centos7的安装和创建用户

    1 centos7 2的安装 打开安装包之后解压 然后双击 进入下面的界面 选择语言 点击下一步 2 然后来到了配置页面 可以配置时间 选择中国的时区 3 其他的选择默认就好 重要的是选择安装类型和磁盘分区 4 选择安装类型 一般默认是mi
  • npm开发微信小程序--使用vantui 详解干货

    更新微信开发者工具创建项目 1 创建项目 放在一个合适的文件夹中 没有APPID时 请点击测试号 或去注册一个 2 进入项目的根目录 npm init 一路回车 要先npm init 初始化项目 否则会报错 官方文档中没有提到的东东 里面有
  • 爬虫实战——58同城租房数据爬取

    背景 自己本人在暑期时自学了python 还在中国大学mooc上学习了一些爬虫相关的知识 对requests库 re库以及BeautifulSoup库有了一定的了解 但是没有过爬虫方面的实战 刚好家人有这方面需求 就对58同城上的租房数据进
  • 简单工厂模式

    提示 文章写完后 目录可以自动生成 如何生成可参考右边的帮助文档 文章目录 前言 一 创建头文件 二 创建 c文件 1 cat c 2 dog c 3 person c 三 创建main c 四 运行结果 总结 前言 工厂模式 常用的设计模
  • 《Keras深度学习:入门、实战与进阶》CIFAR-10图像识别

    本文摘自 Keras深度学习 入门 实战与进阶 https item jd com 10038325202263 html 这个数据集由Alex Krizhevsky Vinod Nair和Geoffrey Hinton收集整理 共包含了6
  • cas TicketValidationException 未能够识别出目标 ‘ST-1-UxVA37oEE-qN-S0NNZclYXsXxFQSD-20200510PZSQ‘票根

    原因 超时了 解决 去掉debug再测试一下
  • Hive Transaction(Hive 事务管理)

    Hive 事务在 Hive 3 得到增强 hive site xml 配置