一:hive建表语句
sql
create table page_view ( page_id bigint comment '页面ID', page_name string comment '页面名称', page_url string comment '页面URL' ) comment '页面视图' partitioned by (ds string comment '当前时间,用于分区字段') row format delimited stored as rcfile location '/user/hive/test';
这里须要说下stored as 关键词,hive目前支持三种方式:
1:就是最普通的textfile,数据不作压缩,磁盘开销大,解析开销也大
2:SquenceFIle,hadoop api提供的一种二进制API方式,其具备使用方便、可分割、可压缩等特色。
3:rcfile行列存储结合的方式,它会首先将数据进行分块,保证同一个record在一个分块上,避免读一次记录须要读多个块。其次块数据列式存储,便于数据存储和快速的列存取。
RCFILE因为采用是的列式存储,因此加载时候开销较大,但具备很好的查询响应、较好的压缩比。
若是创建的表须要加上分区,则语句以下:
这里partitioned by 表示按什么字段进行分割,一般来讲是按时间
api
create table test_ds ( id int comment '用户ID', name string comment '用户名称' ) comment '测试分区表' partitioned by(ds string comment '时间分区字段') clustered by(id) sorted by(name) into 32 buckets row format delimited fields terminated by '\t' stored as rcfile;
若是须要对某些字段进行聚类存储,方便对hive集群列进行采样,则应该这样编写SQL:oop
create table test_ds ( id int comment '用户ID', name string comment '用户名称' ) comment '测试分区表' partitioned by(ds string comment '时间分区字段') clustered by(id) sorted by(name) into 32 buckets row format delimited fields terminated by '\t' stored as rcfile;
这里表示将id按照name进行排序,聚类汇总,而后分区划分到32个散列桶中。测试
若是想改变表在hdfs中的位置,则应该使用location字段显式的指定:url
create table test_another_location ( id int, name string, url string ) comment '测试另一个位置' row format delimited fields terminated by '\t' stored as textfile location '/tmp/test_location';
其中/tmp/test_location可没必要先建立code