用hive外部表访问hbase数据

用hive外部表访问hbase数据

转载原文:https://blog.csdn.net/yimubanggong/article/details/75410259

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

1.这里hbase中的表oss_user_label_action_data已经存在
=> #<#Class:0xd922e1: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;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值