)row format delimited fields terminated by ','collection items terminated by '-'map keys terminated by ':'location '/outter/data'//向外部表中载⼊数据
load data local inpath '/usr/local/person' into table outter_table;
Hive表的修改操作
表重命名
Alter table 旧名称 RENAME to 新名称;
alter table person rename to person_info;
修改列信息
Alter table 表名 CHANGE [COLUMN] 列名 新列名 数据类型[COMMENT 注释⽂本][AFTER 字段名称|FIRST];
//修改列名称
alter table test1 change name tename string;//修改列类型
alter table test1 change id id string;//指定列位置
alter table test1 change id id string first;
这⾥会遇到类型兼容的问题,看下表:Hive数据类型之间的隐式转换
增加列
Hive能将新的字段添加到已有字段之后 Alter table 表名 ADD COLUMNS( 列名 字段类型 [COMMENT ‘注释’], ........);
//增加列(可以⼀次添加多个)alter table test1 add columns( img string, addr string)
删除或替换列
Alter table 表名 REPLACE COLUMNS(列名 字段类型 [COMMENT ‘注释’], ............);
//删除替换列
alter table test1 replace columns( id string, name string, age string)
修改表的存储属性
Alter table表名 SET FILEFORMAT (TEXTFILE|SEQUENCEFILE|RCFILE|ORCFILE)会出现严重问题:(TEXTFILE的数据⽆法直接向RCFILE和ORCFILE导⼊,由于RCFILE
与ORCFILE都是以块状存储数据,我们只能通过insert语句导⼊到⼀个ORCFILE或RCFILE的新表中)。快对快,⾏对⾏
//修改表的存储结构
alter table test1 set fileformat sequencefile
设置表的注释
alter table 表名 set tblproperties('属性名'='属性值');
//设置表的注释
alter table test1 set tblproperties('comment'='测试表')
修改表的分隔符
alter table 表名 set serdeproperties('属性名'='属性值');
//修改表的分隔符
alter table test1 set serdeproperties('field.delim'='~')
查看表的详细的建表语句
Show create table 表名;
//展⽰创建表的详细信息show create table test1
修改表的serde_class
//修改表的序列化器
alter table test1 set serde 'org.apache.hadoop.hive.serde2.RegexSerDe'with serdeproperties (\"input.regex\" = \"id=(.*),name=(.*),age=(.*)\");
Hive添加jar包
add JAR '/usr/local/hive_data/hive-hcatalog-core-2.3.5.jar'(存放jar路径和jar名称)
Hive的分区操作
hive开发中,在存储数据时,为了更快地查询数据和更好地管理数据,都会对hive表中数据进⾏分区存储。所谓的分区,在hive表中体现的是多了⼀个字段。⽽在底 层⽂件存储系统中,⽐如HDFS上,分区则是⼀个⽂件夹,或者说是⼀个⽂件⽬录,不同的分区,就是数据存放在根⽬录下的不同⼦⽬录⾥,可以通过show partitions查看。
静态分区
//创建静态分区表
create table test_partition (id string comment 'ID',
name string comment '名字') comment '测试分区'
partitioned by (year int comment '年')ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' ;
insert语句:
//插⼊分区表数据(静态分区)
insert into table test_partition partition(year=2000) values(1,'⼩明');insert into table test_partition partition(year=1999) values(2,'⼩红');insert into table test_partition partition(year=2000) values(3,'⼩兰');insert into table test_partition partition(year=1998) values(4,'⼩紫');
load语句:
load data local inpath '/usr/local/part_test' into table test_partition partition (year =2018);load data local inpath '/usr/local/part_test' into table test_partition partition (year =2018);load data local inpath '/usr/local/part_test' into table test_partition partition (year =2017);
动态分区
动态分区默认不开启,需要使⽤下列语句开启:
set hive.exec.dynamic.partition.mode=nonstrict;#需退出hive,重新进⼊执⾏
//创建动态分区
create table test_partitions (id string comment 'ID',
name string comment '名字',year int comment '年')comment '测试'
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' ;
insert语句:
insert into table test_partitions partition(year) values(5,'⼩⽩',2016);insert into table test_partitions partition(year) values(6,'⼩⿊',2016);
load语句:
load data local inpath '/usr/local/part_test' into table test_partitions partition (year);
修改分区
指定到新分区⽬录下 原始分区仍旧存在 但是后续插⼊的新记录会存储到新分区中
alter table test_partitions partition(year=2016) set location '/user/hive/warehouse/new_part/b01.db/test_partition/year=2016';
删除分区
alter table test_partitions drop partition(year=2016);
同步到关系型数据库中的元信息
MSCK REPAIR TABLE test_partitions;
分桶表
分桶表描述
分桶是相对分区进⾏更细粒度的划分。分桶将整个数据内容安装某列属性值得hash值进⾏区分,如要安装name属性分为3个桶,就是对name属性值的hash 值对3取摸,按照取模结果对数据分桶。如取模结果为0的数据记录存放到⼀个⽂件,取模为1的数据存放到⼀个⽂件,取模为2的数据存放到⼀个⽂件。
//先设置⼀下分桶的权限
set hive.enforce.bucketing=true;//创建分桶表use test;
create table student_bck(id int, name string)
clustered by (id) sorted by (id desc) into 3 bucketsrow format delimited fields terminated by \
//插⼊数据
insert overwrite table student_bck select id,name from test_partition;//查询分桶数据
select * from student_bck tablesample(bucket 3 out of 3 on id);
tablesample (bucket x out of y on id);
# x表⽰从哪个桶(x-1)开始,y代表分⼏个桶,也可以理解分x为分⼦,y为分母,及将表分为y份(桶),取第x份(桶)