hive DML[通俗易懂]

hive DML[通俗易懂]hive DML

大家好,又见面了,我是你们的朋友全栈君。

1、load files into tables

把文件中的数据加载到表中(表必须先建好)

语法是:

load data [local] inpath ‘filepath’ [overwrite] into table tablename [partition (partcol1=val1, partcol2=val2 …)]
load data [local] inpath ‘filepath’ [overwrite] into table tablename [partition (partcol1=val1, partcol2=val2 …)] [inputformat ‘inputformat’ serde ‘serde’] (3.0 or later)

hive3.0之前的加载操作是单纯的数据复制/移动操作,会将数据文件复制/移动到hdfs中hive表对应的目录中。到底是复制还是移动??

如 create table pokes (foo int, bar string);                                                        创建非分区表pokes

    create table invites (foo int, bar string) partitioned by (ds string);                创建分区表invites,分区列是ds

load data local inpath ‘/home/koushengrui/app/hive/examples/files/kv1.txt’ overwrite into table pokes;

load data local inpath ‘/home/koushengrui/app/hive/examples/files/kv2.txt’ overwrite into table invites partition (ds=’2018-09-01′);

load data local inpath ‘/home/koushengrui/app/hive/examples/files/kv3.txt’ overwrite into table invites partition (ds=’2018-09-02′);

filepath可以是

相对路径,例如project/data1

绝对路径,例如/user/hive/project/data1

带有schema的完整uri,例如hdfs://namenode:9000/user/hive/project/data1

加载的目标可以是表或分区。如果表是分区表,则必须通过指定所有分区列的值来指定表的特定分区。

filepath可以引用一个文件(在这种情况下hive会将文件移动到表中)或者它可以是一个目录(在这种情况下,hive会将该目录中的所有文件移动到表中)。filepath是一个目录的话,下面不能有子目录,否则会报错。

如果指定了local关键字,则load命令将在本地文件系统中查找filepath。如果指定了相对路径,则将相对于用户的当前工作目录进行解释。

如果没有指定local关键字,则hive将使用filepath的完整uri(如果指定了一个),或者将应用以下规则:

①如果未指定schema,则hive将使用hadoop的hadoop-env.sh中fs.default.name(有时也用fs.defaultFS)属性的值来指定namenode uri。

②如果filepath不是绝对路径,则hive将相对于/user/<username>解释它。

如果使用overwrite关键字,则将删除目标表(或分区)的内容,并替换为filepath所引用的文件。

注意表的列分隔符要和文件数据的列分隔符对应,默认都是row format delimited fields terminated by ‘\001′(亦即^A,Ctrl+A)。如果不对应的话,load时不会报错,但是查数据的时候,所有记录、所有字段值全都是null

hive3.0以后支持其他加载操作,因为hive在内部将加载重写为insert as select。因为本次研究的是hive2.3.3版本,故这里不展开解释,如果用的版本是hive3.0的话,再看官方英文文档吧。

2、insert data into hive tables from queries

把查询结果插入到表中

语法:

标准语法:

insert into table tablename1 [partition (partcol1=val1, partcol2=val2 …)] select_statement1 from from_statement;
insert overwrite table tablename1 [partition (partcol1=val1, partcol2=val2 …) [if not exists]] select_statement1 from from_statement;
hive扩展:一次插入多个结果集

from from_statement
insert overwrite table tablename1 [partition (partcol1=val1, partcol2=val2…) [if not exists]] select_statement1
[insert overwrite table tablename2 [partition … [if not exists]] select_statement2]
[insert into table tablename2 [partition…] select_statement2]…;

from from_statement
insert into table tablename1 [partition (partcol1=val1, partcol2=val2…)] select_statement1
[insert into table tablename2 [partition…] select_statement2]
[insert overwrite table tablename2 [partition … [if not exists]] select_statement2]…;

hive扩展:动态分区插入

insert overwrite table tablename partition (partcol1[=val1], partcol2[=val2]…) select_statement from from_statement;
insert into table tablename partition (partcol1[=val1], partcol2[=val2]…) select_statement from from_statement;

insert overwrite将覆盖表或分区中的现有数据。if not exists表示当分区不存在时才插入或者覆盖,否则什么都不做。
从hive2.3.0开始,如果表具有tblproperties(“auto.purge”=”true”),则在对表运行insert overwrite时,表之前的数据不会移动到回收站,而是直接删除。此功能仅适用于托管表,并且在”auto.purge”属性未设置或者设置为false时关闭。
如果在创建表时指定了tblproperties(“immutable”=”true”),则表不可变。默认值是false。当不可变表中无数据时,insert into可以生效。当不可变表中有数据时,insert into会报错“Inserting into a non-empty immutable table is not allowed“。
insert overwrite的行为不受”immutable”属性影响。
insert 可以操作表或分区。如果表已分区,则必须通过指定所有分区列的值来指定表的特定分区。
可以在一个语句中指定多个插入子句(目标表和数据都可以不一样)。如

from pokes
insert into invites partition (ds = ‘2018-09-05’) select *
insert into invites partition (ds = ‘2018-09-06’) select *;

假如一个表的OutputFormat实现了AcidOutputFormat,并且系统配置为使用实现ACID的事务管理器,则将禁用该表的insert overwrite。这是为了避免用户无意中覆盖历史数据。可以通过使用truncate table(对于非分区表)或drop partition,然后insert into 来实现相同的功能。

alter table invites drop if exists partition (ds = ‘2018-09-01’) purge;

动态分区插入:

在动态分区插入中,用户只需在partition子句中指定分区列名称列表,而列值是不必需的。如果给出了分区列值,则称为静态分区,否则是动态分区。每个动态分区列都有一个来自select语句的相应输入列,这意味着动态分区创建由输入列的值确定。动态分区列必须在select语句的列中最后指定,并且与它们在partition()子句中出现的顺序相同。从hive3.0.0开始,不需要指定动态分区列。如果未指定,hive将自动生成分区规范。

例如:

创建一个有2个分区列的表,并在此表中插入数据。

create table invites2 (foo int, bar string) partitioned by (ds string, fs int);

insert into invites2 partition (ds=’2018-09-04′, fs) select foo, bar, foo from pokes;

如果foo有很多不同值,则上面插入操作可能会报错Container is running beyond virtual memory limits.可以参考

https://www.jianshu.com/p/62800898dd02

https://blog.csdn.net/T1DMzks/article/details/78818874

解决。

与动态分区插入有关的配置:

hive.exec.dynamic.partition,默认值是true,即允许动态分区插入。改为false,则禁用动态分区插入。

hive.exec.dynamic.partition.mode,默认值是strict,这种情况下,在指定动态分区列时,至少要有一个静态分区列。可改为nonstrict,这样的话,可以所有分区列都是动态分区列。

hive.exec.max.dynamic.partitions.pernode,默认值是100。

hive.exec.max.dynamic.partitions,默认值是1000。

hive.exec.max.created.files,默认值是100000。

hive.error.on.empty.partition,默认值是false。

这些配置项都在hive-default.xml.template文件中有,可以去里面看具体解释。

3、Writing data into the filesystem from queries

将查询结果集写到文件系统中,相当于load的反向操作

标准语法:

insert overwrite [local] directory directory1
[row format row_format] [stored as file_format]
select … from …

hive扩展:

from from_statement
insert overwrite [local] directory directory1 select_statement1
[insert overwrite [local] directory directory2 select_statement2] …

其中row_format支持

row_format
: delimited [fields terminated by char [escaped by char]] [collection items terminated by char]
[lines terminated by char]
[null defined as char]

directory目录可以是完整的URI。如果未指定schema或authority,hive将使用hadoop配置变量fs.default.name中的schema和authority来指定NameNode URI。

如果使用了local关键字,hive会将数据写入本地本件系统。

写入文件系统的数据被序列化为文本,其中列由^A分隔,行由换行符分隔。如果某些列类型不是原始类型,则这些列会被序列化为json格式。

需要注意的是,可以在一条语句同时insert overwrite directory、local directory以及hive表。

4、Inserting values into tables from SQL

用普通sql插入数据,可以一次插入多条记录。

标准语法:

insert into table tablename [partition (partcol1[=val1], partcol2[=val2] …)] values values_row [, values_row …]

这里有个地方需要注意:

假如目标表不是分区表的话,则在插入数据时是可以只指定某些列的,就像普通关系型数据库一样,但由于建hive表时一般不指定列的默认值,故指定列之外的列的值为null。

insert into table pokes values (1, ‘abc1’), (2, ‘abc2’);

