hive常见操作语句--创建表语句

一:hive建表语句

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 表示按什么字段进行分割,通常来说是按时间

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:

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字段显式的指定:

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可不必先创建

 

 

posted @ 2015-01-02 16:13  老去的JAVA程序员  阅读(4245)  评论(0编辑  收藏  举报