hive与hbase数据交互的详解指南 | ApacheCN(apache中文网)

HBase和Hive的集成原理

ApacheCN | apache中文网html

Hive和Hbase有各自不一样的特征:hive是高延迟、结构化和面向分析的,hbase是低延迟、非结构化和面向编程的。Hive数据仓库在hadoop上是高延迟的。Hive集成Hbase就是为了使用hbase的一些特性。以下是hive和hbase的集成架构:java

图1 hive和hbase架构图node

        Hive集成HBase能够有效利用HBase数据库的存储特性,如行更新和列索引等。在集成的过程当中注意维持HBase jar包的一致性。Hive集成HBase须要在Hive表和HBase表之间创建映射关系,也就是Hive表的列(columns)和列类型(column types)与HBase表的列族(column families)及列限定词(column qualifiers)创建关联。每个在Hive表中的域都存在于HBase中,而在Hive表中不须要包含全部HBase中的列。HBase中的RowKey对应到Hive中为选择一个域使用:key来对应,列族(cf:)映射到Hive中的其它全部域,列为(cf:cq)。例以下图2为Hive表映射到HBase表:linux

图2 Hive表映射HBase表sql

 

1.文章来源:

http://blog.csdn.net/jiedushi/article/details/7325292shell

http://www.cr173.com/html/24339_1.html数据库

2.基本介绍

Hive是基于Hadoop的一个数据仓库工具,能够将结构化的数据文件映射为一张数据库表,并提供完整的sql查询功能,能够将sql语句转换为MapReduce任务进行运行。 其优势是学习成本低,能够经过类SQL语句快速实现简单的MapReduce统计,没必要开发专门的MapReduce应用,十分适合数据仓库的统计分析。
HiveHBase的整合功能的实现是利用二者自己对外的API接口互相进行通讯,相互通讯主要是依靠hive_hbase-handler.jar工具类, 大体意思如图所示:apache

3.软件版本

使用的软件版本:(没有下载地址的百度中找就行)编程

jdk-6u24-linux-i586.bin    架构

hive-0.9.0.tar.gz           http://yun.baidu.com/share/link?shareid=2138315647&uk=1614030671

hbase-0.94.14.tar.gz    http://mirror.bit.edu.cn/apache/hbase/hbase-0.94.14/

hadoop-1.1.2.tar.gz      http://pan.baidu.com/s/1mgmKfsG

4.安装位置

安装目录:/usr/local/     (记得解压后重命名一下哦)
Hbase的安装路径为:/usr/local/hbase

 Hive的安装路径为:/usr/local/hive

5.整合步骤

整合hive与hbase的过程以下:

1.在 /usr/local/hbase-0.90.4下:

hbase-0.94.14.jar,hbase-0.94.14-tests.jar 与lib/zookeeper-3.4.5.jar拷贝到/usr/local /hive/lib文件夹下面
注意:

若是hive/lib下已经存在这两个文件的其余版本(例如zookeeper-3.3.1.jar)

建议删除后使用hbase下的相关版本

还须要

protobuf-java-2.4.0a.jar拷贝到/usr/local/hive/lib和/usr/local/hadoop/lib下

2.修改hive-site.xml文件

在/usr/local/hive/conf下目录下,在hive-site.xml最底部添加以下内容:

(跳转到最下面的linux命令:按住Esc键 + 冒号 + $  而后回车) <property> <name>hive.querylog.location</name> <value>/usr/local/hive/logs</value> </property> <property> <name>hive.aux.jars.path</name>
注意:若是不存在则自行建立,或者把文件更名后使用。拷贝到全部节点包括的下。拷贝下的文件到全部节点包括的下。

