hive SQL基本操作

1.建表

创建简单表:

hive> CREATE TABLE pokes (foo INT, bar STRING);

创建外部表:

CREATE EXTERNAL TABLE page_view(viewTime INT, userid BIGINT,
page_url STRING, referrer_url STRING,

ip STRING COMMENT 'IP Address of the User',

country STRING COMMENT 'country of origination')

COMMENT 'This is the staging page view table'

ROW FORMAT DELIMITED FIELDS TERMINATED BY '\054'

STORED AS TEXTFILE

LOCATION '<hdfs_location>';

建分区表:

CREATE TABLE par_table(viewTime INT, userid BIGINT,
page_url STRING, referrer_url STRING,

ip STRING COMMENT 'IP Address of the User')

COMMENT 'This is the page view table'

PARTITIONED BY(date STRING, pos STRING)

ROW FORMAT DELIMITED ‘\t’

FIELDS TERMINATED BY '\n'

STORED AS SEQUENCEFILE;

建Bucket表:

CREATE TABLE par_table(viewTime INT, userid BIGINT,
     page_url STRING, referrer_url STRING,
     ip STRING COMMENT 'IP Address of the User')
 COMMENT 'This is the page view table'
 PARTITIONED BY(date STRING, pos STRING)
 CLUSTERED BY(userid) SORTED BY(viewTime) INTO 32 BUCKETS
 ROW FORMAT DELIMITED ‘\t’
   FIELDS TERMINATED BY '\n'
STORED AS SEQUENCEFILE;

创建表并创建索引字段ds:

hive> CREATE TABLE invites (foo INT, bar STRING) PARTITIONED BY (ds STRING);

复制一个空表:

CREATE TABLE empty_key_value_store
LIKE key_value_store;

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

3.将外部数据文件导入到hive中

建空表

hive> create table scores(id int, score int)
    > row format delimited
    > fields terminated by ','
    > stored as textfile;
hive> load data local inpath '/home/bruce/study/perl/score.text' overwrite into table scores;

要求:

1. 两个表的维度必须一样,才能够正常写入

2. 如果查询出来的数据类型和插入表格对应的列数据类型不一致,将会进行转换,但是不能保证转换一定成功,比如如果查询出来的数据类型为int,插入表格对应的列类型为string,可以通过转换将int类型转换为string类型;但是如果查询出来的数据类型为string,插入表格对应的列类型为int,转换过程可能出现错误,因为字母就不可以转换为int,转换失败的数据将会为NULL。

3. overwrite是删除原有数据然后在新增数据,如果有分区那么只会删除指定分区数据,其他分区数据不受影响

猜你喜欢

转载自my.oschina.net/u/3264690/blog/1811254