Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6543977
  • 博文数量: 1005
  • 博客积分: 8199
  • 博客等级: 中将
  • 技术积分: 13071
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-25 20:19
个人简介

脚踏实地、勇往直前!

文章分类

全部博文(1005)

文章存档

2020年(2)

2019年(93)

2018年(208)

2017年(81)

2016年(49)

2015年(50)

2014年(170)

2013年(52)

2012年(177)

2011年(93)

2010年(30)

分类: HADOOP

2014-11-12 14:38:26


 在hbase中创建表后,我们只能在hbase shell中使用scan查询数据,这对于熟悉SQL的使用者不怎么习惯,不过我们可以在hive中创建外部表来访问hbase表中的数据,例子如下:


1.这里hbase中的表oss_user_label_action_data已经存在
=> #<#:0xd5a1b0>
hbase(main):067:0> scan 'oss_user_label_action_data',LIMIT=>1
ROW                                COLUMN+CELL                                                                                      
 201407|31130101|8613500000001     column=info:areacode, timestamp=1415243857802, value=22                                          
 201407|31130101|8613500000001     column=info:cardtype, timestamp=1415243857802, value=1                                           
 201407|31130101|8613500000001     column=info:createtime, timestamp=1415243857802, value=20141028 11:18:34                         
 201407|31130101|8613500000001     column=info:enable_flag, timestamp=1415243857802, value=0                                        
 201407|31130101|8613500000001     column=info:extstring, timestamp=1415243857802, value=                                           
 201407|31130101|8613500000001     column=info:labelno, timestamp=1415243857802, value=31130101                                     
 201407|31130101|8613500000001     column=info:labelvalue, timestamp=1415243857802, value=9693                                      
 201407|31130101|8613500000001     column=info:modifytime, timestamp=1415243857802, value=20141028 11:18:45                         
 201407|31130101|8613500000001     column=info:monthno, timestamp=1415243857802, value=201407                                       
 201407|31130101|8613500000001     column=info:provcode, timestamp=1415243857802, value=1                                           
 201407|31130101|8613500000001     column=info:usernumber, timestamp=1415243857802, value=8613500000001                             
1 row(s) in 0.0580 seconds

2.创建外部表
CREATE EXTERNAL TABLE hive_oss_user_label_action_data(
key string, 
monthno string,
usernumber string,
labelno string,
labelvalue string,
provcode string,
areacode string,
cardtype string,
extstring string,
createtime string,
modifytime string

STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' 
WITH SERDEPROPERTIES
("hbase.columns.mapping" = 
":key,info:monthno,info:usernumber,info:labelno,info:labelvalue,info:provcode,info:areacode,info:cardtype,info:extstring,info:createtime,info:modifytime")
TBLPROPERTIES("hbase.table.name" = "oss_user_label_action_data");

注意hbase.columns.mapping后面的字段直接不能出现空格和换行.


3.通过hive查询数据
根据rowkey查询
select * from hive_oss_user_label_action_data where key='201407|31130101|8613500000001'

根据某个字段查询
select * from hive_oss_user_label_action_data where usernumber='8613500000001'

组合查询
select * from hive_oss_user_label_action_data where usernumber='8613500000001' and labelno='31130101'


说明:
这里我们访问的hive_oss_user_label_action_data表是虚表,数据是存储在hbase中的,我们可以创建另外一个hive中的表,
将hbase中的数据加载到hive本地


创建另外一个表
CREATE TABLE hive_oss_user_label_action_data_local(
key string, 
monthno string,
usernumber string,
labelno string,
labelvalue string,
provcode string,
areacode string,
cardtype string,
extstring string,
createtime string,
modifytime string
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
STORED AS TEXTFILE;


将hbase中的表数据加载到本地表
INSERT OVERWRITE TABLE hive_oss_user_label_action_data_local SELECT * FROM hive_oss_user_label_action_data;

-- The End --
阅读(10713) | 评论(0) | 转发(0) |
0

上一篇:hive外部表

下一篇:hbase行键过滤器RowFilter

给主人留下些什么吧!~~