您好,登錄后才能下訂單哦!
這篇文章給大家介紹Hive中怎么讀取Hbase數(shù)據(jù),內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
第一步,啟動(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來(lái)建立,如下:
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)中對(duì)應(yīng)的test表中的row,value字段對(duì)應(yīng)的是test表中的value
OK,現(xiàn)在可以來(lái)看看查詢結(jié)果了,
我們?cè)趆ive命令行中先查看一下hbase_table_2,
hive> select * from hbase_table_2; OK row1 value1 row12 value3 Time taken: 0.197 seconds hive>
對(duì)比一下test表中的列族為data:1的數(shù)據(jù),
row1 column=data:1, timestamp=1300847098583, value=value1 row12 column=data:1, timestamp=1300849056637, value=value3
和查詢結(jié)果相符,沒(méi)問(wèn)題,然后我們?cè)趆base中在給列族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)了,說(shuō)明可以通過(guò)hbase_table_2查詢hbase的test表
下面我們來(lái)查詢一下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表對(duì)比一下,
row12 column=data:1, timestamp=1300849056637, value=value3
OK,這樣我們就可以使用SQL來(lái)對(duì)hbase進(jìn)行查詢了。
以上只是在命令行里左對(duì)應(yīng)的查詢,我們的目的是使用JAVA代碼來(lái)查詢出有用的數(shù)據(jù),其實(shí)這個(gè)也很簡(jiǎn)單,
首先,啟動(dòng)Hive的命令有點(diǎn)變化,使用如下命令:
./hive --service hiveserver
這里我們默認(rèn)使用嵌入的Derby數(shù)據(jù)庫(kù),這里可以在hive-site.xml文件中查看到:
<property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:derby:;databaseName=metastore_db;create=true</value>//指定了數(shù)據(jù)庫(kù)默認(rèn)的名字和地址 </property> <property> <name>javax.jdo.option.ConnectionDriverName</name> <value>org.apache.derby.jdbc.EmbeddedDriver</value> </property>
在此,數(shù)據(jù)庫(kù)鏈接的URL可以使用默認(rèn)的:jdbc:hive://localhost:10000/default
有了上面的準(zhǔn)備,下面我們就可以使用JAVA代碼來(lái)讀取數(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)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。