注意,若是3,4两步跳过的话,运行hive时极可能出现以下错误:
org.apache.hadoop.hbase.ZooKeeperConnectionException: HBase is able to connect to ZooKeeper but the connection closes immediately.
This could be a sign that the server has too many connections (30 is the default). Consider inspecting your ZK server logs for that error and
then make sure you are reusing HBaseConfiguration as often as you can. See HTable's javadoc for more information. at org.apache.hadoop.
hbase.zookeeper.ZooKeeperWatcher.

5 启动hive (测试成功)
单节点启动
bin/hive -hiveconf hbase.master=master:60000
集群启动   (这个我没测试)
bin/hive -hiveconf hbase.zookeeper.quorum=node1,node2,node3   (全部的zookeeper节点)
若是hive-site.xml文件中没有配置hive.aux.jars.path,则能够按照以下方式启动。
hive --auxpath /opt/mapr/hive/hive-0.7.1/lib/hive-hbase-handler-0.7.1.jar,/opt/mapr/hive/hive-0.7.1/lib/hbase-0.90.4.jar,/opt/mapr/hive/hive-0.7.1/lib/zookeeper-3.3.2.jar -hiveconf hbase.master=localhost:60000

经测试修改hive的配置文件hive-site.xml

<property>
  <name>hive.zookeeper.quorum</name>
  <value>node1,node2,node3</value>
  <description>The list of zookeeper servers to talk to. This is only needed for read/write locks.</description>
</property>

不用增长参数启动hive就能够联合hbase

 

6.测试hive到hbase中

启动后进行测试(重启一下集群)

1.  用hive建立hbase能识别的表

语句以下:

create table hbase_table_1(key int, value string)

stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'

with serdeproperties ("hbase.columns.mapping" = ":key,cf1:val")

tblproperties ("hbase.table.name" = "xyz");

此刻你进入hbase shell中发现多了一张表 ‘xyz’
(能够先跳过这句话:hbase.table.name 定义在hbase的table名称,

多列时:data:1,data:2;多列族时:data1:1,data2:1;)
hbase.columns.mapping 定义在hbase的列族,里面的:key 是固定值并且要保证在表pokes中的foo字段是惟一值

建立有分区的表

create table hbase_table_1(key int, value string) 

partitioned by (day string)

stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'

with serdeproperties ("hbase.columns.mapping" = ":key,cf1:val")

tblproperties ("hbase.table.name" = "xyz");

不支持表的修改
会提示不能修改非本地表。
hive> ALTER TABLE hbase_table_1 ADD PARTITION (day = '2012-09-22');
FAILED: Error in metadata: Cannot use ALTER TABLE on a non-native table FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask 

 

2.  导入数据到关联hbase的表中去

1.在hive中新建一张中间表

create table pokes(foo int,bar string)

row format delimited fields terminated by ',';
批量导入数据
load data local inpath '/home/1.txt' overwrite into table pokes;

1.txt文件的内容为 
1,hello 
2,pear 
3,world

使用sql导入hbase_table_1

set hive.hbase.bulk=true;

2.插入数据到hbase表中去

insert overwrite table hbase_table_1

select * from pokes;

导入有分区的表

insert overwrite table hbase_table_1  partition (day='2012-01-01') 

select * from pokes;

3.查看关联hbase的那张表

hive> select * from hbase_table_1;
OK
1 hello
2 pear
3 world

(注:与hbase整合的有分区的表存在个问题  select * from table查询不到数据,select key,value from table能够查到数据)

4.登陆hbase查看那张表的数据

hbase shell

hbase(main):002:0> describe 'xyz' 
DESCRIPTION ENABLED {NAME => 'xyz', FAMILIES => [{NAME => 'cf1', BLOOMFILTER => 'NONE', REPLICATION_S true 
COPE => '0', COMPRESSION => 'NONE', VERSIONS => '3', TTL => '2147483647', BLOCKSI 
ZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}]} 
1 row(s) in 0.0830 seconds
hbase(main):003:0> scan 'xyz'
ROW COLUMN+CELL 
1 column=cf1:val, timestamp=1331002501432, value=hello 
2 column=cf1:val, timestamp=1331002501432, value=pear 
3 column=cf1:val, timestamp=1331002501432, value=world

