HIVE数据导入导出

HIVE数据导入导出

导入数据

1、location

(1)将数据上传到到hdfs上的一目录中

# 将table_a.txt上传到HDFS的/demo/data/table_a目录中
hdfs dfs -put table_a.txt /demo/data/table_a;

(2)创建表时指定数据在hdfs上的位置

create table if not exists tb_a(
id int,
name string,
gender string,
age int
)
row format delimited fields terminated by ','
location '/demo/data/table_a'; -- 指定数据在hdfs上的/demo/data/table_a目录中

2、-put

先创建表,然后在利用hdfs的-put命令将数据上传到hdfs表所在目录

# 直接将原始数据上传到表tb_a所在目录
hdfs dfs -put table_a.txt /user/hive/warehouse/doit.db/tb_a

3、load

hive> load data [local] inpath '/opt/module/datas/student.txt' [overwrite] into table student [partition (partcol1=val1,)];
/*
[]:表示其中的内容可以写或者不写
load data:表示加载数据
local :表示从本地加载数据到hive表,否则从HDFS加载数据到hive表
inpath:表示加载数据的路径
overwrite:表示覆盖表中已有的数据,否则表示追加
into table:表示加载到哪张表
student:具体的表名
partition:表示上传到指定的分区
*/

例子

-- 将本地数据加载到tb_teacher表中
load  data local inpath  "/data/user/" into table tb_teacher ;
-- 将HDFS中的数据加载到tb_teacher表中
load  data inpath  "/data/user/" into table tb_teacher ;
-- 将本地数据加载到tb_teacher表中,并且覆盖原先表中已有的数据
 load  data local inpath '/data/'  overwrite into table tb_user ;

4、insert

在表已经存在的前提下,通过查询语句向表中导入数据

-- 将tb_user表中gender为F的所有数据导入到tb_user2中
insert into tb_user2 select * from tb_user where gender='F';

多插入模式:只需要扫描一遍原始数据表,就能将需要的数据插入到相应的表中

-- 将from语句到最前面,一次扫描,表tb_b、tb_c、tb_d就能获取到在tb_a中满足条件的数据
from tb_a
insert overwrite table tb_b
select * where salary>6000
insert overwrite table tb_c
select * where age>35
insert overwrite table tb_d
select * where age>20 and age<=35;

5、建表导入数据

根据查询结果创建表,查询的结果会插入到新创建的表中

-- 创建一个tb_b表,将查询到的结果插入到tb_b表中
create table tb_b as select * from tb_a; 

6、Import

import数据到指定的HIVE表中

注意先用export导出后,再将数据导入。

-- 将HDFS中的数据导入到tb_user表中
import table tb_user from "/demo/data/user"

导出数据

1、insert

-- 将查询的结果导出到本地,结果用默认的分隔符隔开,
insert overwrite local directory '/data/student_1' select * from student;

-- 将查询的结果格式化导出到本地,结果用“,”分隔开
insert overwrite local directory '/data/student_2'
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' select * from student;

--将查询的结果导出到HDFS上(没有local)
insert overwrite directory '/demo/data/student_3'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'  select * from student;

2、hadoop命令

利用hadoop命令将表中的数据导出到本地

-- 在hive上直接使用hadoop命令将数据直接导出到本地,通过这种方式导出的是原始的表数据
dfs -get /user/hive/warehouse/student/month=201709/000000_0
/data/student/studen.csv;

3、shell(常用)

shell脚本或命令导出

# 基本语法
hive -e 执行语句 > file 
hive -f 执行脚本文件 > file # 可以写在定时器里,每天定时获取数据

例子

-- 先切换数据库,然后再查询tb_a表的所有数据,写入res1.txt文件中
hive -e 'use doit;select * from tb_a' > res1.txt

准备脚本test.cmd

use doit;
select * from yg
where uid>2;

用命令执行脚本

# 执行脚本,将结果写入res2.txt
hive -f test.cmd > res2.txt

定时器执行脚本

[root@linux01 output]# crontab -e    #用vi编辑定时器内容
# 每天0点将查询的数据写入/data/usr/output/res3.txt文件中
* 0 * * * /opt/apps/hive3.1.2/bin/hive -f /data/usr/output/test.cmd >> /data/usr/output/res3.txt

4、export

和导入数据import是对应的

-- 将数据库doit中表tb_a的数据导入HDFS上demo/export/table_a目录下
export table doit.tb_a to '/demo/export/table_a';
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值