hive的建表,插入数据、删除分区等命令大全

1、查询所有数据库

show databases;

2、删除数据库

drop database if exists task01;

内部表基本操作

3、创建student表

create table if not exists student(
name string,
age int,
agent string,
adress struct<street:STRING,city:STRING>)
row format delimited
fields terminated by ',' //字段与字段之间的分隔符
collection items terminated by ';'//一个字段各个item的分隔符
lines terminated by '\n' "); //行分隔符

4、查询所有表

show tables;

5、查看表结构

desc student;

6、加载数据

load data local inpath /home/hiveTest/student.txt overwrite into table student;

7、查询数据

select * from student;

8、统计查询(会运行mapreduce作业,资源开销较大)

select count(1) from student;

9、删除表数据不删除表结构

truncate table student;

10、删除表数据并删除表结构

drop table student;

外部表基本操作
外部表删除后,hdfs文件系统上的数据还在,
重新创建同路径外部表后,其数据仍然存在

11、创建student外部表 (在创建外部表的时候多加一个external限制)

create external table if not exists student(
name string,
age int,
agent string,
adress struct<street:STRING,city:STRING>)
row format delimited
fields terminated by ',' //字段与字段之间的分隔符
collection items terminated by ';'//一个字段各个item的分隔符
lines terminated by '\n' "); //行分隔符

从一张已经存在的表上复制其表结构,并不会复制其数据

12、创建表,携带数据

create table student01 as select * from student;

13、创建表,携带表结构

create table student01 like student;

分区表的操作
必须在表定义时创建partition
###静态分区

14、创建分区格式表

create table if not exists student02(
name string ,
salary int ,
subordinate array<string> ,
deductions map<string,float> ,
address struct<street:string,city:string>)
partitioned by (city string,street string)
row format delimited 
fields terminated by '\t' 
collection items terminated by ',' 
map keys terminated by ':' 
lines terminated by '\n' 
stored as textfile");

15、添加分区表

alter table student02 add partition(city='beijing',street='houchangcun')

16、查看分区表信息

show partitions student02;

17、向分区表中插入数据

load data local inpath /home/hiveTest/student02.txt overwrite into table student02 partition (city='beijing',street='houchangcun');

18、删除分区表

alter   table student02 drop partition(city='beijing',street='houchangcun');

19、把一个分区打包成一个har包

alter table student02 archive partition (city='beijing',street='houchangcun');

20、把一个分区har包还原成原来的分区

alter table student02 unarchive partition (city='beijing',street='houchangcun');

21、 保护分区防止被删除

alter table student02 partition (city='beijing',street='houchangcun') enable no_drop;

22、保护分区防止被查询

alter table student02 partition (city='beijing',street='houchangcun') enable offline;

23、允许分区删除和查询

 alter table student02 partition (city='beijing',street='houchangcun') disable no_drop;
 alter table student02 partition (city='beijing',street='houchangcun') disable offline;

动态分区
当需要一次插入多个分区的数据时,可以使用动态分区,根据查询得到的数据动态分配到分区里。
动态分区与静态分区的区别就是不指定分区目录,由hive根据实际的数据选择插入到哪一个分区。

set hive.exec.dynamic.partition=true; 启动动态分区功能
set hive.exec.dynamic.partition.mode=nonstrict;  分区模式,默认nostrict
set hive.exec.max.dynamic.partitions=1000;      最大动态分区数,默认1000

24、根据查询到的数据向分区表中插入数据

insert overwrite  table student02 partition (city='beijing',street) select name,salary,subordinate,deductions,address,address.street from student01;

25、当删除一个hive 数据库时,若该数据库时包含表,则会提示不能删除

此时,用户要么先删除数据库中的表,要么再删除数据库;要么在删除命令的最后面加上关键字CASCADE ,这样可以使hive自行先删除数据库中的表;

DROP DATABASE task01 CASCADE;
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值