温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

hive与hbase数据交互的方法是什么

发布时间:2021-12-09 13:54:42 来源:亿速云 阅读:124 作者:iii 栏目:云计算

本篇内容介绍了“hive与hbase数据交互的方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

HBase和Hive的集成原理

ApacheCN | apache中文网

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

hive与hbase数据交互的方法是什么

图1 hive和hbase架构图

        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表:

hive与hbase数据交互的方法是什么

图2 Hive表映射HBase表

基本介绍

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

hive与hbase数据交互的方法是什么

软件版本

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

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

安装位置

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

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

整合步骤

整合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

测试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中插入的数据了。

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

“hive与hbase数据交互的方法是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI