Hive数据导入导出
数据导入
[root@qianfeng01 hive]# mkdir /hivedata
[root@qianfeng01 hive]# cd /hivedata
[root@qianfeng01 hive]# vi user.txt
-- 加入下面的数据
1,廉德枫
2,刘浩
3,王鑫
4,司翔
-- 在hive中创建一张表准备加载数据
create table t_user2(
id int,
name string
)
row format delimited
fields terminated by ','
lines terminated by '\n'
stored as textfile;
加载数据到Hive,一般分为两种:
- 一种是从本地Linux上加载到Hive中
- 另外一种是从HDFS加载到Hive中
方法1:使用hdfs dfs -put将本地文件上传到hive表目录下
hdfs dfs -put ./user.txt /user/hive/warehouse/zoo.db/t_user2
方法2:在hive中使用load 命令
load data [local] inpath '文件路径' [overwrite] into table 表名
hive> load data local inpath '/root/hivedata/user.txt' into table t_user2;
加载数据时:
1. 最好是写'绝对路径',从根开始写。
2. 写相对路径也是可以的,但是'一定要记住你登录hive时的位置',写相对路径是从ive当前登录的位置写相对路径
3. ~在hive中,是相对路径的写法
例如:启动路径是/root
在提供相对路径的时候 ~/hivedata/user.txt --》 ~就相当于/root
4. 使用benline工具进行远程登录(客户端与服务端不在同一台机器)时,使用以下语句时:
load data local inpath '文件路径' [overwrite] into table 表名
会有一个大坑:local是指服务端的文件系统。[benline就是客户端他使用的主机的文件系统]
5.overwrite 添加就是覆盖数据写入 不添加就是追加数据写入
扩展:hive可以加载的默认文件格式信息
WeChat9f2f42ee8a97608d13a9a4c388b6d985
方法3:从另外一张表(也可称之为备份表)中动态加载数据
-- 语法 将tableName1中所有select后面列数据插入到tableName2中
insert into table tableName2 select 表中的列(可以是多个)from tableName1
例如:
create table u2(
id int,
name string
)
row format delimited fields terminated by ',';
insert into table u2 select id,name from t_user2;
-- 语法 向多张表中插入数据语法
from tableName1(数据源表)
insert into tableName2(插入数据的表)select *或列 [where 条件]
insert into tableName3(插入数据的表)select *或列 [where 条件]
...
create table u3(
id int,
name string
)
row format delimited fields terminated by ',';
create table u4(
id int,
name string
)
row format delimited fields terminated by ',';
from u2
insert into u3 select *
insert into u4 select id,name;
注意: tableName2表中的字段个数必须和tableName1表中查询出来的个数相同
上述的这个操作会启动MR进行操作
方法4:克隆表数据
- create table if not exists tableName2 as select 列 from tableName1;
- create table if not exists tableName2 like tableName1 location 'tableName1的存储目录的路径' # 新表不会产生自己的表目录,因为用的是别的表的路径 -- 它不执行MR操作
扩展内容:只复制表结构
create table if not exists tableName2 like tableName1;
注意:上述的这个操作会启动MR进行操作
加载数据的本质:
- 如果数据在本地,加载数据的本质就是将数据copy到hdfs上的表目录下。
- 如果数据在hdfs上,加载数据的本质是将数据移动到hdfs的表目录下。
注意:hive使用的是严格的读时模式:加载数据时不检查数据的完整性,读时发现数据不对则使用NULL来代替。而mysql使用的是写时模式:在写入数据时就进行检查
数据导出
hive数据导出分类
- 从hive表中导出本地文件系统中(目录、文件)
- 从hive表中导出hdfs文件系统中
- hive表中导出到其它hive表中 (之前在导入数据的时候使用动态加载数据)
导出到目录下
-- 1.导出数据到本地文件系统目录下
insert overwrite local directory '本地系统文件导入路径' 获取表中数据语句操作(查询语句)
hive> insert overwrite local directory '/root/out/00' select * from t_user2;
-- 2.导出数据到HDFS文件系统目录下
insert overwrite directory 'HDFS系统文件导入路径' 获取表中数据语句操作(查询语句)
hive> insert overwrite directory '/root/out/00' select * from t_user2;
-- 上述导出数据的操作,数据之间是不存在分隔
--3.导出数据到“本地文件系统”或“HDFS”中可以指定导出数据的分隔符号
insert overwrite [local] directory '本地或HDFS文件系统文件导入路径'
row format delimited fields terminated by '指定数据分隔符号'
获取表中数据语句操作(查询语句)
直接导入到本地文件系统的文件中
ps:这种方式使用外部hive命了进行数据导出操作
-- 语法:
hive -e '提供查询语句获取数据(提供表的时候要使用 数据库名.表名)' >> 本地文件系统路径(路径上最后一个名字时输出文件的名字,不是文件夹的名字)
[root@qianfeng01 ~]# hive -e 'select * from zoo.t_user2' >> /root/out/01
-- 导出的数据默认会使用\t做数据分隔
更多大数据精彩内容欢迎B站搜索“千锋教育”或者扫码领取全套资料
【千锋教育】大数据开发全套教程,史上最全面的大数据学习视频
还没有评论,来说两句吧...