Hbase 關(guān)聯(lián) hive 表

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");

創(chuàng)建hive-hbase 管理表

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;

創(chuàng)建普通的 hive 管理表

3、向 tbl_hive_mange_1 中添加數(shù)據(jù)

load data inpath 'hdfs:/ns1/user/hive/warehouse/xx.db/a...' into table tbl_hive_mange_1;

向 tbl_hive_mange_1 中添加數(shù)據(jù)

4、將 tbl_hive_mange_1 的數(shù)據(jù)添加到 tbl_hive_mange

insert overwrite table tbl_hive_mange select * from tbl_hive_mange_1;

將 tbl_hive_mange_1 的數(shù)據(jù)添加到 tbl_hive_mange

目前為止:

建立了一張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

load data

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

desc formatted tbl_hive_mange

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 查詢的時候報錯

show tables

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

drop table

繼續(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");

建立外部表(hive),關(guān)聯(lián)存在hbase表

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;

創(chuàng)建普通的 hive 管理表

3、向 tbl_hive_xternal_1 中添加數(shù)據(jù)

load data inpath 'hdfs:/ns1/user/hive/warehouse/xx.db/a...' into table tbl_hive_xternal_1;

向 tbl_hive_xternal_1 中添加數(shù)據(jù)

4、將 tbl_hive_xternal_1 的數(shù)據(jù)添加到 tbl_hive_xternal

insert overwrite table tbl_hive_xternal select * from tbl_hive_xternal_1;

將 tbl_hive_xternal_1 的數(shù)據(jù)添加到 tbl_hive_xternal

目前為止:

建立了 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)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容