使用Hive如何和Hbase集成

主要讲的是使用Hive如何和Hbase集成,Hbase和Hive的底层存储都在HDFS上,都是hadoop生态系统中的重要一员,所以他们之间有着很亲密的联系,可以相互转换与操作。


hadoop,hbase和hive的搭建就不重复说了,不会的朋友,可以看散仙前面的博客,下面直接进入重点,关于hive集成hbase这一块,网上资料不算多,有的版本比较旧,散仙这里使用的版本是hive0.12和hbase0.96.2。

hive集成hbase的方式其实很简单,就是把Hbase的几个jar包,拷贝到hive的lib目录下即可截图如下:




上面的几个包放进Hive的lib中之后,我们需要配置在hive-site.xml里面配置下Hbase的zk地址,便于Hive连接Hbase使用,添加配置如下:

Xml代码 复制代码  收藏代码
  1. <property>  
  2. <name>hbase.zookeeper.quorum</name>  
  3. <value>h1,h2,h3</value>  
  4. </property>  
<property>
<name>hbase.zookeeper.quorum</name>
<value>h1,h2,h3</value>
</property>


下面可以进行测试,我们直接把Hbase里的一张表,转成我们的Hive表,Hive启动前,首先要把Hadoop和Hbase启动起来,然后启动Hive,启动完进程如下所示:

Java代码 复制代码  收藏代码
  1. [search@h1 ~]$ jps  
  2. 7950 RunJar  
  3. 6559 DataNode  
  4. 7691 HRegionServer  
  5. 6879 ResourceManager  
  6. 7563 HMaster  
  7. 6985 NodeManager  
  8. 7474 HQuorumPeer  
  9. 6731 SecondaryNameNode  
  10. 6458 NameNode  
  11. 8120 Jps  
  12. [search@h1 ~]$   
[search@h1 ~]$ jps
7950 RunJar
6559 DataNode
7691 HRegionServer
6879 ResourceManager
7563 HMaster
6985 NodeManager
7474 HQuorumPeer
6731 SecondaryNameNode
6458 NameNode
8120 Jps
[search@h1 ~]$ 


先在hbase里建一张表,并添加列簇,和列数据如下:



Java代码 复制代码  收藏代码
  1. hbase(main):001:0> list  
  2. TABLE                                                                                                                                                                             
  3. SLF4J: Class path contains multiple SLF4J bindings.  
  4. SLF4J: Found binding in [jar:file:/home/search/hbase-0.96.2-hadoop2/lib/slf4j-log4j12-1.6.4.jar!/org/slf4j/impl/StaticLoggerBinder.class]  
  5. SLF4J: Found binding in [jar:file:/home/search/hadoop/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]  
  6. SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.  
  7. mytest                                                                                                                                                                            
  8. webpage                                                                                                                                                                           
  9. 2 row(s) in 3.0870 seconds  
  10.   
  11. => ["mytest""webpage"]  
  12. hbase(main):002:0> scan 'mytest'  
  13. ROW                                           COLUMN+CELL                                                                                                                         
  14.  2207                                         column=item:name, timestamp=1407329588864, value=hadoop                                                                             
  15.  23008                                        column=home:address, timestamp=1407329589330, value=shenzhen                                                                        
  16.  2309                                         column=info:age, timestamp=1407329589497, value=899                                                                                 
  17.  24008                                        column=home:address, timestamp=1407329588587, value=guangzhou                                                                       
  18.  2407                                         column=item:name, timestamp=1407329588447, value=big data                                                                           
  19.  2409                                         column=info:age, timestamp=1407329588728, value=22.32                                                                               
  20.  3109                                         column=info:age, timestamp=1407329588308, value=7.4                                                                                 
  21.  321008                                       column=home:address, timestamp=1407329588169, value=luoyang                                                                         
  22.  33107                                        column=item:name, timestamp=1407329588028, value=x-code                                                                             
  23.  4205                                         column=home:address, timestamp=1407329587701, value=beijing                                                                         
  24.  44101                                        column=info:age, timestamp=1407329587883, value=18.5                                                                                
  25.  444105                                       column=item:name, timestamp=1407329587555, value=a dog                                                                              
  26. 12 row(s) in 0.0980 seconds  
