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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 视频识别车牌号(Python)

    视频识别车牌号(Python)提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录前言 使用步骤 总结一、前言视频识别车牌号(有视频和代码)二、使用步骤直接上代码#导入所需要的库fromhyperlprimport*importcv2importnumpyasnp#定义保存图片函数#image:要保存的图片名字#addr;图片地址与相片名字的前部分#num:相片,名字的后缀。int类型defsave_image(i..

    2022年5月20日
    83
  • iis默认路径_服务器配置文件在哪

    iis默认路径_服务器配置文件在哪本文的性质为“编著”。“图形化网站管理者”请留步。 问题:当主机上的IIS服务由于各种原因无法打开时,无法看到当前系统内已经部署了哪些网站,以及其对应的目录等信息。为解决这一问题,本文通过查看IIS服务器的配置文件来获取系统内已部署网站的信息。 可能的“误导”预警:配置文件的信息与IIS的版本有关系,但本文仅为了解决问题,将操作系统与IIS版本混在了一起。 对win

    2022年9月25日
    0
  • 多图详解缓冲区溢出问题

    多图详解缓冲区溢出问题蠕虫病毒是一种常见的利用Unix系统中的缺点来进行攻击的病毒。缓冲区溢出一个常见的后果是:黑客利用函数调用过程中程序的返回地址,将存放这块地址的指针精准指向计算机中存放攻击代码的位置,造成程序异常中止。为了防止发生严重的后果,计算机会采用栈随机化,利用金丝雀值检查破坏栈,限制代码可执行区域等方法来尽量避免被攻击。虽然,现代计算机已经可以“智能”查错了,但是我们还是要养成良好的编程习惯,尽量避免写出有漏洞的代码,以节省宝贵的时间!

    2022年7月12日
    38
  • (转载)iphone 用法总结:NSNumber、NSString、NSDate、NSCalendarDate、NSData

    (转载)iphone 用法总结:NSNumber、NSString、NSDate、NSCalendarDate、NSData

    2021年8月15日
    48
  • 数据结构与算法(十六):平衡二叉树

    数据结构与算法(十六):平衡二叉树一、什么是平衡二叉树1.概述平衡二叉树(AVL树)是一种带有平衡条件的二叉搜索树。它的特性如下:AVL树的左右两个子树的高度差的绝对值不超过1AVL树的左右两个子树都是一棵平衡二叉树举个例子

    2022年8月16日
    3
  • ubuntu中apt和dpkg命令总结「建议收藏」

    ubuntu中apt和dpkg命令总结「建议收藏」1.apt和dpkg命令总结apt-cachesearch#——(package搜索包)apt-cacheshow#——(package获取包的相关信息,如说明、大小、版本等)apt-getinstall#——(package安装包)apt-getinstall#—–(package–reinstall重新安装包)apt-get

    2022年5月15日
    33

发表回复

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

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