insert into table pokes (foo) values (1);

假如目标表是分区表,则不能指定某些列。支持静态分区插入、动态分区插入。

insert into table invites partition (ds = ‘2018-09-07’) values (1, ‘val_1’);

insert into table invites partition (ds) values (1, ‘val_1’, ‘2018-09-07’);   同样的,把动态分区列的值放到最后面。

5、update

更新操作仅能在支持ACID的表上操作成功,否则会报Attempt to do update or delete using transaction manager that does not support these operations 错误。

标准语法:

UPDATE tablename SET column = value [, column = value…] [WHERE expression]

注意,分区列值无法更新,bucket列值也无法更新。

建议将hive.optimize.sort.dynamic.partition 设为fase,因为这会生成更高效的执行计划。

6、delete

删除操作也是仅能在支持ACID的表上操作成功。

标准语法:

delete from tablename [where expression]

同样建议将hive.optimize.sort.dynamic.partition 设为fase

7、merge

从hive2.2之后,才支持merge操作,且也是仅能在支持ACID的表上操作成功。

标准语法:

merge into <target table> as t using <source expression/table> as s
on <boolean expression1>
when matched [and <boolean expression2>] then update set <set clause list>
when matched [and <boolean expression3>] then delete
when not matched [and <boolean expression4>] then insert values<value list>

先研究完hive事务再研究这里。

转载于:https://www.cnblogs.com/koushr/p/9574946.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/107334.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • grunt集成Babel 实现ES6转ES5

    grunt集成Babel 实现ES6转ES5grunt集成Babel实现ES6转ES5背景:原来前端项目使用ES5开发,后来有个小伙伴使用了ES6的高级语言,导致项目无法通过grunt压缩。使用grunt集成babel,实现ES6转ES5,主要有一下几个步骤:1.配置package.jsondevDependencies里面是开发依赖,dependencies里面是项目依赖。”devDependencies”:{“babel-core”:”^6.26.3″,”babel-loader”:”^7.1.5″,

    2025年12月1日
    13
  • javascript中数据类型有哪些_四种基本数据类型

    javascript中数据类型有哪些_四种基本数据类型JavaScript里有几种数据类型

    2025年9月21日
    10
  • mysql重置root密码linux_linux怎么重置root密码

    mysql重置root密码linux_linux怎么重置root密码为数据库用户修改密码是DBA比较常见的工作之一。对于MySQL用户账户的密码修改,有几种不同的方式,推荐的方式使用加密函数来修改密码。本文主要描述了通过几种不同的方式来修改用户密码以及mysqlroot账户密码丢失(重置root密码)的处理方法。1、密码修改的几种方法a、可以在创建用户的时候指定密码,以及直接使用grant创建用户的时候指定密码。  对于已经存在的用户直接使用grant方式也可

    2022年8月13日
    10
  • PermitRootLogin是基于UID还是用户名?

    PermitRootLogin是基于UID还是用户名?Sometimesitisfuntodigabitdeeperintohowthingsworkjusttosatisfyyourcuriositywhilelearningsomethingnew,likePermitRootLogin,forexample.DoesitchecktheUIDortheusername?Tod…

    2022年5月20日
    51
  • C++写一个CSGO开箱模拟器「建议收藏」

    C++写一个CSGO开箱模拟器「建议收藏」head玩过csgo的人类都知道,开箱是很有趣的。————名人名言Appstore上有专门的开箱模拟器,但是啊,他有广告。(;′⌒`)所以心善的博主决定自己用C++手撸一款开箱模拟器:3A大作,有整整一种箱子可供选择附加开箱动画,效果及其逼真…

    2022年9月28日
    3
  • 如何实现股票自动化交易,甚至自动打板?

    如何实现股票自动化交易,甚至自动打板?闲聊曾经比较喜欢玩手机游戏 在某个手游板砖了无数个日月后 突然一天牛顿的宁夏哈密瓜砸爆了我的头 我为什么要手动玩游戏 一个开发者应该有开发者玩游戏的尊严 自动化 因此我完成了一个脚本来玩游戏来解放双手 在一个小圈子我突然冒出来 我是程序员 然后大家都惊呆了 原来经常讨论的程序猿就在身边 同时我共享了自己的脚本 得到了大家的认可 大佬 收下我的膝盖 程序猿无所不能 不禁有了一丝作

    2025年12月7日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号