Hive 的DML操作

10 篇文章 0 订阅
2 篇文章 0 订阅

一、导入数据

1、向表中加载数据(load)

语法:

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

(1)、load data:表示加载数据

(2)、local:表示加载本地数据到hive表中,否则是加载HFDS中的数据到hive表中。

(3)、inpath:表示数据的路径

(4)、overwrite:表示覆盖表中已有数据,否则表示追加

(5)、into table:表示加载到哪张表

(6)、student:表示具体的表

(7)、partition:表示上传到指定的分区

 

2、通过查询语句向表中加载数据(insert)

基本插入(单条数据)

insert into table student  partition(month='201801')  values(5,'laoliu') ;

基本模式插入(根据单张表查询结果)

insert overwrite table student partition(month='201805') select id, name from student where month='201801';

多插入模式(根据多张表查询结果)

from student
   insert overwrite table student partition(month='201803')
   select id,name where month='201801'
   insert overwrite table student partition(month='201804')
   select id,name where month='201802';

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

根据查询结果创建表(查询的结果会添加到新创建的表中)

create table if not exists student2 as select id ,name from student;

4、创建表时通过 Location 指定加载数据的路径

 先创建表student3 并在HDFS上创建专属目录/user/hive/warehouse/student3,只要数据上传到这个目录就会自动关联上。

create table if not exists student3(id int,name string)
   row format delimited fields terminated by '\t'
   location '/user/hive/warehouse/student3';

上传数据到/user/hive/warehouse/student3

dfs -put /opt/module/datas/student.txt /user/hive/warehouse/student3;

5、Import  数据到指定 Hive  表中

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

export table default.student to '/user/xiaomage/student';

create table student4(id int,name string) partitioned by (month string) row format delimited fields terminated by '\t';

import table student4 partition(month='201801') from '/user/xiaomage/student';

二、导出数据

1、insert导出

将查询的结果导出到本地

insert overwrite local directory 
   '/opt/module/datas/export/student'
   select id,name from student2;

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

   insert overwrite local directory 
   '/opt/module/datas/export/student/s1'
   row format delimited fields terminated by '\t'
   select * from student;

将查询的结果格式化导出到HDFS中

   insert overwrite directory 
   '/user/xiaomage/student'
   row format delimited fields terminated by '\t'
   select * from student;

2、Hadoop 命令行将数据导出到本地

 dfs -get /user/xiaomage/student/000000_0  /opt/module/datas/export/student/student.txt;

3、Hive命令将数据导出到本地

 [xiaomage@hadoop111 hive]$ bin/hive -e 'select * from student2' > /opt/module/datas/export/student

[xiaomage@hadoop111 hive]$ bin/hive -f '/opt/module/datas/select_sql.txt' > /opt/module/datas/export/student

4、export导出数据到HDFS上

export table default.student3 to '/user/xiaomage/student3';

三、清除表中的数据

注意:使用truncate命令可以清除管理表中的数据,外部表无法清除。

truncate table student3;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值