这时在Hbase中能够看到刚才在hive中插入的数据了。

7.测试hbase到hive中

1.在hbase中建立表

create 'test1','a','b','c'

put 'test1','1','a','qqq'

put 'test1','1','b','aaa'

put 'test1','1','c','bbb'

put 'test1','2','a','qqq'

put 'test1','2','c','bbb'

2.把hbase中的表关联到hive中

对于在hbase已经存在的表,在hive中使用CREATE EXTERNAL TABLE来创建
例如hbase中的表名称为test1,字段为 a: , b: ,c: 在hive中建表语句为

create external table hive_test

(key int,gid map<string,string>,sid map<string,string>,uid map<string,string>)

stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'

with serdeproperties ("hbase.columns.mapping" ="a:,b:,c:") 

tblproperties  ("hbase.table.name" = "test1");

2.检查test1中的数据

在hive中创建好表后,查询hbase中test1表内容
select * from hive_test;

OK
1 {"":"qqq"} {"":"aaa"} {"":"bbb"}
2 {"":"qqq"} {} {"":"bbb"}

查询gid字段中value值的方法为
select gid[''] from hive_test;
获得查询结果
Total MapReduce jobs = 1
Launching Job 1 out of 1
Number of reduce tasks is set to 0 since there's no reduce operator
Starting Job = job_201203052222_0017, Tracking URL = http://localhost:50030/jobdetails.jsp?jobid=job_201203052222_0017
Kill Command = /opt/mapr/hadoop/hadoop-0.20.2/bin/../bin/hadoop job -Dmapred.job.tracker=maprfs:/// -kill job_201203052222_0017
2012-03-06 14:38:29,141 Stage-1 map = 0%, reduce = 0%
2012-03-06 14:38:33,171 Stage-1 map = 100%, reduce = 100%
Ended Job = job_201203052222_0017
OK
qqq
qqq

若是hbase表test1中的字段为user:gid,user:sid,info:uid,info:level,在hive中建表语句为
create external table hive_test

(key int,user map<string,string>,info map<string,string>)

stored by 'org.apache.hadoop.hive.hbase.hbasestoragehandler'

with serdeproperties ("hbase.columns.mapping" ="user:,info:") 

tblproperties  ("hbase.table.name" = "test1");

查询hbase表的方法为
select user['gid'] from hive_test;

 注:hive链接hbase优化,将HADOOP_HOME/conf中的hbase-site.xml文件中增长配置

 <property>
   <name>hbase.client.scanner.caching</name>
   <value>10000</value>
 </property>

或者在执行hive语句以前执行hive>set hbase.client.scanner.caching=10000;

报错:

Hive报错

1.NoClassDefFoundError
Could not initialize class java.lang.NoClassDefFoundError: Could not initialize class org.apache.hadoop.hbase.io.HbaseObjectWritable
将protobuf-***.jar添加到jars路径
 

//$HIVE_HOME/conf/hive-site.xml

<property>

   <name>hive.aux.jars.path</name>

   <value>file:///data/hadoop/hive-0.10.0/lib/hive-hbase-handler-0.10.0.jar,file:///data/hadoop/hive-0.10.0/lib/hbase-0.94.8.jar,file:///data/hadoop/hive-0.10.0/lib/zookeeper-3.4.5.jar,file:///data/hadoop/hive-0.10.0/lib/guava-r09.jar,file:///data/hadoop/hive-0.10.0/lib/hive-contrib-0.10.0.jar,file:///data/hadoop/hive-0.10.0/lib/protobuf-java-2.4.0a.jar</value>

</property>

 

 

Hbase 报错

:java.lang.NoClassDefFoundError: com/google/protobuf/Message  

编个Hbase程序,系统提示错误,java.lang.NoClassDefFoundError: com/google/protobuf/Message

