hive 集成hbase 笔记

转载地址:http://blog.csdn.net/daniel_ustc/article/details/12795627 按这个博文安装成功,留个参考。

Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供完整的sql查询功能,可以将sql语句转换为

MapReduce任务进行运行。其优点学习成本低,可以通过类SQL语句快速实现简单的MapReduce统计,不必开发专门的MapReduce应用,

十分适合数据仓库的统计分析。Hive与HBase的整合功能的实现是利用两者本身对外的API接口互相进行通信,相互通信主要是依靠hive_hbase-handler.jar工具类.

Hive Hbase整合 见官网 Hive HBase Integration:https://cwiki.apache.org/confluence/display/Hive/HBaseIntegration

具体步骤整理如下:

集成步骤:

hbase 版本为 0.94.6.1 hive 0.11.0

1,首先将hbase-0.94.6.1/ 目录下的  hbase-0.94.6.1.jar 和 hbase-0.94.6.1/lib下的 zookeeper-3.3.5.jar复制到hive/lib目录下。

    注意:如果hive/lib下已经存在这两个文件的其他版本(例如zookeeper-3.3.3.jar),建议删除后使用hbase下的相关版本.

2,在hive/conf下hive-site.xml文件中添加如下的内容:

 如果hive/conf 目录下没有hive-site.xml 则把此目录下的hive-default.xml.template拷贝一份并命名 为hive-site.xml。

[html] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <property>  
  2.   <name>hive.aux.jars.path</name>    
  3.   <value>  
  4.          file:///root/hive-0.11.0/lib/hive-hbase-handler-0.11.0.jar,  
  5.          file:///root/hive-0.11.0/lib/hbase-0.94.6.1.jar,  
  6.          file:///root/hive-0.11.0/lib/zookeeper-3.3.5.jar  
  7.   </value>  
  8. </property>  

3. 拷贝hbase-0.94.6.1.jar到所有hadoop节点(包括master)的hadoop/lib下。

4. 拷贝hbase/conf下的hbase-site.xml文件到所有hadoop节点(包括master)的hadoop/conf下

注意,如果3,4两步跳过的话,运行hive时很可能出现如下错误:

