个人技术分享

Hive在默认情况下是没有办法进行update、delete的,
但是我们可以通过以下方式实现 行级别&批量级 的delete、update

行级别delete、update

• 只支持ORC存储格式
• 表必须分桶
• 更新指定配置文件
1.创建ORC存储格式、且分桶的表
CREATE TABLE table_name (
id int,
name string
)CLUSTERED BY (id) INTO 2 BUCKETS STORED AS ORC
TBLPROPERTIES ("transactional"="true");
--transactional:交易型的;事务性的;事务处理的
2.修改添加hive中的hive-site.xml配置文件
<property>
    <name>hive.support.concurrency</name>
    <value>true</value>
</property>
<property>
    <name>hive.enforce.bucketing</name>
    <value>true</value>
</property>
<property>
    <name>hive.exec.dynamic.partition.mode</name>
    <value>nonstrict</value>
</property>
<property>
    <name>hive.txn.manager</name>
    <value>org.apache.hadoop.hive.ql.lockmgr.DbTxnManager</value>
</property>
<property>
    <name>hive.compactor.initiator.on</name>
    <value>true</value>
</property>

批量级delete、update

分区表

-- delete 操作 就是删除指定分区
alter table drop if exists partition(xxx);

-- update 操作 就是先删除再加载
insert overwrite table xxx partition(xxx);
--或者
load data inpath 'xxx' overwrite into table partition(xxx);

非分区表

-- delete 操作 将除了“条件”外的数据覆盖本表
insert overwrite table xxx select * from xxx where dt != '20221010';

-- update 操作 依旧是先执行上述的删除操作再进行插入操作
insert overwrite table xxx select * from xxx where dt != '20221010';
insert into table xxx values(......);
--或者
load data inpath into table xxx;