Hive快速入门系列(9) | Hive表中数据的加载与导出

网友投稿 669 2022-05-29

本次博主为大家带来的是Hive表中数据的加载与导出。希望能够帮助到大家。

目录

一. Hive表中加载数据

1.1 直接向分区表中插入数据

1.2 通过查询插入数据

1.3 多插入模式

1.4 查询语句中创建表并加载数据(as select)

1.5 创建表时通过location指定加载数据路径

二. Hive表中的数据导出(了解就行)

2.1 insert导出

2.2 Hadoop命令导出到本地

2.3 hive shell 命令导出

2.4 export导出到HDFS上(全表导出)

三. 清空表数据

一. Hive表中加载数据

1.1 直接向分区表中插入数据

create table score3 like score; insert into table score3 partition(month ='201807') values ('001','002','100');

1

2

3

4

1.2 通过查询插入数据

1. 通过load方式加载数据

(linux) load data local inpath ‘/export/servers/hivedatas/score.csv’ overwrite into table score partition(month=‘201806’); (HDFS) load data inpath ‘/export/servers/hivedatas/score.csv’ overwrite into table score partition(month=‘201806’);

1

2

3

4

5

2. 通过查询方式加载数据

create table score4 like score; insert overwrite table score4 partition(month = '201806') select s_id,c_id,s_score from score;

1

2

关键字overwrite 必须要有

1.3 多插入模式

常用于实际生产环境当中,将一张表拆开成两部分或者多部分

1. 给score表加载数据

load data local inpath '/export/servers/hivedatas/score.csv' overwrite into table score partition(month='201806');

1

2. 创建第一部分表:

create table score_first( s_id string,c_id string) partitioned by (month string) row format delimited fields terminated by '\t' ;

1

3. 创建第二部分表:

create table score_second(c_id string,s_score int) partitioned by (month string) row format delimited fields terminated by '\t';

1

4. 分别给第一部分与第二部分表加载数据

from score insert overwrite table score_first partition(month='201806') select s_id,c_id insert overwrite table score_second partition(month = '201806') select c_id,s_score;

1

1.4 查询语句中创建表并加载数据(as select)

将查询的结果保存到一张表当中去

create table score5 as select * from score;

1

1.5 创建表时通过location指定加载数据路径

1. 创建表,并指定在hdfs上的位置

create external table score6 (s_id string,c_id string,s_score int) row format delimited fields terminated by '\t' location '/myscore6';

1

2. 上传数据到hdfs上

hdfs dfs -mkdir -p /myscore6 hdfs dfs -put score.csv /myscore6;

1

2

3

3. 查询数据

select * from score6;

1

二. Hive表中的数据导出(了解就行)

将hive表中的数据导出到其他任意目录,例如linux本地磁盘,例如hdfs,例如mysql等等

2.1 insert导出

1. 将查询的结果导出到本地

insert overwrite local directory '/export/servers/exporthive' select * from score;

1

2. 将查询的结果格式化导出到本地

insert overwrite local directory '/export/servers/exporthive' row format delimited fields terminated by '\t' collection items terminated by '#' select * from student;

1

Hive快速入门系列(9) | Hive表中数据的加载与导出

3. 将查询的结果导出到HDFS上(没有local)

insert overwrite directory '/export/servers/exporthive' row format delimited fields terminated by '\t' collection items terminated by '#' select * from score;

1

2.2 Hadoop命令导出到本地

dfs -get /export/servers/exporthive/000000_0 /export/servers/exporthive/local.txt;

1

2.3 hive shell 命令导出

基本语法:(hive -f/-e 执行语句或者脚本 > file)

bin/hive -e "select * from myhive.score;" > /export/servers/exporthive/score.txt

1

2.4 export导出到HDFS上(全表导出)

export table score to '/export/exporthive/score';

1

三. 清空表数据

只能清空管理表,也就是内部表

truncate table score6;

1

清空这个表会报错

本次的分享就到这里了,

看 完 就 赞 , 养 成 习 惯 ! ! ! \color{#FF0000}{看完就赞,养成习惯!!!} 看完就赞,养成习惯!!!^ _ ^ ❤️ ❤️ ❤️

码字不易,大家的支持就是我坚持下去的动力。后不要忘了关注我哦!

Hadoop Hive

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:16.9 Linux /boot/grub/目录
下一篇:GAMES101 课程环境搭建(Win10+Clion)及作业0说明
相关文章