找了半天,从这个地方发现了些东西:http://abloz.com/2012/06/15/hive-execution-hbase-create-the-table-can-not-find-protobuf.html

 

内容以下:

hadoop:1.0.3

hive:0.9.0

hbase:0.94.0

protobuf:$HBASE_HOME/lib/protobuf-java-2.4.0a.jar

能够看到,0.9.0的hive里面自带的hbase的jar是0.92版本的。

[zhouhh@Hadoop48 ~]$ hive –auxpath $HIVE_HOME/lib/hive-hbase-handler-0.9.0.jar,$HIVE_HOME/lib/hbase-0.92.0.jar,$HIVE_HOME/lib/zookeeper-3.3.4.jar,$HIVE_HOME/lib/guava-r09.jar,$HBASE_HOME/lib/protobuf-java-2.4.0a.jar

hive> CREATE TABLE hbase_table_1(key int, value string)

> STORED BY ‘org.apache.hadoop.hive.hbase.HBaseStorageHandler’

> WITH SERDEPROPERTIES (“hbase.columns.mapping” = “:key,cf1:val”)

> TBLPROPERTIES (“hbase.table.name” = “xyz”);

java.lang.NoClassDefFoundError: com/google/protobuf/Message

at org.apache.hadoop.hbase.io.HbaseObjectWritable.(HbaseObjectWritable.java

Caused by: java.lang.ClassNotFoundException: com.google.protobuf.Message

解决办法:

将$HBASE_HOME/lib/protobuf-java-2.4.0a.jar 拷贝到 $HIVE_HOME/lib/.

[zhouhh@Hadoop48 ~]$ cp /home/zhouhh/hbase-0.94.0/lib/protobuf-java-2.4.0a.jar $HIVE_HOME/lib/.

hive> CREATE TABLE hbase_table_1(key int, value string)

> STORED BY ‘org.apache.hadoop.hive.hbase.HBaseStorageHandler’

> WITH SERDEPROPERTIES (“hbase.columns.mapping” = “:key,cf1:val”)

> TBLPROPERTIES (“hbase.table.name” = “xyz”);

OK

Time taken: 10.492 seconds

hbase(main):002:0> list ‘xyz’

TABLE

xyz

1 row(s) in 0.0640 seconds

 

在引用 的jar包中包含protobuf-java-2.4.0a.jar便可。

 

 

测试脚本

bin/hive -hiveconf hbase.master=master:60000

 

hive --auxpath /usr/local/hive/lib/hive-hbase-handler-0.9.0.jar,/usr/local/hive/lib/hbase-0.94.7-security.jar,/usr/local/hive/lib/zookeeper-3.4.5.jar -hiveconf hbase.master=localhost:60000

 

<property>

 <name>hive.aux.jars.path</name>

<value>file:///usr/local/hive/lib/hive-hbase-handler-0.9.0.jar,file:///usr/local/hive/lib/hbase-0.94.7-security.jar,file:///usr/local/hive/lib/zookeeper-3.4.5.jar</value>

</property>

 

1 nana

2 hehe

3 xixi

hadoop dfsadmin -safemode leave

 

/home/hadoop

 

create table hbase_table_1(key int, value string) 

stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' 

with serdeproperties ("hbase.columns.mapping" = ":key,cf1:val") 

tblproperties ("hbase.table.name" = "xyz");

 

 

drop table pokes;

create table pokes

(id int,name string)

row format delimited fields terminated by ' '

stored as textfile;

load data local inpath '/home/hadoop/kv1.txt' overwrite into table pokes;

 

insert into table hbase_table_1

select * from pokes; 

 

 

 

create external table hive_test

(key int,gid map<string,string>,sid map<string,string>,uid map<string,string>)

stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'

with serdeproperties ("hbase.columns.mapping" ="a:,b:,c:") 

tblproperties  ("hbase.table.name" = "test1");

相关文章
相关标签/搜索