通過hive表整合查詢hbase資料
阿新 • • 發佈:2019-01-09
大家知道,直接從hbase的讀取資料是一個比較繁鎖的過程,需要java程式碼或是spark 查詢
通過Hive整合HBase,可以通過hive表查詢hbase資料,下面是測試過程
--建立hbase表
create "user","account","address","info","userid"
--建立對映hbase表列族的hive外部表
CREATE EXTERNAL TABLE hbase_user(key string, idcard string,passport string,country string,name string,password string, province string,city string,age string,sex string ,id string) STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,account:idcard,account:passport,account:country,account:name,account:password, address:province,address:city,info:age,info:sex,userid:id") TBLPROPERTIES("hbase.table.name" = "user");
--新增hbase新的記錄
put 'user','zhangsan','account:idcard','420923156366998855' put 'user','lisi','account:idcard','520369856366998855' put 'user','lisi','account:country','china' put 'user','lichangzai','account:idcard','132901981090831721' put 'user','changfei','account:idcard','132901983090831736' put 'user','lichangzai','info:age','35' put 'user','lichangzai','info:sex','man' put 'user','changfei','info:age','30'
-- hive查詢hbase資料
hive> select * from user;
--修改hbase記錄,直接put行建的的列值
put 'user','lisi','account:idcard','520369856366998856'
--新增列族
alter 'user','work','adu'
--新增新列族的欄位值
put 'user','lichangzai','edu:mschool','rq no.1' put 'user','lichangzai','edu:university','qhddx' put 'user','lichangzai','work:company1','12580' put 'user','lichangzai','work:company2','china mobile' put 'user','lichangzai','info:site','blog.csdn.net/lichangzai' put 'user','lichangzai','info:mobile','13712345678' put 'user','changfei','edu:university','bjdx' put 'user','changfei','work:company1','LG' put 'user','changfei','info:mobile','13598765401' put 'user','changfei','info:site','hi.baidu/lichangzai'
再查詢hive表,可以看到新新增的欄位值沒有被顯示。
下面可以建立刪除hive外部表,再重新建立對映hbase的外部表,把hbase新增的列族對映進來
--刪除hive外部表(外部表的特性,表會刪除,資料還在)
hive> drop table user;
--重新建立外表
CREATE EXTERNAL TABLE hbase_user(key string, idcard string,passport string,country string,name string,password string,
province string,city string,age string,sex string,site string,mobile string,work1 string,work2 string,edu_mschool string,edu_univ string,id string)
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,account:idcard,account:passport,account:country,account:name,account:password,
address:province,address:city,info:age,info:sex,info:site,info:mobile,work:company1,work:company2,edu:mschool,edu:university,userid:id")
TBLPROPERTIES("hbase.table.name" = "user");
--再次查詢hive表
select * from hbase_user