hive基本操作指令

  • 创建内部表
create table mytable(
	id int, 
	name string) 
row format delimited fields terminated by '\t' stored as textfile;

附:
mytable是表名
id int是参数名字和参数类型
as textfile是代表格式,可以省略,默认都是text格式

  • 创建外部表
create external table mytable2(
	id int, 
	name string)
row format delimited fields terminated by '\t' location '/user/hive/warehouse/mytable2';

关键词external配合location使用,单引号中间内容是指定数据文件存在hdfs的目录

  • 创建分区表
create table mytable3(
	id int, 
	name string)
partitioned by(gender string) row format delimited fields terminated by '\t'stored as textfile;
  • 插入测试数据
load data local inpath '/root/hivedata/girl.txt' overwrite into table mytable3 partition(gender='girl');
  • 查询表分区
show partitions mytable3;
  • 查询分区表数据
select * from mytable3;
  • 重命名表
alter table student rename to student_mdf
  • 增加列
alter table student_mdf add columns (gender string);
  • 改变列
alter table student_mdf change sex gender string;
  • 替换列
alter table student_mdf replace columns (id string, name string);
  • 删除表
drop table if exists mytable;
  • 增加分区
alter table mytable3 add partition(sex='unknown') localtion /user/hive/warehouse/mydb.db/mytable3/sex=unknown;
  • 删除分区
alter table mytable3 drop if exists partition(sex='unknown');
  • Load 装载数据
load data [local] inpath 'filepath' [overwrite] into table tablename [partition (partcol1=val1, partcol2=val2 ...)]

附:
load:操作只是单纯的复制/移动操作,将数据文件移动到 Hive 表对应的位置
filepath:包括相对路径、绝对路径和包含模式的完整URI
local:如果指定了local,load命令会去查找本地文件系统中的 filepath。如果没有指定local关键字,则根据inpath中的URI查找文件
如果使用了overwrite关键字,则目标表(或者分区)中的内容会被覆盖

  • 插入一条数据
insert into table student_mdf values('1','zhangsan');
  • 开启动态分区
set hive.exec.dynamic.partition.mode=nonstrict

  • 将查询的数据直接存到一张新表中
create table mytable5 as select id, name from mytable3;
  • 动态分区使用场景,当我们想要对数据进行分区的时候,你能拿到的数据却未必是已经分好区的文件,并不能直接load进来就能使用,这时我们就经常用动态分区解决问题
    在这里插入图片描述
    例如对上表persons进行处理,把数据加入到按月份分区的表中
insert into birthdays partition(month) select id,name,month from persons;

附:
默认使用的是严格模式,是不允许动态分区的,我们需要在命令行执行set hive.exec.dynamic.partition.mode=nonstrict
使用动态分区会将查询结果集的最后一个作为分区条件,所以select查询要注意.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值