hbase和hive整合

656 阅读1分钟

HBase相关命令

-- 创建表
create 'xws:subscribe', 'f1'[, 'f2', 'f3']
-- 扫描表
scan 'xws:subscribe', {RAW => true, VERSIONS => 10}

Hive相关命令

#显示表创建语句
show create table tableName;

HBase与Hive整合

1. 建立Hive表,关联HBase表,插入数据到Hive表的同时能影响HBase表

在Hive中创建表会同时创建HBase表并关联起来(若HBase中已存在表,则报错,并且删除Hive表也会删除HBase中的关联表)

create table hive_hbase_subscribe (id string, gn string, av string, tm bigint)
stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
with serdeproperties ("hbase.columns.mapping"=":key,f1:gn,f1:av,f1:tm")
tblproperties ("hbase.table.name"="xws:subscribe2");

在Hive中创建临时中间表,用于load数据

不能将数据直接load进Hive所关联HBase的那张表中

create table subscribe_tmp (id string, name string, value string, seconds bigint)
row format delimited fields terminated by ',';

向Hive中间表load数据

load data local inpath '/home/hive/xws/subscribe.txt' into table subscribe_tmp;
select * from subscribe_tmp;

通过insert命令将中间表中的数据导入到Hive关联HBase的那张表中

insert into table hive_hbase_subscribe select * from subscribe_tmp;
select * from hive_hbase_subscribe;

2. 先有HBase表,建立Hive外部表关联HBase表(推荐使用)

在Hive中创建外部表关联HBase表(mapping映射列)

-- 创建外部表
create external table ext_hbase_subscribe (id string, gn string, av string, tm bigint)
stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
with serdeproperties ("hbase.columns.mapping"=":key,f1:gn,f1:av,f1:tm")
tblproperties ("hbase.table.name"="xws:subscribe");

-- 查询数据
select * from ext_hbase_subscribe;