HIve编程指南

HIve编程指南数据定义库描述信息表管理表(外部表)外部表分区表管理表外部分区表修改表增加修改删除表分区删除分区修改列信息增加列删除或替换列数据操作插入数据静态分区插入向管理表中装载数据通过查询语句向表中插入数据动态分区插入单个查询语句中创建表并加载数据导出数据查询算术运算符函数数学函数聚合函数表生成函数内置其他函数limit语句嵌套select 语句case ... when ... then..谓词操作符like和rlikejoin语句inner joinorder by 和 sort by含有sort
摘要由CSDN通过智能技术生成

数据定义

描述信息

创建数据库并添加描述信息

create database test
comment "this is a test";

添加键值对描述信息

create database test
with dbproperties ('creator'='test db','date'='2021-4-23');

查看数据库的描述信息

describe database test;
或
describe database extended test;

查看表结构信息

describe extended my.user;
formatted 可以替代 extended 可提供更加可读和长的输出信息

向表添加字段以及描述

alter table  bm_itv_order_statis_analyse_d add columns(other_price bigint,new_renew_price bigint);
alter table bm_itv_order_statis_analyse_d change column total_price total_price bigint COMMENT '总收入';
alter table bm_itv_order_statis_analyse_d change column new_renew_price new_renew_price bigint COMMENT '新增续订收入';

查看某列的信息

describe my.user;name;

管理表(外部表)

默认情况下hive会将这些表的数据存储由配置项hive.meatstore.warehouse.dir 所定义的目录的子目录下(/user/hive/warehouse)

当删除管理表时hive会删除表内数据

外部表

用于读取数据文件

读取 'data/stocks‘下 以  ’,‘  分隔的数据
create external table if no exists user(
	name string,
	age int,
	sex string)
row format delimited fields terminated by ','
locatilon 'data/stocks'

删除外部表不会删除原数据 但是描述表的元数据信息会被删除

对于管理表,可以对存在的一张表进行表结构的复制

create external table if not exists mydb.tmp_user
like mydb.user
location '/path/to/data'

在这里插入图片描述

分区表 (分区字段不能和表字段一样)

管理表

按照country和state来对数据进行分区

create table employees(
name string,
salary float,
address struck<street:string,city:string,state:string,zip:int)
partitioned by (country string,state string)

分区表改变了hive对数据存储的组织方式,如果在mydb内创建这个分区表,则会有一个对应的employees目录对应 /user/hive/warehouse/mydb.db/employees

查看表内所有分区

show partitions employees;

查看某个特定分区下的分区
show partitions employees partition (country=‘us’); 查位于country=‘us’ 下的所有分区

通过载入数据的方式在管理表创建分区

从本地目录$HOME}/c_employees  载入数据到employees表  hive会创建对应分区目录  /employees/country=us/state=ca   $HOME}/c_employees这个目录下的数据将会被拷贝到上述分区的目录下
load data local inpath '${env:HOME}/c_employees'
into table employees
partition (country='us',state='ca');
外部分区表
create table if not exists log_messages(
hms int,
severity string,
sever fstring,
process_id int,
message string)
partitioned by (year int,month int,day int)
row format delimited fields terminated by '\t';

向分区表添加一个特定的分区

alter table log_message add partition(year=2021,month=3,day=2)
location '/data/log_message/2021/3/2';

修改表

alter table 只会修改元数据不会修改数据本身

修改表名

alter table user rename to user_test;

修改表属性(表属性可以修改但无法删除)
在这里插入图片描述

增加修改删除表分区
增加 alter table user add  if not exists
partition (year=2021,month=2,day=2) location 'logs/2021/2/2'
partition (year=2021,month=2,day=3) location 'logs/2021/2/3'

对于管理表 添加分区,分区内的数据会同时和元数据信息一起删除
对于外部表,分区内数据不会删除

移动位置来修改分区路径

alter table user partition(year=2021,month=2,day=5)
set location '.logs
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值