一:Hive與Hbase的整合過程。sql
1.建立HBase識別的表,而後查看錶是否建立成功。數據庫
hive:apache
hive> CREATE TABLE hivehbase(key int, value string)
> STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
> WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,columnfamily1:val")
> TBLPROPERTIES ("hbase.table.name" = "hbasehive");markdown
hive> CREATE TABLE hivehbase(key int, value string) > STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' > WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,columnfamily1:val") > TBLPROPERTIES ("hbase.table.name" = "hbasehive"); OK Time taken: 9.881 seconds hive> show tables; OK hivehbase testhive Time taken: 0.055 seconds, Fetched: 2 row(s)
hbase:app
hbase(main):002:0> list TABLE hbasehive
注意: hbase.table.name 定義在hbase的table名稱
hbase.columns.mapping 定義在hbase的列族 oop
2.使用sql往hivehbase中導入數據,將testhive表中的如下數據導入到hivehbase中,以下:學習
hive> select * from testhive; OK 1 Berg 1 Berg 2 Cccc 3 Xxxx 4 Jjjj
開始導入數據,即: 注意overwrite,相同的數據記錄會覆蓋點,複製。ui
hive> insert overwrite table hivehbase select * from testhive;spa
導入數據成功後,在查詢該表中數據,即:.net
hive> select * from hivehbase; OK 1 Berg 2 Cccc 3 Xxxx 4 Jjjj Time taken: 0.458 seconds, Fetched: 4 row(s)
而後進入hbase,查看hbase中hbasehive表中是否存在數據:
hbase(main):004:0> scan 'hbasehive' ROW COLUMN+CELL 1 column=columnfamily1:val, timestamp=1464514508295, value=Berg 2 column=columnfamily1:val, timestamp=1464514508295, value=Cccc 3 column=columnfamily1:val, timestamp=1464514508295, value=Xxxx 4 column=columnfamily1:val, timestamp=1464514508295, value=Jjjj 4 row(s) in 0.0470 seconds
能夠看到,在hive中往hivehbase中添加的數據,已經存在在hbase中的hbasehive表中了,
那反過來,在hbase中往hbasehive中添加的數據,在hive中的hivehbase可否查看到了?
往hbase中的hbasehive中添加一條數據記錄,以下:
hbase(main):007:0> put 'hbasehive', '5','columnfamily1:val','Yyyy'
而後在在hive中查看hivehbase中的表數據,能夠看到:
hive> select * from hivehbase;
OK
1 Berg
2 Cccc
3 Xxxx
4 Jjjj
5 Yyyy : 已經說明,在hbase中操做數據,也可以在hive中查詢到。
3.hive訪問已經存在的hbase,使用CREATE EXTERNAL TABLE,
在hbase中準備一張 student表,併爲其表中插入三條數據記錄,即:
hbase(main):026:0> create 'student','info'
hbase(main):028:0> put 'student','1','info:name','Berg'
hbase(main):029:0> put 'student','2','info:name','Hbase'
hbase(main):030:0> put 'student','3','info:name','hive'
使用CREATE EXTERNAL TABLE:
CREATE EXTERNAL TABLE hbase_person(key string, value string,value string,value int) STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" = "info:name,info:sex,info:age")
TBLPROPERTIES("hbase.table.name" = "person");
以下:
hive> CREATE EXTERNAL TABLE hivehbase_student(key int, value string)
> STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
> WITH SERDEPROPERTIES ("hbase.columns.mapping" = "info:name")
> TBLPROPERTIES("hbase.table.name" = "student");
OK
查看錶是否生成:
hive> show tables;
OK
hivehbase_student : 表示已經生成存在。
而後在查看hive中 這張表的數據:
hive> select * from hivehbase_student;
OK
1 Berg
2 Hbase
3 hive
結果表名,該數據跟hbase中student表中的數據一致。
****************************************************************************************************
完成上述操做後,在接着往下看:
二:多列和多列族(Multiple Columns and Families)
1.在hive中操做數據庫,以下:
先準備一張元數據表,multiplehive,往該表中存放多條數據記錄,操做以下:
hive> create table multiplehive > (id int, name string,sex string, age int) > ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' STORED AS TEXTFILE; OK Time taken: 0.179 seconds hive> load data local inpath '/home/hadoop/mytestdata/multiplehive.txt' into table multiplehive; Loading data to table default.multiplehive OK Time taken: 0.736 seconds hive> select * from multiplehive; OK 1 Berg f 21 2 BigData m 20 Time taken: 0.186 seconds, Fetched: 2 row(s)
CREATE TABLE hivehbase_person(key int, name string, sex string, age int)
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,info:name,info:sex,info:age")
TBLPROPERTIES("hbase.table.name" = "hbasehive_person");
hive> CREATE TABLE hivehbase_person(key int, name string, sex string, age int) > STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' > WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,info:name,info:sex,info:age") > TBLPROPERTIES("hbase.table.name" = "hbasehive_person"); OK
建立關聯表成功後,在hive中往表 hivehbase _person表中插入準備好的數據,即:
hive> insert overwrite table hivehbase_person select id,name,sex,age from multiplehive;
而後在查看這張表的數據,以下:
hive> select * from hivehbase_person; OK 1 Berg f 21 2 BigData m 20 Time taken: 0.679 seconds, Fetched: 2 row(s)
再去Hbase中,查看這張表的數據:hbase(main):039:0> scan 'hbasehive_person'
hbase(main):039:0> scan 'hbasehive_person' ROW COLUMN+CELL 1 column=info:age, timestamp=1464522188307, value=21 1 column=info:name, timestamp=1464522188307, value=Berg 1 column=info:sex, timestamp=1464522188307, value=f 2 column=info:age, timestamp=1464522188307, value=20 2 column=info:name, timestamp=1464522188307, value=BigData 2 column=info:sex, timestamp=1464522188307, value=m 2 row(s) in 8.1630 seconds
更多學習:
hbase的一些基本命令的操做:
http://my.oschina.net/gently/blog/674386 (這是本身寫的)
hive與hbase的整合:
http://blog.csdn.net/wulantian/article/details/38111683
hive導入數據的四種方式:
http://blog.csdn.net/lifuxiangcaohui/article/details/40588929
另外在收藏一個不錯的網址學習: