Hadoop Hive与Hbase整合
jopen
13年前
用hbase做数据库,但由于hbase没有类sql查询方式,所以操作和计算数据非常不方便,于是整合hive,让hive支撑在hbase数据库层面 的 hql查询.hive也即 做数据仓库 <br /> <br /> 1. 基于Hadoop+Hive架构对海量数据进行查询:http://blog.csdn.net/kunshan_shenbin/article/details/7105319 <br /> 2. HBase 0.90.5 + Hadoop 1.0.0 集成:http://blog.csdn.net/kunshan_shenbin/article/details/7209990 <br /> 本文的目的是要讲述如何让Hbase和Hive能互相访问,让Hadoop/Hbase/Hive协同工作,合为一体。 <br /> 本文测试步骤主要参考自:http://running.iteye.com/blog/898399 <br /> 当然,这边博文也是按照官网的步骤来的:http://wiki.apache.org/hadoop/Hive/HBaseIntegration <br /> 1. 拷贝hbase-0.90.5.jar和zookeeper-3.3.2.jar到hive/lib下。 <br /> 注意:如何hive/lib下已经存在这两个文件的其他版本(例如zookeeper-3.3.1.jar),建议删除后使用hbase下的相关版本。 <br /> 2. 修改hive/conf下hive-site.xml文件,在底部添加如下内容: <br /> <pre class="brush:java; toolbar: true; auto-links: false;">[html] view plaincopy <!-- <property> <name>hive.exec.scratchdir</name> <value>/usr/local/hive/tmp</value> </property> --> <property> <name>hive.querylog.location</name> <value>/usr/local/hive/logs</value> </property> <property> <name>hive.aux.jars.path</name> <value>file:///usr/local/hive/lib/hive-hbase-handler-0.8.0.jar,file:///usr/local/hive/lib/hbase-0.90.5.jar,file:///usr/local/hive/lib/zookeeper-3.3.2.jar</value> </property> </pre> <br /> <br /> 注意:如果hive-site.xml不存在则自行创建,或者把hive-default.xml.template文件改名后使用。 <br /> 具体请参见:http://blog.csdn.net/kunshan_shenbin/article/details/7210020 <br /> <br /> 3. 拷贝hbase-0.90.5.jar到所有hadoop节点(包括master)的hadoop/lib下。 <br /> 4. 拷贝hbase/conf下的hbase-site.xml文件到所有hadoop节点(包括master)的hadoop/conf下。 <br /> 注意,hbase-site.xml文件配置信息参照:http://blog.csdn.net/kunshan_shenbin/article/details/7209990 <br /> 注意,如果3,4两步跳过的话,运行hive时很可能出现如下错误: <br /> <pre class="brush:java; toolbar: true; auto-links: false;">[html] view plaincopy 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. </pre> <br /> <br /> 参考:http://blog.sina.com.cn/s/blog_410d18710100vlbq.html <br /> <br /> 现在可以尝试启动Hive了。 <br /> 单节点启动: <br /> <pre class="brush:java; toolbar: true; auto-links: false;">> bin/hive -hiveconf hbase.master=master:60000</pre> <br /> 集群启动: <br /> <pre class="brush:java; toolbar: true; auto-links: false;">> bin/hive -hiveconf hbase.zookeeper.quorum=slave</pre> <br /> 如何hive-site.xml文件中没有配置hive.aux.jars.path,则可以按照如下方式启动。 <br /> <pre class="brush:java; toolbar: true; auto-links: false;">> bin/hive --auxpath /usr/local/hive/lib/hive-hbase-handler-0.8.0.jar, /usr/local/hive/lib/hbase-0.90.5.jar, /usr/local/hive/lib/zookeeper-3.3.2.jar -hiveconf hbase.zookeeper.quorum=slave</pre> <br /> <br /> 接下来可以做一些测试了。 <br /> 1.创建hbase识别的数据库: <br /> [sql] view plaincopy <br /> CREATE TABLE hbase_table_1(key int, value string) <br /> STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' <br /> WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,cf1:val") <br /> TBLPROPERTIES ("hbase.table.name" = "xyz"); <br /> hbase.table.name 定义在hbase的table名称 <br /> hbase.columns.mapping 定义在hbase的列族 <br /> 2.使用sql导入数据 <br /> a) 新建hive的数据表 <br /> [sql] view plaincopy <br /> <span><span></span></span>hive> CREATE TABLE pokes (foo INT, bar STRING); <br /> b) 批量插入数据 <br /> [sql] view plaincopy <br /> <pre class="brush:java; toolbar: true; auto-links: false;">hive> LOAD DATA LOCAL INPATH './examples/files/kv1.txt' OVERWRITE INTO TABLE </pre>pokes; <br /> c) 使用sql导入hbase_table_1 <br /> [sql] view plaincopy <br /> hive> INSERT OVERWRITE TABLE hbase_table_1 SELECT * FROM pokes WHERE foo=86; <br /> 3. 查看数据 <br /> [sql] view plaincopy <br /> hive> select * from hbase_table_1; <br /> 这时可以登录Hbase去查看数据了. <br /> > /usr/local/hbase/bin/hbase shell <br /> hbase(main):001:0> describe 'xyz' <br /> hbase(main):002:0> scan 'xyz' <br /> hbase(main):003:0> put 'xyz','100','cf1:val','www.360buy.com' <br /> 这时在Hive中可以看到刚才在Hbase中插入的数据了。 <br /> hive> select * from hbase_table_1 <br /> 4. hive访问已经存在的hbase <br /> 使用CREATE EXTERNAL TABLE <br /> [sql] view plaincopy <br /> CREATE EXTERNAL TABLE hbase_table_2(key int, value string) <br /> STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' <br /> WITH SERDEPROPERTIES ("hbase.columns.mapping" = "cf1:val") <br /> TBLPROPERTIES("hbase.table.name" = "some_existing_table"); <br /> <br /> <br /> 多列和多列族(Multiple Columns and Families) <br /> 1.创建数据库 <br /> Java代码 <br /> CREATE TABLE hbase_table_2(key int, value1 string, value2 int, value3 int) <br /> STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' <br /> WITH SERDEPROPERTIES ( <br /> "hbase.columns.mapping" = ":key,a:b,a:c,d:e" <br /> ); <br /> <br /> 2.插入数据 <br /> Java代码 <br /> INSERT OVERWRITE TABLE hbase_table_2 SELECT foo, bar, foo+1, foo+2 <br /> FROM pokes WHERE foo=98 OR foo=100; <br /> <br /> <br /> 这个有3个hive的列(value1和value2,value3),2个hbase的列族(a,d) <br /> Hive的2列(value1和value2)对应1个hbase的列族(a,在hbase的列名称b,c),hive的另外1列(value3)对应列(e)位于列族(d) <br /> <br /> 3.登录hbase查看结构 <br /> Java代码 <br /> <pre class="brush:java; toolbar: true; auto-links: false;">hbase(main):003:0> describe "hbase_table_2" DESCRIPTION ENABLED {NAME => 'hbase_table_2', FAMILIES => [{NAME => 'a', COMPRESSION => 'N true ONE', VERSIONS => '3', TTL => '2147483647', BLOCKSIZE => '65536', IN_M EMORY => 'false', BLOCKCACHE => 'true'}, {NAME => 'd', COMPRESSION => 'NONE', VERSIONS => '3', TTL => '2147483647', BLOCKSIZE => '65536', IN _MEMORY => 'false', BLOCKCACHE => 'true'}]} 1 row(s) in 1.0630 seconds </pre> <br /> <br /> 4.查看hbase的数据 <br /> Java代码 <br /> <pre class="brush:java; toolbar: true; auto-links: false;">hbase(main):004:0> scan 'hbase_table_2' ROW COLUMN+CELL 100 column=a:b, timestamp=1297695262015, value=val_100 100 column=a:c, timestamp=1297695262015, value=101 100 column=d:e, timestamp=1297695262015, value=102 98 column=a:b, timestamp=1297695242675, value=val_98 98 column=a:c, timestamp=1297695242675, value=99 98 column=d:e, timestamp=1297695242675, value=100 2 row(s) in 0.0380 seconds </pre> <br /> <br /> 5.在hive中查看 <br /> Java代码 <br /> <pre class="brush:java; toolbar: true; auto-links: false;">hive> select * from hbase_table_2; OK 100 val_100 101 102 98 val_98 99 100 Time taken: 3.238 seconds </pre> <br /> <br /> 参考资料: <br /> http://running.iteye.com/blog/898399 <br /> http://heipark.iteye.com/blog/1150648 <br /> http://www.javabloger.com/article/apache-hadoop-hive-hbase-integration.html <br />