栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

04-Hive表数据导入和导出

04-Hive表数据导入和导出

Hive表数据导入
[root@tianqinglong01 hive]# mkdir /hivedata
[root@tianqinglong01 hive]# cd hivedata
[root@tianqinglong01 hivedata]# vi user.txt
1,张三
2,李四
3,王五

hive>create table if not exists t_user(
id int,
name string
)
row format delimited
fields terminated by ','
lines terminated by 'n'
stored as textfile;

加载数据到hive,一般分为两种:

一种是从本地Linux上加载到hive中
另一种是从HDFS加载到hive中

方法一:使用hdfs dfs -put 将本地文件上传到表目录下

hdfs dfs -put ./user.txt  /usr/hive/warehouse/qfdb.db/t_user

方法二:在hive中使用load命令

hive> load data local inpath '/root/user.txt' [overwrite] into table t_user;  #追加 

方式三:

hive>create table if not exists t_user2(
id int,
name string
)
row format delimited
fields terminated by ','
lines terminated by 'n'
stored as textfile;
hive>create table if not exists t_user3(
id int,
name string
)
row format delimited
fields terminated by ','
lines terminated by 'n'
stored as textfile;
hive>create table if not exists t_user4(
id int,
name string
)
row format delimited
fields terminated by ','
lines terminated by 'n'
stored as textfile;

hive> insert into table t_user2 select * from t_user;

#扩展
hive> from t_user
    > insert into t_user3 select *
	> insert into t_user4 select * where id<3;
                                               
                                               
hive> create table t_user5 (id int);
hive> insert into t_user5 select id from t_user;

方法四 :克隆表数据

hive> create table if not exists t_user6 as select * from t_user;

#扩展内容 只复制表结构
hive> create table if not exists t_user7 like t_user;

加载数据的本质:

1,如果数据在本地,加载数据的本质就是将数据copy到hdfs上的表目录下。

2,如果数据在hdfs上,加载数据的本质是将数据移动到hdfs的表目录下。

hive数据导出
--1. 导出数据到本地文件系统的目录下
hive>insert overwirte local directory '/root/out/00'
	>select * from t_user;

--2. 导出数据到hdfs的目录下
hive>insert overwirte directory '/root/out/00'
	>select * from t_user;

---导出的文件字段默认不分隔

--3.修改导出后的列与列之间的格式
hive>insert overwirte directory '/root/out/01'
 	>row format delimited fields terminated by ','
	>select * from t_user;
	
--4.直接导入到本地系统s的文件
[root@tianqinglong01 ~]# hive -e 'select * from qfdb.t_user' >> /root/out/02
#导出的文件字段默认分隔符 't'
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/467669.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号