hive 创建和删除库、表

hive 创建和删除库、表

前言

本文主要介绍hive 中操作库和表的语句。

hive 库操作

  1. 查看数据库:show databases
  2. 创建数据库:create database if not exists hive_testdb
  3. 使用某个数据库:use hive_testdb
  4. 查询数据库:show databases like ‘hive*’
  5. 显示数据库的信息:describe database hive_testdb
  6. 添加数据库备注:create database teacherdb comment
  7. 删除空数据库:drop database hive_testdb
  8. 强制删除数据库:drop database hive_testdb casecade

hive表操作

  1. 查看表的列表:show tables
  2. 创建表:create table if not exists hive_db.table_test(id string, name string, age int, …)
  3. 删除表:drop table if exists table_test
  4. 重命名表:alter table table_test rename to table1
  5. 修改表中列信息:alter table table_test change columns id tb_id int
  6. 增加列:alter table table_test add columns(class string commet “班级”)
  7. 删除或者替换列:alter table table_test replace columns(id string commet “备注”,name string commet “备注”);//这里是将所用列全部删除后再新建id和name列
  8. 复制表及数据:create table table_test1 as select * from table_test
  9. 导入表数据:load data local inpath ‘xxx(数据目录)/data/xxx.xxx’ overwrite into table hive_db.table_test
  10. 查询表:show tables in hive_db like “table*”
  11. 查看表信息:desc table_test
  12. 内部表与外部表的相互转换:
    alter table table_test set tblproperties(“EXTERNAL”=“TRUE”)  //内部表转换为外部表
    alter table table_test set tblproperties(“EXTERNAL”=“FALSE”)  //外部表转换为内部表
  13. 分区表(物理分区,逻辑上还是整表)
    创建分区表(按照月份来分区):create table table_partition(id string,name string) partitioned by (month string) row format delimited fields terminated by ‘\t’
    上传数据到分区表:load data local inpath ‘xxx(数据目录)/data/xxx.xxx’ into table table_partition partition(month=“201909”)
    查找分区表:select * from table_partition //查找分区表中的所有记录
    select * from table_partition where month=“201909”  //查找分区表中分区名201909中的所有记录
    查看分区:show partitions table_partition
    增加分区:alter table table_partition add partition (month=“201910”)
    alter table table_partition add partition (month=“201911”), partition (month=“201912”)
    删除分区:alter table table_partition drop partition(month=“201909”)
    ps:二级分区指的是2个分区字段,按照字段的顺序来设置分区顺序,比如:partition(month=“201909”,day=“01”)就是一个二级分区,目录结构中day是month文件夹的子文件夹。

参考博客

hive的数据定义之创建数据库和表 - hdc520 - 博客园

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值