使用Hive读取Hbase中的数据
转自:http://www.4ucode.com/Study/Topic/1925466
第一步,启动hadoop,命令:./start-all.sh
第二步,启动hive,命令:
./hive --auxpath /home/dream-victor/hive-0.6.0/lib/hive_hbase-handler.jar,/home/dream-victor/hive-0.6.0/lib/hbase-0.20.3.jar,/home/dream-victor/hive-0.6.0/lib/zookeeper-3.2.2.jar -hiveconf hbase.master=127.0.0.1:60000
这里,-hiveconf hbase.master=指向自己在hbase-site.xml中hbase.master的值
第三步,启动hbase,命令:./start-hbase.sh
第四步,建立关联表,这里我们要查询的表在hbase中已经存在所以,使用CREATE EXTERNAL TABLE来建立,如下:
CREATE EXTERNAL TABLE hbase_table_2(key string, value string) STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' WITH SERDEPROPERTIES ("hbase.columns.mapping" = "data:1") TBLPROPERTIES("hbase.table.name" = "test");
hbase.columns.mapping指向对应的列族;多列时,data:1,data:2;多列族时,data1:1,data2:1;
hbase.table.name指向对应的表;
hbase_table_2(key string, value string),这个是关联表
我们看一下HBase中要查询的表的结构,
hbase(main):001:0> describe 'test' DESCRIPTION ENABLED {NAME => 'test', FAMILIES => [{NAME => 'data', COMPRESSION => 'NONE', true VERSIONS => '3', TTL => '2147483647', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}]} 1 row(s) in 0.0810 seconds hbase(main):002:0>
在看一下表中的数据,
hbase(main):002:0> scan 'test' ROW COLUMN+CELL row1 column=data:1, timestamp=1300847098583, value=value1 row12 column=data:1, timestamp=1300849056637, value=value3 row2 column=data:2, timestamp=1300847106880, value=value2 3 row(s) in 0.0160 seconds hbase(main):003:0>
列族:data:1、data:2两个
Key:row1、row12、row2
value:value1、value3、value2
hbase_table_2(key string, value string)中对应的test表中的row,value字段对应的是test表中的value
OK,现在可以来看看查询结果了,
我们在hive命令行中先查看一下hbase_table_2,
hive> select * from hbase_table_2; OK row1 value1 row12 value3 Time taken: 0.197 seconds hive>
对比一下test表中的列族为data:1的数据,
row1 column=data:1, timestamp=1300847098583, value=value1 row12 column=data:1, timestamp=1300849056637, value=value3
和查询结果相符,没问题,然后我们在hbase中在给列族data:1新增一条数据,
hbase(main):003:0> put 'test','row13','data:1','value4' 0 row(s) in 0.0050 seconds hbase(main):004:0>
再查看hbase_table_2表,
hive> select * from hbase_table_2; OK row1 value1 row12 value3 row13 value4 Time taken: 0.165 seconds hive>
新增数据value4出现了,说明可以通过hbase_table_2查询hbase的test表
下面我们来查询一下test表中value值为value3的数据,
hive> select * From hbase_table_2 where value='value3'; 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_201103231022_0001, Tracking URL = http://localhost:50030/jobdetails.jsp?jobid=job_201103231022_0001 Kill Command = /home/dream-victor/hadoop-0.20.2/bin/hadoop job -Dmapred.job.tracker=localhost:9001 -kill job_201103231022_0001 2011-03-23 11:23:27,807 Stage-1 map = 0%, reduce = 0% 2011-03-23 11:23:30,824 Stage-1 map = 100%, reduce = 0% 2011-03-23 11:23:33,854 Stage-1 map = 100%, reduce = 100% Ended Job = job_201103231022_0001 OK row12 value3 Time taken: 11.929 seconds hive>
和hbase的test表对比一下,
row12 column=data:1, timestamp=1300849056637, value=value3
OK,这样我们就可以使用SQL来对hbase进行查询了。
以上只是在命令行里左对应的查询,我们的目的是使用JAVA代码来查询出有用的数据,其实这个也很简单,
首先,启动Hive的命令有点变化,使用如下命令:
./hive --service hiveserver
这里我们默认使用嵌入的Derby数据库,这里可以在hive-site.xml文件中查看到:
<property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:derby:;databaseName=metastore_db;create=true</value>//指定了数据库默认的名字和地址 </property> <property> <name>javax.jdo.option.ConnectionDriverName</name> <value>org.apache.derby.jdbc.EmbeddedDriver</value> </property>
在此,数据库链接的URL可以使用默认的:jdbc:hive://localhost:10000/default
有了上面的准备,下面我们就可以使用JAVA代码来读取数据了,如下:
public class HiveTest extends TestCase { private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver"; private Connection con; private boolean standAloneServer = true; public void testSelect() throws SQLException { Statement stmt = con.createStatement(); ResultSet res = stmt.executeQuery("select * from hbase_table_2"); boolean moreRow = res.next(); while (moreRow) { System.out.println(res.getString(1)+","+res.getString(2)); moreRow = res.next(); } } @Override protected void setUp() throws Exception { super.setUp(); Class.forName(driverName); con = DriverManager.getConnection( "jdbc:hive://localhost:10000/default", "", ""); } }
结果,
row1,value1 row12,value3 row13,value4 row14,test
查看一下hbase中的结果,
ROW COLUMN+CELL row1 column=data:1, timestamp=1300847098583, value=value1 row12 column=data:1, timestamp=1300849056637, value=value3 row13 column=data:1, timestamp=1300850443699, value=value4 row14 column=data:1, timestamp=1300867550502, value=test
OK,完美了,不过还是希望这样的需求少一点,毕竟Hbase产生的初衷不是为了支持结构化查询。