- 创建内部表
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查询要注意.