Hive 之 DML 数据操作

1. 数据导入

1.1 向表中 load 数据

load 可以从本地服务器、hdfs 文件系统加载数据到数据表中:

load data [local] inpath '/opt/module/datas/student.txt' [overwrite] into table student [partition (partcol1=val1,)];

// 加载到 default 库 student 表
load data local inpath '/opt/module/datas/student.txt' into table default.student;

// 覆盖已有数据
load data inpath '/user/atguigu/hive/student.txt' overwrite into table default.student;
  • local:从本地加载,否则从 hdfs
  • overwrite:覆盖已有数据,否则为追加
  • partition:上传到指定分区

1.2 通过查询语句向表中插入 (insert) 数据

insert 操作会执行 MR 操作

// 创建一个分区表
create table student(id int, name string) partitioned by (month string) row format delimited fields terminated by '\t';

// 一般插入
insert into table  student partition(month='201709') values(1,'wangwu');

// 查询中间表(单表)方式插入
insert overwrite table student partition(month='201708')
             select id, name from student where month='201709';
         
// 查询中间表(多表)方式插入    
from student
              insert overwrite table student partition(month='201707')
              select id, name where month='201709'
              insert overwrite table student partition(month='201706')
              select id, name where month='201709';

1.3 创建表时指定数据路径 location

// 指定 hdfs 数据路径
hive (default)> create table if not exists student5(
              id int, name string
              )
              row format delimited fields terminated by '\t'
              location '/user/hive/warehouse/student5';

// 上传数据到 hdfs 上
hive (default)> dfs -put /opt/module/datas/student.txt
/user/hive/warehouse/student5;

1.4 import 导入数据

import 一般配合 export 使用,先用 export 导出后,再将数据导入

hive (default)> import table student2 partition(month='201709') from
 '/user/hive/warehouse/export/student';

2. 数据导出

  • insert:可以导出到本地、hdfs
  • hadoop 命令
  • hive shell 命令
  • Export 命令
  • Sqoop 导出

2.1 insert 导出

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

hive (default)> insert overwrite local directory '/opt/module/datas/export/student'
            select * from student;

// 格式化再导出
hive(default)>insert overwrite local directory '/opt/module/datas/export/student1'
           ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'             select * from student;

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

hive (default)> insert overwrite directory '/user/atguigu/student2'
             ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' 
             select * from student;

2.2 Hadoop命令导出到本地

hive (default)> dfs -get /user/hive/warehouse/student/month=201709/000000_0
/opt/module/datas/export/student3.txt;

2.3 Hive Shell 命令导出

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

bin/hive -e 'select * from default.student;' > /opt/module/datas/export/student4.txt;

2.4 Export导出到HDFS上

(defahiveult)> export table default.student to '/user/hive/warehouse/export/student';
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风老魔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值