這篇文章給大家介紹Hive中怎么讀取Hbase數(shù)據(jù),內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
為奉化等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及奉化網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站、奉化網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
第一步,啟動(dòng)hadoop,命令:./start-all.sh
第二步,啟動(dòng)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的值
第三步,啟動(dòng)hbase,命令:./start-hbase.sh
第四步,建立關(guān)聯(lián)表,這里我們要查詢的表在hbase中已經(jīng)存在所以,使用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指向?qū)?yīng)的列族;多列時(shí),data:1,data:2;多列族時(shí),data1:1,data2:1;
hbase.table.name指向?qū)?yīng)的表;
hbase_table_2(key string, value string),這個(gè)是關(guān)聯(lián)表
我們看一下HBase中要查詢的表的結(jié)構(gòu),
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>
在看一下表中的數(shù)據(jù),
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兩個(gè)
Key:row1、row12、row2
value:value1、value3、value2
hbase_table_2(key string, value string)中對應(yīng)的test表中的row,value字段對應(yīng)的是test表中的value
OK,現(xiàn)在可以來看看查詢結(jié)果了,
我們在hive命令行中先查看一下hbase_table_2,
hive> select * from hbase_table_2; OK row1 value1 row12 value3 Time taken: 0.197 seconds hive>
對比一下test表中的列族為data:1的數(shù)據(jù),
row1 column=data:1, timestamp=1300847098583, value=value1 row12 column=data:1, timestamp=1300849056637, value=value3
和查詢結(jié)果相符,沒問題,然后我們在hbase中在給列族data:1新增一條數(shù)據(jù),
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>
新增數(shù)據(jù)value4出現(xiàn)了,說明可以通過hbase_table_2查詢hbase的test表
下面我們來查詢一下test表中value值為value3的數(shù)據(jù),
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進(jìn)行查詢了。
以上只是在命令行里左對應(yīng)的查詢,我們的目的是使用JAVA代碼來查詢出有用的數(shù)據(jù),其實(shí)這個(gè)也很簡單,
首先,啟動(dòng)Hive的命令有點(diǎn)變化,使用如下命令:
./hive --service hiveserver
這里我們默認(rèn)使用嵌入的Derby數(shù)據(jù)庫,這里可以在hive-site.xml文件中查看到:
javax.jdo.option.ConnectionURL jdbc:derby:;databaseName=metastore_db;create=true //指定了數(shù)據(jù)庫默認(rèn)的名字和地址javax.jdo.option.ConnectionDriverName org.apache.derby.jdbc.EmbeddedDriver
在此,數(shù)據(jù)庫鏈接的URL可以使用默認(rèn)的:jdbc:hive://localhost:10000/default
有了上面的準(zhǔn)備,下面我們就可以使用JAVA代碼來讀取數(shù)據(jù)了,如下:
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", "", ""); } }
結(jié)果,
row1,value1 row12,value3 row13,value4 row14,test
查看一下hbase中的結(jié)果,
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
關(guān)于Hive中怎么讀取Hbase數(shù)據(jù)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。