---
title: "Hive HBase 整合(中文) "
author: "thomas0988"
date: "2012-05-07T09:07:06Z"
tags: []
categories: ["数据库"]
slug: "hive-hbase"
draft: false
lastmod: "2012-05-07T09:07:06Z"
id: 1513668
url: "https://thomas0988.iteye.com/blog/1513668"
---

hive hbase整合，要求比较多，1.hive的得是0.6.0（当前最新的版本）  
2.hive本身要求hadoop的最高版本是hadoop-0.20.2  
3.要求hbase的版本是0.20.3，其他版本需要重新编译hive\_hbase-handler  
但是新版的hbase（0.90）变动特别大，根本无法从编译。这点比较恶心，hbase目前升级比较快，当前是0.90（从0.20.6直接跳到0.89），至于为什么这样跳跃，参考官方的解释http://wiki.apache.org/hadoop/Hbase/HBaseVersions

**1)启动Hbase,**  
要求hbase-0.20.3，zookeeper-3.2.2  
如果使用的不是hbase-0.20.3需要重新编译hive\_hbase-handler.jar

**2)单节点HBase的连接**  
./bin/hive -hiveconf hbase.master=master:60000

**3)集群HBase的连接**  
1.启动zookeeper  
2.启动hbase  
3.启动hive，添加zookeeper的支持

