hive到hbase的使用

一、简单介绍

hive的元数据保存在metastore里面,真实的数据一般位于hdfs中,可以通过hql来对数据进行分析。hbase中的数据也是存放在hdfs上的,可不可以使用hive来分析hbase中的数据呢?

二、hive表到hbase表的映射

2.1hbase表t1的结构和其中的数据如下图

2.2创建hive表映射到hbase的表

首先输入下面的命令进入hive的客户端

hive --auxpath /usr/local/hive-0.14.0/lib/hive-hbase-handler-0.14.0.jar,/usr/local/hive-0.14.0/lib/zookeeper-3.4.5.jar -hiveconf hbase.master=hadoop26:60000 -hiveconf hbase.zookeeper.quorum=hadoop26

使用下面的语句创建hive表,数据是存储在hbase中的

CREATE EXTERNAL TABLE h1
(id string, name string,age int,gender string)
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,f1:name,f1:age,f1:gender") TBLPROPERTIES("hbase.table.name" = "t1");

三、使用hql来查询hbase中的数据

向hbase表t1中插入数据

put 't1','4','f1:name','zhaoliu'

 再次从hive中查询

 

hbase中的数据和hive是同步的,这也说明hive表的数据是位于hbase中的

四、使用hive向hbase中插入数据

4.1首先是需要创建一个hive临时表

create table h1_tmp
(id int,name string,age int,gender string)
row format delimited
fields terminated by '\t';

4.2向临时表中批量的上传数据

load data local inpath '/root/temp.txt' into table h1_tmp;

4.3把临时表h1_tmp的数据插入到目标表

insert into table h1 select * from h1_tmp; 

五、注意

如果hbase中添加新的列,那么hive中是查询不到的。

因为hive创建表的字段没有hbase新列的映射。如下面的情况。

put 't1','4','f1:birthday','1993'

但是在hive中查不到

posted @ 2016-07-18 15:10  dongdone  阅读(6037)  评论(0编辑  收藏  举报