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]
[map keys 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事务再研究这里。

hive DML的更多相关文章

  1. Hive 6、Hive DML(Data Manipulation Language)

    DML主要是对Hive 表中的数据进行操作的(增 删 改),但是由于Hadoop的特性,所以单条的修改.删除,其性能会非常的低所以不支持进行级操作: 主要说明一下最常用的批量插入数据较为常用的方法: ...

  2. Hive 官方手册翻译 -- Hive DML(数据操纵语言)

    由 Confluence Administrator创建, 最终由 Lars Francke修改于 八月 15, 2018 原文链接 https://cwiki.apache.org/confluen ...

  3. hive DML操作

    1.数据导入 1)向表中装载数据(load) 语法 hive> load data [local] inpath '/opt/module/datas/student.txt' [overwri ...

  4. hive dml语法

    Loading files into tables 语法 1 LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename ...

  5. hive DML 操作

    数据导入 向表中装载数据(Load) 1.语法 load data [local] inpath '数据的 path' [overwrite] into table student [partitio ...

  6. Hive 编程之DDL、DML、UDF、Select总结

    Hive的基本理论与安装可参看作者上一篇博文<Apache Hive 基本理论与安装指南>. 一.Hive命令行 所有的hive命令都可以通过hive命令行去执行,hive命令行中仍有许多 ...

  7. hive从入门到放弃(三)——DML数据操作

    上一篇给大家介绍了 hive 的 DDL 数据定义语言,这篇来介绍一下 DML 数据操作语言. 没看过的可以点击跳转阅读: hive从入门到放弃(一)--初识hive hive从入门到放弃(二)--D ...

  8. HIVE教程

    完整PDF下载:<HIVE简明教程> 前言 Hive是对于数据仓库进行管理和分析的工具.但是不要被“数据仓库”这个词所吓倒,数据仓库是很复杂的东西,但是如果你会SQL,就会发现Hive是那 ...

  9. [Hive - LanguageManual] Alter Table/Partition/Column

    Alter Table/Partition/Column Alter Table Rename Table Alter Table Properties Alter Table Comment Add ...

随机推荐

  1. How attach Java source(为eclipseIDE附加资源)

    In Eclipse, when you press Ctrl button and click on any  Class names, the IDE will take you to the s ...

  2. Understanding sun.misc.Unsafe

    转自: https://dzone.com/articles/understanding-sunmiscunsafe The biggest competitor to the Java virtua ...

  3. BZOJ 2460 [BeiJing2011]元素(线性基模板题)

    Description 相传,在远古时期,位于西方大陆的 Magic Land 上,人们已经掌握了用魔法矿石炼制法杖的技术.那时人们就认识到,一个法杖的法力取决于使用的矿石.一般地,矿石越多则法力越强 ...

  4. RAD Studio Demo Code和几个国外FMX网站 good

    FireMonkey X –  Amazing overview of FireMonkey FMX Feeds – All your FireMonkey news in one place FMX ...

  5. oracle数据库sqlldr命令的使用

    将数据导入 oracle 的方法应该很多 , 对于不同需求有不同的导入方式 , 最近使用oracle的sqlldr命令 导入数据库数据感觉是个挺不错的技术点 .  使用sqlldr命令 将文本文件导入 ...

  6. BlockingCollection 集合随记

    BlockingCollection 集合是一个并发安全的集合,而且设计用来实现类似于消息队列的功能,生产者.消费者模式. static void Main(string[] args) { Bloc ...

  7. 菜鸟的Xamarin.Forms前行之路——windows下VS运行ios模拟器调试

    在Xamarin.Forms项目中,运行安卓模拟器是很方便的,但是想要运行IOS模拟器,相对而言是困难一点. 在参考一些资料后,发现很多是与Xamarin.studio有关的方法,尝试了许久没有成功. ...

  8. ML.NET Cookbook --- 1.如何从文本文件中加载数据?

    使用ML.NET中的TextLoader扩展方法从文本文件中加载数据.你需要知道在文本文件中数据列在那里,它们的类型是什么,在文本文件中什么位置可以找到它们. 请注意:对于ML.NET只读取文件的某些 ...

  9. NPOI CellStyle 设置

    public class ExcelNPOIUnit { public static void SetCell(IWorkbook workbook, ISheet sheet, IRow row, ...

  10. python index()函数

    python内置index()函数 index() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,该方法与 python ...