Java代码 [![复制代码](http://running.iteye.com/images/icon_copy.gif)](http://running.iteye.com/blog/898399 "复制代码") [![收藏代码](http://running.iteye.com/images/icon_star.png)![](http://running.iteye.com/images/spinner.gif)](http://running.iteye.com/blog/898399 "收藏这段代码")

1. ./bin/hive -hiveconf hbase.zookeeper.quorum= master,slave-A,slave-B

**\[java]** [view plain](http://blog.csdn.net/a221133/article/details/6728769 "view plain")[copy](http://blog.csdn.net/a221133/article/details/6728769 "copy")[print](http://blog.csdn.net/a221133/article/details/6728769 "print")[?](http://blog.csdn.net/a221133/article/details/6728769 "?")

1. ./bin/hive -hiveconf hbase.zookeeper.quorum= master,slave-A,slave-B

//所有的zookeeper节点

**二、插入数据**  
启动

Java代码 [![复制代码](http://running.iteye.com/images/icon_copy.gif)](http://running.iteye.com/blog/898399 "复制代码") [![收藏代码](http://running.iteye.com/images/icon_star.png)![](http://running.iteye.com/images/spinner.gif)](http://running.iteye.com/blog/898399 "收藏这段代码")

1. ./bin/hive --auxpath /data/soft/hive/lib/hive\_hbase-handler.jar,/data/soft/hive/lib/hbase-0.20.3.jar,/data/soft/hive/lib/zookeeper-3.2.2.jar  -hiveconf hbase.zookeeper.quorum=slave-001,slave-002,slave-003

**\[java]** [view plain](http://blog.csdn.net/a221133/article/details/6728769 "view plain")[copy](http://blog.csdn.net/a221133/article/details/6728769 "copy")[print](http://blog.csdn.net/a221133/article/details/6728769 "print")[?](http://blog.csdn.net/a221133/article/details/6728769 "?")

1. ./bin/hive --auxpath /data/soft/hive/lib/hive\_hbase-handler.jar,/data/soft/hive/lib/hbase-0.20.3.jar,/data/soft/hive/lib/zookeeper-3.2.2.jar  -hiveconf hbase.zookeeper.quorum=slave-001,slave-002,slave-003

hive  
**1.创建hbase识别的数据库**

Java代码 [![复制代码](http://running.iteye.com/images/icon_copy.gif)](http://running.iteye.com/blog/898399 "复制代码") [![收藏代码](http://running.iteye.com/images/icon_star.png)![](http://running.iteye.com/images/spinner.gif)](http://running.iteye.com/blog/898399 "收藏这段代码")

1. CREATE TABLE hbase\_table\_1(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" = "xyz");

**\[java]** [view plain](http://blog.csdn.net/a221133/article/details/6728769 "view plain")[copy](http://blog.csdn.net/a221133/article/details/6728769 "copy")[print](http://blog.csdn.net/a221133/article/details/6728769 "print")[?](http://blog.csdn.net/a221133/article/details/6728769 "?")

1. CREATE TABLE hbase\_table\_1(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" = "xyz");

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

**2.使用sql导入数据**  
**i.预先准备数据**  
a)新建hive的数据表

Java代码 [![复制代码](http://running.iteye.com/images/icon_copy.gif)](http://running.iteye.com/blog/898399 "复制代码") [![收藏代码](http://running.iteye.com/images/icon_star.png)![](http://running.iteye.com/images/spinner.gif)](http://running.iteye.com/blog/898399 "收藏这段代码")

1. CREATE TABLE pokes (foo INT, bar STRING);

**\[java]** [view plain](http://blog.csdn.net/a221133/article/details/6728769 "view plain")[copy](http://blog.csdn.net/a221133/article/details/6728769 "copy")[print](http://blog.csdn.net/a221133/article/details/6728769 "print")[?](http://blog.csdn.net/a221133/article/details/6728769 "?")

1. CREATE TABLE pokes (foo INT, bar STRING);

b）批量插入数据

Java代码 [![复制代码](http://running.iteye.com/images/icon_copy.gif)](http://running.iteye.com/blog/898399 "复制代码") [![收藏代码](http://running.iteye.com/images/icon_star.png)![](http://running.iteye.com/images/spinner.gif)](http://running.iteye.com/blog/898399 "收藏这段代码")

1. hive&gt; LOAD DATA LOCAL INPATH './examples/files/kv1.txt' OVERWRITE INTO TABLE pokes;

**\[java]** [view plain](http://blog.csdn.net/a221133/article/details/6728769 "view plain")[copy](http://blog.csdn.net/a221133/article/details/6728769 "copy")[print](http://blog.csdn.net/a221133/article/details/6728769 "print")[?](http://blog.csdn.net/a221133/article/details/6728769 "?")

1. hive&gt; LOAD DATA LOCAL INPATH './examples/files/kv1.txt' OVERWRITE INTO TABLE pokes;

这个文件位于hive的安装目录下，examples/files/kv1.txt

Java代码 [![复制代码](http://running.iteye.com/images/icon_copy.gif)](http://running.iteye.com/blog/898399 "复制代码") [![收藏代码](http://running.iteye.com/images/icon_star.png)![](http://running.iteye.com/images/spinner.gif)](http://running.iteye.com/blog/898399 "收藏这段代码")

1. ii.使用sql导入hbase\_table\_1

**\[java]** [view plain](http://blog.csdn.net/a221133/article/details/6728769 "view plain")[copy](http://blog.csdn.net/a221133/article/details/6728769 "copy")[print](http://blog.csdn.net/a221133/article/details/6728769 "print")[?](http://blog.csdn.net/a221133/article/details/6728769 "?")

1. ii.使用sql导入hbase\_table\_1

Java代码 [![复制代码](http://running.iteye.com/images/icon_copy.gif)](http://running.iteye.com/blog/898399 "复制代码") [![收藏代码](http://running.iteye.com/images/icon_star.png)![](http://running.iteye.com/images/spinner.gif)](http://running.iteye.com/blog/898399 "收藏这段代码")

1. INSERT OVERWRITE TABLE hbase\_table\_1 SELECT * FROM pokes WHERE foo=86;

**\[java]** [view plain](http://blog.csdn.net/a221133/article/details/6728769 "view plain")[copy](http://blog.csdn.net/a221133/article/details/6728769 "copy")[print](http://blog.csdn.net/a221133/article/details/6728769 "print")[?](http://blog.csdn.net/a221133/article/details/6728769 "?")

1. INSERT OVERWRITE TABLE hbase\_table\_1 SELECT * FROM pokes WHERE foo=86;

注意，默认的启动会报错的  
FAILED: Execution Error, return code 2 from org.apache.hadoop.hive.ql.exec.ExecDriver  
启动的时候要添加

Java代码 [![复制代码](http://running.iteye.com/images/icon_copy.gif)](http://running.iteye.com/blog/898399 "复制代码") [![收藏代码](http://running.iteye.com/images/icon_star.png)![](http://running.iteye.com/images/spinner.gif)](http://running.iteye.com/blog/898399 "收藏这段代码")

1. -auxpath /data/soft/hive/lib/hive\_hbase-handler.jar,/data/soft/hive/lib/hbase-0.20.3.jar,/data/soft/hive/lib/zookeeper-3.2.2.jar

**\[java]** [view plain](http://blog.csdn.net/a221133/article/details/6728769 "view plain")[copy](http://blog.csdn.net/a221133/article/details/6728769 "copy")[print](http://blog.csdn.net/a221133/article/details/6728769 "print")[?](http://blog.csdn.net/a221133/article/details/6728769 "?")

1. -auxpath /data/soft/hive/lib/hive\_hbase-handler.jar,/data/soft/hive/lib/hbase-0.20.3.jar,/data/soft/hive/lib/zookeeper-3.2.2.jar

**3查看数据**

Java代码 [![复制代码](http://running.iteye.com/images/icon_copy.gif)](http://running.iteye.com/blog/898399 "复制代码") [![收藏代码](http://running.iteye.com/images/icon_star.png)![](http://running.iteye.com/images/spinner.gif)](http://running.iteye.com/blog/898399 "收藏这段代码")

1. hive&gt; select * from  hbase\_table\_1;

**\[java]** [view plain](http://blog.csdn.net/a221133/article/details/6728769 "view plain")[copy](http://blog.csdn.net/a221133/article/details/6728769 "copy")[print](http://blog.csdn.net/a221133/article/details/6728769 "print")[?](http://blog.csdn.net/a221133/article/details/6728769 "?")

1. hive&gt; select * from  hbase\_table\_1;

会显示刚刚插入的数据  
86      val\_86

hbase  
**1.登录hbase**

Java代码 [![复制代码](http://running.iteye.com/images/icon_copy.gif)](http://running.iteye.com/blog/898399 "复制代码") [![收藏代码](http://running.iteye.com/images/icon_star.png)![](http://running.iteye.com/images/spinner.gif)](http://running.iteye.com/blog/898399 "收藏这段代码")

1. \[root@master hbase]# ./bin/hbase shell

**\[java]** [view plain](http://blog.csdn.net/a221133/article/details/6728769 "view plain")[copy](http://blog.csdn.net/a221133/article/details/6728769 "copy")[print](http://blog.csdn.net/a221133/article/details/6728769 "print")[?](http://blog.csdn.net/a221133/article/details/6728769 "?")

1. \[root@master hbase]# ./bin/hbase shell

**2.查看表结构**

Java代码 [![复制代码](http://running.iteye.com/images/icon_copy.gif)](http://running.iteye.com/blog/898399 "复制代码") [![收藏代码](http://running.iteye.com/images/icon_star.png)![](http://running.iteye.com/images/spinner.gif)](http://running.iteye.com/blog/898399 "收藏这段代码")

1. hbase(main):001:0&gt; describe 'xyz'
2. DESCRIPTION                                                             ENABLED
3. {NAME =&gt; 'xyz', FAMILIES =&gt; [{NAME =&gt; 'cf1', COMPRESSION =&gt; 'NONE', VE true
4. RSIONS =&gt; '3', TTL =&gt; '2147483647', BLOCKSIZE =&gt; '65536', IN\_MEMORY =&gt;
5. 'false', BLOCKCACHE =&gt; 'true'}]}
6. 1 row(s) in 0.7460 seconds

**\[java]** [view plain](http://blog.csdn.net/a221133/article/details/6728769 "view plain")[copy](http://blog.csdn.net/a221133/article/details/6728769 "copy")[print](http://blog.csdn.net/a221133/article/details/6728769 "print")[?](http://blog.csdn.net/a221133/article/details/6728769 "?")

1. hbase(main):001:0&gt; describe 'xyz'
2. DESCRIPTION                                                             ENABLED
3. {NAME =&gt; 'xyz', FAMILIES =&gt; [{NAME =&gt; 'cf1', COMPRESSION =&gt; 'NONE', VE true
4. RSIONS =&gt; '3', TTL =&gt; '2147483647', BLOCKSIZE =&gt; '65536', IN\_MEMORY =&gt;
5. 'false', BLOCKCACHE =&gt; 'true'}]}
6. 1 row(s) in 0.7460 seconds

**3.查看加载的数据**

Java代码 [![复制代码](http://running.iteye.com/images/icon_copy.gif)](http://running.iteye.com/blog/898399 "复制代码") [![收藏代码](http://running.iteye.com/images/icon_star.png)![](http://running.iteye.com/images/spinner.gif)](http://running.iteye.com/blog/898399 "收藏这段代码")

1. hbase(main):002:0&gt; scan 'xyz'
2. ROW                          COLUMN+CELL
3. 86                          column=cf1:val, timestamp=1297690405634, value=val\_86

**\[java]** [view plain](http://blog.csdn.net/a221133/article/details/6728769 "view plain")[copy](http://blog.csdn.net/a221133/article/details/6728769 "copy")[print](http://blog.csdn.net/a221133/article/details/6728769 "print")[?](http://blog.csdn.net/a221133/article/details/6728769 "?")

1. hbase(main):002:0&gt; scan 'xyz'
2. ROW                          COLUMN+CELL
3. 86                          column=cf1:val, timestamp=1297690405634, value=val\_86

                                                      
1 row(s) in 0.0540 seconds  
可以看到，在hive中添加的数据86，已经在hbase中了

**4.添加数据**

Java代码 [![复制代码](http://running.iteye.com/images/icon_copy.gif)](http://running.iteye.com/blog/898399 "复制代码") [![收藏代码](http://running.iteye.com/images/icon_star.png)![](http://running.iteye.com/images/spinner.gif)](http://running.iteye.com/blog/898399 "收藏这段代码")

1. ' hbase(main):008:0&gt; put 'xyz','100','cf1:val','www.360buy.com'
2. 0 row(s) in 0.0630 seconds

**\[java]** [view plain](http://blog.csdn.net/a221133/article/details/6728769 "view plain")[copy](http://blog.csdn.net/a221133/article/details/6728769 "copy")[print](http://blog.csdn.net/a221133/article/details/6728769 "print")[?](http://blog.csdn.net/a221133/article/details/6728769 "?")

1. ' hbase(main):008:0&gt; put 'xyz','100','cf1:val','www.360buy.com'
2. 0 row(s) in 0.0630 seconds

Hive  
**参看hive中的数据**

Java代码 [![复制代码](http://running.iteye.com/images/icon_copy.gif)](http://running.iteye.com/blog/898399 "复制代码") [![收藏代码](http://running.iteye.com/images/icon_star.png)![](http://running.iteye.com/images/spinner.gif)](http://running.iteye.com/blog/898399 "收藏这段代码")

1. hive&gt; select * from hbase\_table\_1;
2. OK
3. 100     www.360buy.com
4. 86      val\_86
5. Time taken: 8.661 seconds

**\[java]** [view plain](http://blog.csdn.net/a221133/article/details/6728769 "view plain")[copy](http://blog.csdn.net/a221133/article/details/6728769 "copy")[print](http://blog.csdn.net/a221133/article/details/6728769 "print")[?](http://blog.csdn.net/a221133/article/details/6728769 "?")

1. hive&gt; select * from hbase\_table\_1;
2. OK
3. 100     www.360buy.com
4. 86      val\_86
5. Time taken: 8.661 seconds

刚刚在hbase中插入的数据，已经在hive里了

**hive访问已经存在的hbase**  
使用CREATE EXTERNAL TABLE

Java代码 [![复制代码](http://running.iteye.com/images/icon_copy.gif)](http://running.iteye.com/blog/898399 "复制代码") [![收藏代码](http://running.iteye.com/images/icon_star.png)![](http://running.iteye.com/images/spinner.gif)](http://running.iteye.com/blog/898399 "收藏这段代码")

1. CREATE EXTERNAL TABLE hbase\_table\_2(key int, value string)
2. STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
3. WITH SERDEPROPERTIES ("hbase.columns.mapping" = "cf1:val")
4. TBLPROPERTIES("hbase.table.name" = "some\_existing\_table");

**\[java]** [view plain](http://blog.csdn.net/a221133/article/details/6728769 "view plain")[copy](http://blog.csdn.net/a221133/article/details/6728769 "copy")[print](http://blog.csdn.net/a221133/article/details/6728769 "print")[?](http://blog.csdn.net/a221133/article/details/6728769 "?")

1. CREATE EXTERNAL TABLE hbase\_table\_2(key int, value string)
2. STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
3. WITH SERDEPROPERTIES ("hbase.columns.mapping" = "cf1:val")
4. TBLPROPERTIES("hbase.table.name" = "some\_existing\_table");

**三、多列和多列族（Multiple Columns and Families）**  
**1．创建数据库**

Java代码 [![复制代码](http://running.iteye.com/images/icon_copy.gif)](http://running.iteye.com/blog/898399 "复制代码") [![收藏代码](http://running.iteye.com/images/icon_star.png)![](http://running.iteye.com/images/spinner.gif)](http://running.iteye.com/blog/898399 "收藏这段代码")

1. CREATE TABLE hbase\_table\_2(key int, value1 string, value2 int, value3 int)
2. STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
3. WITH SERDEPROPERTIES (
4. "hbase.columns.mapping" = ":key,a:b,a:c,d:e"
5. );

**\[java]** [view plain](http://blog.csdn.net/a221133/article/details/6728769 "view plain")[copy](http://blog.csdn.net/a221133/article/details/6728769 "copy")[print](http://blog.csdn.net/a221133/article/details/6728769 "print")[?](http://blog.csdn.net/a221133/article/details/6728769 "?")

1. CREATE TABLE hbase\_table\_2(key int, value1 string, value2 int, value3 int)
2. STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
3. WITH SERDEPROPERTIES (
4. "hbase.columns.mapping" = ":key,a:b,a:c,d:e"
5. );

**2.插入数据**

Java代码 [![复制代码](http://running.iteye.com/images/icon_copy.gif)](http://running.iteye.com/blog/898399 "复制代码") [![收藏代码](http://running.iteye.com/images/icon_star.png)![](http://running.iteye.com/images/spinner.gif)](http://running.iteye.com/blog/898399 "收藏这段代码")

1. INSERT OVERWRITE TABLE hbase\_table\_2 SELECT foo, bar, foo+1, foo+2
2. FROM pokes WHERE foo=98 OR foo=100;

**\[java]** [view plain](http://blog.csdn.net/a221133/article/details/6728769 "view plain")[copy](http://blog.csdn.net/a221133/article/details/6728769 "copy")[print](http://blog.csdn.net/a221133/article/details/6728769 "print")[?](http://blog.csdn.net/a221133/article/details/6728769 "?")

1. INSERT OVERWRITE TABLE hbase\_table\_2 SELECT foo, bar, foo+1, foo+2
2. FROM pokes WHERE foo=98 OR foo=100;

这个有3个hive的列（value1和value2，value3），2个hbase的列族（a，d）  
Hive的2列（value1和value2）对应1个hbase的列族（a，在hbase的列名称b，c），hive的另外1列（value3）对应列（e）位于列族（d）

**3.登录hbase查看结构**

Java代码 [![复制代码](http://running.iteye.com/images/icon_copy.gif)](http://running.iteye.com/blog/898399 "复制代码") [![收藏代码](http://running.iteye.com/images/icon_star.png)![](http://running.iteye.com/images/spinner.gif)](http://running.iteye.com/blog/898399 "收藏这段代码")

1. hbase(main):003:0&gt; describe "hbase\_table\_2"
2. DESCRIPTION                                                             ENABLED
3. {NAME =&gt; 'hbase\_table\_2', FAMILIES =&gt; [{NAME =&gt; 'a', COMPRESSION =&gt; 'N true
4. ONE', VERSIONS =&gt; '3', TTL =&gt; '2147483647', BLOCKSIZE =&gt; '65536', IN\_M
5. EMORY =&gt; 'false', BLOCKCACHE =&gt; 'true'}, {NAME =&gt; 'd', COMPRESSION =&gt;
6. 'NONE', VERSIONS =&gt; '3', TTL =&gt; '2147483647', BLOCKSIZE =&gt; '65536', IN
7. \_MEMORY =&gt; 'false', BLOCKCACHE =&gt; 'true'}]}
8. 1 row(s) in 1.0630 seconds

**\[java]** [view plain](http://blog.csdn.net/a221133/article/details/6728769 "view plain")[copy](http://blog.csdn.net/a221133/article/details/6728769 "copy")[print](http://blog.csdn.net/a221133/article/details/6728769 "print")[?](http://blog.csdn.net/a221133/article/details/6728769 "?")

1. hbase(main):003:0&gt; describe "hbase\_table\_2"
2. DESCRIPTION                                                             ENABLED
3. {NAME =&gt; 'hbase\_table\_2', FAMILIES =&gt; [{NAME =&gt; 'a', COMPRESSION =&gt; 'N true
4. ONE', VERSIONS =&gt; '3', TTL =&gt; '2147483647', BLOCKSIZE =&gt; '65536', IN\_M
5. EMORY =&gt; 'false', BLOCKCACHE =&gt; 'true'}, {NAME =&gt; 'd', COMPRESSION =&gt;
6. 'NONE', VERSIONS =&gt; '3', TTL =&gt; '2147483647', BLOCKSIZE =&gt; '65536', IN
7. \_MEMORY =&gt; 'false', BLOCKCACHE =&gt; 'true'}]}
8. 1 row(s) in 1.0630 seconds

**4.查看hbase的数据**

Java代码 [![复制代码](http://running.iteye.com/images/icon_copy.gif)](http://running.iteye.com/blog/898399 "复制代码") [![收藏代码](http://running.iteye.com/images/icon_star.png)![](http://running.iteye.com/images/spinner.gif)](http://running.iteye.com/blog/898399 "收藏这段代码")

1. hbase(main):004:0&gt; scan 'hbase\_table\_2'
2. ROW                          COLUMN+CELL
3. 100                         column=a:b, timestamp=1297695262015, value=val\_100
4. 100                         column=a:c, timestamp=1297695262015, value=101
5. 100                         column=d:e, timestamp=1297695262015, value=102
6. 98                          column=a:b, timestamp=1297695242675, value=val\_98
7. 98                          column=a:c, timestamp=1297695242675, value=99
8. 98                          column=d:e, timestamp=1297695242675, value=100
9. 2 row(s) in 0.0380 seconds

**\[java]** [view plain](http://blog.csdn.net/a221133/article/details/6728769 "view plain")[copy](http://blog.csdn.net/a221133/article/details/6728769 "copy")[print](http://blog.csdn.net/a221133/article/details/6728769 "print")[?](http://blog.csdn.net/a221133/article/details/6728769 "?")

1. hbase(main):004:0&gt; scan 'hbase\_table\_2'
2. ROW                          COLUMN+CELL
3. 100                         column=a:b, timestamp=1297695262015, value=val\_100
4. 100                         column=a:c, timestamp=1297695262015, value=101
5. 100                         column=d:e, timestamp=1297695262015, value=102
6. 98                          column=a:b, timestamp=1297695242675, value=val\_98
7. 98                          column=a:c, timestamp=1297695242675, value=99
8. 98                          column=d:e, timestamp=1297695242675, value=100
9. 2 row(s) in 0.0380 seconds

**5.在hive中查看**

Java代码 [![复制代码](http://running.iteye.com/images/icon_copy.gif)](http://running.iteye.com/blog/898399 "复制代码") [![收藏代码](http://running.iteye.com/images/icon_star.png)![](http://running.iteye.com/images/spinner.gif)](http://running.iteye.com/blog/898399 "收藏这段代码")

1. hive&gt; select * from hbase\_table\_2;
2. OK
3. 100     val\_100 101     102
4. 98      val\_98  99      100
5. Time taken: 3.238 seconds