hive outline
数据导入hive
本地文件
注意:
所谓本地文件,指的是源数据文件(这里是studet.txt)和hiveserver2在同一节点上
1、 加载本地文件studet.txt到hive默认数据库student表中(拷贝+追加)
拷贝:
本地文件依然存在
追加:
如果hive表student有数据,会追加
注:
- /opt/modules/hive-1.2.1/input是本地文件所在路径
- 不走mr
load data local inpath '/opt/modules/hive-1.2.1/inpu/student.txt' into table default.student;
2、加载本地文件studet.txt到hive默认数据库student表中(拷贝+覆盖)
覆盖:
如果hive表student有数据,会覆盖
load data local inpath '/opt/modules/hive-1.2.1/inpu/student.txt' overwrite into table default.student;
hdfs
1、加载hdfs文件到hive默认数据库student表中(移动+追加)
移动:
hdfs上的input目录下的student.txt文件从input目录移动了hive数据仓库所在的地方
追加:
如果hive表student有数据,会追加
注解:
- 其中/input/student.txt是hdfs的文件路径
- 不走mr
load data inpath '/input/student.txt' into table default.student;
2、加载hdfs文件到hive默认数据库student表中(移动+覆盖)
移动:
hdfs上的input目录下的student.txt文件从input目录移动了hive数据仓库所在的地方
覆盖:
如果hive表student有数据,会覆盖
注解:
- 其中/input/student.txt是hdfs的文件路
- 不走mr
load data inpath '/input/student.txt' overwrite into table default.student;
hql
1、insert+values (很少使用)
注解:
会走mr
insert overwrite table student
values (1, 'x'),
(2, 'y'),
(3, 'z');
2、insert + select (大量使用)
注解:
- 会走mr
- 在数据文件存储格式转换时经常用(例如把文件存储格式由textfile—>sequencefile)
这里会把对people 表的查询结果插入到表student表 中
insert overwrite table student select * from people;
或者
insert overwrite table student select 5, 'a';
动态分区插入
hive cte
数据从hive导出
本地
1、将查询的结果导出到本地
注意:
- /opt/modules/output/hive/student是本地路径
- 如果不指定分隔符,默认为\001
insert overwrite local directory '/opt/modules/output/hive/student' select * from student;
2、将查询的结果格式化导出到本地
/opt/modules/output/hive/student1是本地路径
insert overwrite local directory '/opt/modules/output/hive/student1' row format delimited fields terminated by '\t' select * from student;
hdfs
将查询的结果导出到 hdfs 上(没有 local 关键字)
insert overwrite directory '/output/hive/student2' row format delimited fields terminated by '\t' select * from student;
hive shell 命令导出
基本语法:(hive -f/-e 执行语句或者脚本 > file)
bin/hive -e 'select * from default.student;' > /opt/modules/output/hive/student3.txt;