FAILED: Error in metadata: MetaException(message:org.apache.hadoop.hbase.MasterNotRunningException: ubuntu.ubuntu-domain:60000
    at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.getMaster(HConnectionManager.java:394)
    at org.apache.hadoop.hbase.client.HBaseAdmin.<init>(HBaseAdmin.java:83)
    at org.apache.hadoop.hive.hbase.HBaseStorageHandler.getHBaseAdmin(HBaseStorageHandler.java:74)
    at org.apache.hadoop.hive.hbase.HBaseStorageHandler.preCreateTable(HBaseStorageHandler.java:158)
    at org.apache.hadoop.hive.metastore.HiveMetaStoreClient.createTable(HiveMetaStoreClient.java:344)

   不要忘记第3 4步,我曾经忘了这一步  搞了半天!

现在可以尝试启动Hive了。

单节点启动:
1  > bin/hive -hiveconf hbase.master=localhost:60000

集群启动:
1 > bin/hive -hiveconf hbase.zookeeper.quorum=slave1,slave2,slave3,slave4

测试:

 5,测试:

  A, 建立关联表,这里我们要查询的表在hbase中已经存在所以,使用CREATE EXTERNAL TABLE来建立,如下:

[html] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. CREATE EXTERNAL TABLE hbase_table_1(key string, value string)     
  2. STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'     
  3. WITH SERDEPROPERTIES ("hbase.columns.mapping" = "data:1")     
  4. TBLPROPERTIES("hbase.table.name" = "hbase_test");   

hbase.columns.mapping指向对应的列族;多列时,data:1,data:2;多列族时,data1:1,data2:1;

 hbase.table.name指向对应的表;hbase_table_2(key string, value string),这个是关联表。

我们看一下HBase中要查询的表的结构:

[html] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. hbase(main):001:0> describe 'hbase_test'    
  2. DESCRIPTION                                                             ENABLED                                   
  3.  {NAME => 'hbase_test', FAMILIES => [{NAME => 'data', COMPRESSION => 'NONE',  true                                      
  4.  VERSIONS => '3', TTL => '2147483647', BLOCKSIZE => '65536', IN_MEMORY                                            
  5.  => 'false', BLOCKCACHE => 'true'}]}                                                                              
  6. 1 row(s) in 0.0810 seconds    
  7. hbase(main):002:0>    

看一下Hbase表中的数据:

[html] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. hbase(main):002:0> scan 'hbase_test'    
  2. ROW                          COLUMN+CELL                                                                          
  3.  row11                        column=data:1, timestamp=1300847098583value=value11                                 
  4.  row12                       column=data:1, timestamp=1300849056637value=value12                                 
  5.  row21                        column=data:2, timestamp=1300847106880value=value21                                 
  6. 3 row(s) in 0.0160 seconds    
  7. hbase(main):003:0>     


列族:data:1、data:2两个, Key:row1、row12、row21,alue:value1、value12、value21

 hbase_table_1(key string, value string)中对应的test表中的row,value字段对应的是hbase_test表中的value

现在可以来看看查询结果了。

我们在hive命令行中先查看一下hbase_table_1:

[html] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. hive> select * from hbase_table_1;    
  2. OK    
  3. row11    value11    
  4. row12   value12    
  5. Time taken: 0.197 seconds    
  6. hive>    

对比一下hbase_test表中的列族为data:1的数据:

[html] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. row11                        column=data:1, timestamp=1300847098583value=value11                                 
  2. row12                       column=data:1, timestamp=1300849056637value=value12  


和查询结果相符。

B,创建hbase识别的数据库:

[html] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. CREATE TABLE test_hbase(key int, value string)  
  2. STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'  
  3. WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,cf1:val")  
  4. TBLPROPERTIES ("hbase.table.name" = "test_hive");   


hbase.table.name 定义在hbase的table名称,hbase.columns.mapping 定义在hbase的列族。

在hbase中查看表是否创建成功:

[plain] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. hbase(main):004:0> list  
  2. TABLE      
  3.   
  4. hbase_test                                                                         
  5. test_hive  
  6.   
  7. 1 row(s) in 0.0110 seconds  
  8.   
  9. hbase(main):005:0> describe 'test_hive'  
  10. DESCRIPTION                                          ENABLED                      
  11.  {NAME => 'test_hive', FAMILIES => [{NAME => 'cf1',  true                         
  12.  BLOOMFILTER => 'NONE', REPLICATION_SCOPE => '0', CO                              
  13.  MPRESSION => 'NONE', VERSIONS => '3', TTL => '21474                              
  14.  83647', BLOCKSIZE => '65536', IN_MEMORY => 'false',                              
  15.   BLOCKCACHE => 'true'}]}                                                         
  16. 1 row(s) in 0.0300 seconds  

导入数据:
我们不能直接使用load data来将数据导入到刚才创建的test_hbase表中,我们可以通过insert overwrite的方式实现数据的插入。

insert overwrite table test_hbase select * from test_join1;

在HBase中查看通过hive导入的数据是否成功scan 'test_hive'。

[plain] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. scan 'test_hive'  
  2. ROW                   COLUMN+CELL                                                 
  3.  1                    column=cf1:val, timestamp=1331278861290, value=SF     
  4.  2                    column=cf1:val, timestamp=1331278861290, value=DANE         
  5.  3                    column=cf1:val, timestamp=1331278861290, value=WANG       
  6.  4                    column=cf1:val, timestamp=1331278861290, value=JULY      
  7.  5                    column=cf1:val, timestamp=1331278861260, value=EVA       
  8.  6                    column=cf1:val, timestamp=1331278861260, value=USTC      
  9. 6 row(s) in 0.6230 seconds  

遇到的错误:

hive> show tables;

FAILED: Error in metadata: java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.metastore.HiveMetaStoreClient
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask

FAILED: Error in metadata: java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.metastore.HiveMetaStoreClientFAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask

debug 模式重新启动 hive,  运行命令:

./hive -hiveconf hive.root.logger=DEBUG,console

运行命令:

hive> show tables;

出现下面类似的错误:

hive error xsdb6 hive another instance of derby may have already booted the

解决方法:

到 hive-0.11.0 目录下把文件 夹 metestore_db 删除,重新启动 hive 即可 解决问题。

不过也可以 不使用默认的内嵌数据库derby,采用mysql作为统计的存储信息。详情键连接

点击打开链接

好,先到这。

参考;

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

http://victorzhzh.iteye.com/blog/972406

猜你喜欢

转载自xinghaifeng2006.iteye.com/blog/1998013