name string comment ‘姓名’,
salary float comment ‘薪水’)
comment ‘这是一个测试的表’
tblproperties(‘creator’=’me’,’created_at’=’2014-11-13 09:50:33’)
location ‘/user/hive/warehouse/sopdm.db/test1’
–分区表
create table if not exists sopdm.test1(
name string comment ‘姓名’,
salary float comment ‘薪水’)
comment ‘这是一个测试的表’
partitioned by(country string,state string)
STORED AS rcfile
tblproperties(‘creator’=’me’,’created_at’=’2014-11-13 09:50:33’)
location ‘/user/hive/warehouse/sopdm.db/test1’
–查看表中存在的所有分区
show partitions table_name;
–查看表中特定分区
show partitions table_name partition(country=’US’);
–可以在表载入数据的时候创建分区
load data local inpath ‘${env:HOME/employees}’
into table employees
partition(country=’US’,state=’CA’);
–删除表
drop table if exists table_name;
–修改表-表重命名
alter table old_table_name rename to new_table_name;
–增加分区
alter table table_name add if not exists partition(year=2011,month=1,day=1)
location ‘/logs/2011/01/01’;
–修改分区存储路径
alter table table_name partition(year=2011,month=1,day=2)
set location ‘/logs/2011/01/02’;
–删除某个分区
alter table table_name drop if exists partition(year=2011,month=1,day=2);
–修改列信息
alter table table_name
change column old_name new_name int
comment ‘this is comment’
after severity; --字段移到severity字段之后(移动到第一个位置,使用first关键字)
–增加列
alter table table_name add columns(app_name string comment ‘application name’);
–删除或者替换列
alter table table_name replace columns(hms int comment ‘hhh’);
–修改表属性
alter table table_name set tblproperties(‘notes’=’this is a notes’);
–修改存储属性
alter table table_name partition(year=2011,month=1,day=1) set fileformat sequencefile;
–指定新的SerDe,并指定SerDe属性
alter table table_name
set serde “com.example.JSONSerDe”
with serdeproperties(‘prop1’=‘value1’, ‘prop2’=‘value2’);
–增加执行“钩子”——当表中存储的文在hive之外被修改了,就会触发钩子的执行
alter table table_name touch partition(year=2012,month=1,day=1);
–将分区内的文件打成hadoop压缩包文件,只会降低文件系统中的文件数,减轻NameNode的压力,而不会减少任何的存储空间
–使用unarchive替换archive起到反向操作
alter table table_name archive partition(year=2012,month=1,day=1);
–防止分区被删除和被查询(使用enable替代disable可以起到反向的操作目的)
alter table table_name partition(year=2012,month=1,day=1) disable no_drop;
alter table table_name partition(year=2012,month=1,day=1) disable offline;
–向管理表中装载数据
– inpath为一个目录,而且这个路径下不可以包含任何文件夹
load data local inpath ‘${env:HOME}/table_name’
overwrite into table table_name
partition(country=’US’);
–通过查询语句向表中插入数据
–overwrite是覆盖,into是追加
insert overwrite table table_name
partition(country=’US’)
select * from table_name2 tn where tn.cnty=’US’
–高效方式-查询语句插入多个分区
from table_name2 tn
insert overwrite table table_name
partition(country=’US’,state=’OR’)
select * where tn.cnty=’US’ and tn.st=’OR’
insert overwrite table table_name
partition(country=’US’,state=’CA’)
select * where tn.cnty=’US’ and tn.st=’CA’
–动态插入分区
–hive根据select语句最后2列确定分区字段country和state的值(根据位置)
insert overwrite table table_name
partition(country,state)
select …,se.cnty,se.st
from employees se;