HiveQL:数据定义

来源:互联网 发布:刀路查看软件 编辑:程序博客网 时间:2024/05/17 20:09
Hive中的数据库:本质上是表的一个目录或命名空间,数据库所在的目录位于属性hive.metastore.warehouse.dir所指定的顶层目录之后,数据库的文件目录名是以.db结尾的。

hive在查询的时候,如果表中的数据以及分区个数都非常大的话,执行这样一个包含所有分区的查询可能会触发一个巨大的MapReduce任务。一个高度建议的安全措施就是将Hive设置为"strict(严格)"模式,这样如果对分区表进行查询而where字句没有加分区过滤的话,将会禁止提交这个任务。
set hive.mapred.mode=strict;
set hive.mapred.mode=nonstrict;

查看表的详细结构:
describe extended mydb.emp ;

查看表的所有分区:
show partitions emp ;

查看表的指定分区:
show partitions emp partition(state="US");

添加一个分区:
alter table emp add partition(year=2014,month=05,day=02) location 'hdfs://master.server/data/emp/2014/05/02';

自定义表的存储格式:
记录的解析是序列化器/反序列化器(或者简写为SerDe)来控制的。对于TextFile,Hive所使用的SerDe是org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe的Java类。
Hive使用一个InputFormat对象将输入流分隔成记录,然后在使用一个OutputFormat对象来将记录格式化为输出流,在使用一个SerDe在读取数据时将记录解析成列,在写入数据时将列编码成记录。

用户也可以使用指定的第三方的输入和输出格式以及SerDe:
create table kst
partitioned by (ds string)
row format serde 'com.linkedin.haivvreo.AvroSerDe'
with serdeproperties('schema.url'='http://schema_provider/kst.avsc')
stored as
inputformat 'com.linkedin.haivvreo.AvroContainerInputFormat'
outputformat 'com.linkedin.haivvreo.AvroContainerOutputFormat'; 

增加、修改及删除表分区:
alter table emp add if not exists
partition (year=2014,month=05,day=01) location 'hdfs://master.server/data/emp/2014/05/01'
partition (year=2014,month=05,day=02) location 'hdfs://master.server/data/emp/2014/05/02'
partition (year=2014,month=05,day=03) location 'hdfs://master.server/data/emp/2014/05/03';

alter table emp partition(year=2014,month=05,day=01)
set location 'hdfs://master.server/data/mod/2014/05/01';

alter table emp drop if exists partition(year=2014,month=05,day=01);

增加、修改、删除或者替换列:
添加新字段:
alter table emp add columns (
app_name string comment ''
sess_id long comment ''
);

修改hmn字段并将字段位置移动到serverity字段之后:
alter table emp change column
hmn hours_minites_seconds int comment '' 
after serverity ; 

移除表的所有字段并重新定义新的字段:
alter table emp replace columns (
hours_mins_secs int comment ''
serverity string comment ''
mess string comment ''
);

0 0