Hbase 是可以支持實時查詢的非關(guān)系行數(shù)據(jù)庫,采用列存儲的同時也是的直接查詢的數(shù)據(jù)不太直觀,對此,我們可以將之關(guān)聯(lián)hive表,通過HQL大到查詢Hbase的目的
Hbase 關(guān)聯(lián) hive 表有兩種方式(通過建立hive管理表 和 外表的方式實現(xiàn))
通過管理表
1、創(chuàng)建hive-hbase 管理表:
drop table tbl_hive_mange;
create table tbl_hive_mange
(key String,
dict_id String,
city_id String,
city_name String,
city_code String,
group_id String,
group_name String,
area_code String,
bureau_id String,
sort String,
bureau_name String)
row format delimited
fields terminated by '|'
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES("hbase.columns.mapping" = ":key,
? ? info:dict_id,
? ? info:city_id,
? ? info:city_name,
? ? info:city_code,
? ? info:group_id,
? ? info:group_name,
? ? info:area_code,
? ? info:bureau_id,
? ? info:sort,
? ? info:bureau_name")
TBLPROPERTIES("hbase.table.name" = "tbl_hive_mange");

2、創(chuàng)建普通的 hive 管理表
drop table tbl_hive_mange_1;
create table tbl_hive_mange_1
(key String,
dict_id String,
city_id String,
city_name String,
city_code String,
group_id String,
group_name String,
area_code String,
bureau_id String,
sort String,
bureau_name String)
row format delimited
fields terminated by '|'
STORED AS TEXTFILE;

3、向 tbl_hive_mange_1 中添加數(shù)據(jù)
load data inpath 'hdfs:/ns1/user/hive/warehouse/xx.db/a...' into table tbl_hive_mange_1;

4、將 tbl_hive_mange_1 的數(shù)據(jù)添加到 tbl_hive_mange
insert overwrite table tbl_hive_mange select * from tbl_hive_mange_1;

目前為止:
建立了一張Hbase的表:tbl_hive_mange
并關(guān)聯(lián)了 hive 表:tbl_hive_mange
同時將hive表 tbl_hive_mange_1 的數(shù)據(jù)添加到了 tbl_hive_mange
可以通過hivev表:tbl_hive_mange查詢Hbase表:tbl_hive_mange 數(shù)據(jù)
注:
a:建立hive-hbase 管理表 tbl_hive_mange 后不能直接通過load data 的方式添加數(shù)據(jù)
hive> load data inpath 'hdfs://ns1/user/hive/warehouse/xx.db/tbl_hive_test' into table tbl_hive_test;
FAILED: SemanticException [Error 10101]: A non-native table cannot be used as target for LOAD

b:通過 desc formatted tbl_hive_mange 可以看到在默認的hive數(shù)據(jù)存儲路徑下存在制定文件夾,但是卻沒有數(shù)據(jù),表明這個數(shù)據(jù)是存在hbase下面的。

c:刪除表操作
先刪除hbase(disabled->drop)
hbase(main):003:0> disable 'tbl_hive_mange'
0 row(s) in 2.5610 seconds
hbase(main):004:0> drop 'tbl_hive_mange'
0 row(s) in 1.3140 seconds

此時show tables 在hive里面能看到tbl_hive_mange 但是已經(jīng)沒有數(shù)據(jù)了,用show tables還能查出來。但是用select * from tbl_hive_mange 查詢的時候報錯

此時mysql中TBLS還有hive表的信息,執(zhí)行drop table? tbl_hive_mange 報錯。

繼續(xù)show tables時,發(fā)現(xiàn)表已經(jīng)不在了。TBLS里面也沒有hive表信息了。
通過管理表
1、建立外部表(hive),關(guān)聯(lián)存在hbase表
drop table tbl_hive_xternal;
create xternal table tbl_hive_xternal
(key String,
dict_id String,
city_id String,
city_name String,
city_code String,
group_id String,
group_name String,
area_code String,
bureau_id String,
sort String,
bureau_name String)
row format delimited
fields terminated by '|'
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES("hbase.columns.mapping" = ":key,
? ? info:dict_id,
? ? info:city_id,
? ? info:city_name,
? ? info:city_code,
? ? info:group_id,
? ? info:group_name,
? ? info:area_code,
? ? info:bureau_id,
? ? info:sort,
? ? info:bureau_name")
TBLPROPERTIES("hbase.table.name" = "tbl_hive_xternal");

2、創(chuàng)建普通的 hive 管理表
drop table tbl_hive_xternal_1;
create table tbl_hive_xternal_1
(key String,
dict_id String,
city_id String,
city_name String,
city_code String,
group_id String,
group_name String,
area_code String,
bureau_id String,
sort String,
bureau_name String)
row format delimited
fields terminated by '|'
STORED AS TEXTFILE;

3、向 tbl_hive_xternal_1 中添加數(shù)據(jù)
load data inpath 'hdfs:/ns1/user/hive/warehouse/xx.db/a...' into table tbl_hive_xternal_1;

4、將 tbl_hive_xternal_1 的數(shù)據(jù)添加到 tbl_hive_xternal
insert overwrite table tbl_hive_xternal select * from tbl_hive_xternal_1;

目前為止:
建立了 hive 外部表表: tbl_hive_xternal
同時將hive表 tbl_hive_xternal_1 的數(shù)據(jù)添加到了 tbl_hive_xternal
可以通過hivev表: tbl_hive_xternal 查詢Hbase表: tbl_hive_xternal 數(shù)據(jù)
注:
a:使用外部表需要確保 hbase的表存在,同時在建立hive標的時候可以指定hbase中不存在的列
b:desc formatted tbl_hive_xternal可以看到存儲文件的路徑,但是真實的數(shù)據(jù)存儲在hbase
c:刪除操作:刪除hive表對hbase操作沒有影響; 但是刪除hbase表后hive查詢會報錯(數(shù)據(jù)存hbase)