hbase(main):001:0> list
TABLE                                                                                                                                                                           
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/search/hbase-0.96.2-hadoop2/lib/slf4j-log4j12-1.6.4.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/search/hadoop/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
mytest                                                                                                                                                                          
webpage                                                                                                                                                                         
2 row(s) in 3.0870 seconds

=> ["mytest", "webpage"]
hbase(main):002:0> scan 'mytest'
ROW                                           COLUMN+CELL                                                                                                                       
 2207                                         column=item:name, timestamp=1407329588864, value=hadoop                                                                           
 23008                                        column=home:address, timestamp=1407329589330, value=shenzhen                                                                      
 2309                                         column=info:age, timestamp=1407329589497, value=899                                                                               
 24008                                        column=home:address, timestamp=1407329588587, value=guangzhou                                                                     
 2407                                         column=item:name, timestamp=1407329588447, value=big data                                                                         
 2409                                         column=info:age, timestamp=1407329588728, value=22.32                                                                             
 3109                                         column=info:age, timestamp=1407329588308, value=7.4                                                                               
 321008                                       column=home:address, timestamp=1407329588169, value=luoyang                                                                       
 33107                                        column=item:name, timestamp=1407329588028, value=x-code                                                                           
 4205                                         column=home:address, timestamp=1407329587701, value=beijing                                                                       
 44101                                        column=info:age, timestamp=1407329587883, value=18.5                                                                              
 444105                                       column=item:name, timestamp=1407329587555, value=a dog                                                                            
12 row(s) in 0.0980 seconds



在Hive里,执行如下语句,进行关联建表:

Java代码 复制代码  收藏代码
  1. hive> CREATE EXTERNAL  TABLE hbaseive(key int, name string,address string,age double)             
  2.     > STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'       
  3.     > WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,item:name,home:address,info:age")  
  4.     > TBLPROPERTIES ("hbase.table.name" = "mytest");       
hive> CREATE EXTERNAL  TABLE hbaseive(key int, name string,address string,age double)           
    > STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'     
    > WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,item:name,home:address,info:age")
    > TBLPROPERTIES ("hbase.table.name" = "mytest"); 	


如果不报错,就证明,集成成功,如果出现错误,一般都是hbase的某个jar包缺少造成,我们根据提示添加到hive的lib里即可,然后重启hive的的shell客户端。


建表成功后,查询如下:


Java代码 复制代码  收藏代码
  1. hive> select * from hbaseive;  
  2. OK  
  3. 2207    hadoop  NULL    NULL  
  4. 23008   NULL    shenzhen        NULL  
  5. 2309    NULL    NULL    899.0  
  6. 24008   NULL    guangzhou       NULL  
  7. 2407    big data        NULL    NULL  
  8. 2409    NULL    NULL    22.32  
  9. 3109    NULL    NULL    7.4  
  10. 321008  NULL    luoyang NULL  
  11. 33107   x-code  NULL    NULL  
  12. 4205    NULL    beijing NULL  
  13. 44101   NULL    NULL    18.5  
  14. 444105  a dog   NULL    NULL  
  15. Time taken: 0.481 seconds, Fetched: 12 row(s)  
  16. hive>   
hive> select * from hbaseive;
OK
2207    hadoop  NULL    NULL
23008   NULL    shenzhen        NULL
2309    NULL    NULL    899.0
24008   NULL    guangzhou       NULL
2407    big data        NULL    NULL
2409    NULL    NULL    22.32
3109    NULL    NULL    7.4
321008  NULL    luoyang NULL
33107   x-code  NULL    NULL
4205    NULL    beijing NULL
44101   NULL    NULL    18.5
444105  a dog   NULL    NULL
Time taken: 0.481 seconds, Fetched: 12 row(s)
hive> 


至此,我们的Hive集成Hbase就成功了,关联成功后,我们就可以对数据进行分析了,需要注意的是,我们关联过的hbase表,如果在Hbase里面删除了这个表,那么在Hive里面再次查询,就会报错,如果删除了Hive里面的表,则对Hbase没有影响。另外一点需要注意的是,使用Hive关联Hbase的建的表,是一个内部表,所以需要在Create 后面加上 external关键字 。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值