以下基于上篇Hadoop2.6集群部署:http://lizhenliang.blog.51cto.com/7876557/1661354

接下來(lái)安裝Hadoop數(shù)據(jù)倉(cāng)庫(kù)Hive,上節(jié)了解HBase簡(jiǎn)單使用,聽(tīng)起來(lái)HBase與Hive有些類似,概念也有點(diǎn)模糊,那我們先了解下他們之間有什么區(qū)別:
HBase是一種分布式、面向列的NoSQL數(shù)據(jù)庫(kù),基于HDFS存儲(chǔ),以表的形式存儲(chǔ)數(shù)據(jù),表由行和列組成,列劃分到列族中。HBase不提供類SQL查詢語(yǔ)言,要想像SQL這樣查詢數(shù)據(jù),可以使用Phonix,讓SQL查詢轉(zhuǎn)換成hbase的掃描和對(duì)應(yīng)的操作,也可以使用現(xiàn)在說(shuō)講Hive倉(cāng)庫(kù)工具,讓HBase作為Hive存儲(chǔ)。
Hive是運(yùn)行在Hadoop之上的數(shù)據(jù)倉(cāng)庫(kù),將結(jié)構(gòu)化的數(shù)據(jù)文件映射為一張數(shù)據(jù)庫(kù)表,提供簡(jiǎn)單類SQL查詢語(yǔ)言,稱為HQL,并將SQL語(yǔ)句轉(zhuǎn)換成MapReduce任務(wù)運(yùn)算。有利于利用SQL語(yǔ)言查詢、分析數(shù)據(jù),適于處理不頻繁變動(dòng)的數(shù)據(jù)。Hive底層可以是HBase或者HDFS存儲(chǔ)的文件。
兩者都是基于Hadoop上不同的技術(shù),相互結(jié)合使用,可處理企業(yè)中不同類型的業(yè)務(wù),利用Hive處理非結(jié)構(gòu)化離線分析統(tǒng)計(jì),利用HBase處理在線查詢。
Hive三種元數(shù)據(jù)存儲(chǔ)方式:
1>.本地derby存儲(chǔ),只允許一個(gè)用戶連接Hive,適用于測(cè)試環(huán)境
2>.本地/遠(yuǎn)程MySQL存儲(chǔ),支持多用戶連接Hive,適用于生產(chǎn)環(huán)境
三、Hive安裝與配置(以下將元數(shù)據(jù)存儲(chǔ)到遠(yuǎn)程MySQL配置)
1.在MySQL創(chuàng)建Hive元數(shù)據(jù)存放庫(kù)和連接用戶
mysql> create database hive; mysql> grant all on *.* to'hive'@'%' identified by 'hive'; mysql> flush privileges;
2.安裝與配置Hive(在HMaster0安裝)
# tar zxvf apache-hive-1.2.0-bin.tar.gz # mv apache-hive-1.2.0-bin /opt
# vi hive-site.xml <configuration> <!--以下是MySQL連接信息--> <property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:mysql://192.168.18.210:3306/hive?createDatabaseIfNotExist=true</value> </property> <property> <name>javax.jdo.option.ConnectionDriverName</name> <value>com.mysql.jdbc.Driver</value> </property> <property> <name>javax.jdo.option.ConnectionUserName</name> <value>hive_user</value> </property> <property> <name>javax.jdo.option.ConnectionPassword</name> <value>hive_pass</value> </property> </configuration>
3.配置系統(tǒng)變量
# vi /etc/profile HIVE_HOME=/opt/apache-hive-1.2.0-bin PATH=$PATH:$HIVE_HOME/bin export HIVE_HOME PATH # source /etc/profile
4.啟動(dòng)Hive
# hive --service metastore & #啟動(dòng)遠(yuǎn)程模式,否則你只能在本地登錄
5.檢查是否正常啟動(dòng)
查看進(jìn)程是否啟動(dòng):
[root@HMaster0 ~]# jps 2615 DFSZKFailoverController 30027 ResourceManager 29656 NameNode 25451 Jps 10270 HMaster 14975 RunJar #會(huì)啟動(dòng)一個(gè)RunJar進(jìn)程
執(zhí)行hive命令會(huì)進(jìn)入命令界面:
[root@HMaster0 ~]# hive Logging initialized usingconfiguration in file:/opt/apache-hive-1.2.0-bin/conf/hive-log4j.properties hive> show databases; OK default Time taken: 0.986 seconds,Fetched: 1 row(s)
查看數(shù)據(jù)庫(kù),默認(rèn)有一個(gè)default庫(kù),現(xiàn)在就可以用你熟悉的SQL語(yǔ)言了。
6.客戶端連接Hive(必須有Hadoop環(huán)境)
# tar zxvf apache-hive-1.2.0-bin.tar.gz # mv apache-hive-1.2.0-bin /opt
# vi hive-site.xml <configuration> <!--通過(guò)thrift方式連接hive--> <property> <name>hive.metastore.uris</name> <value>thrift://192.168.18.215:9083</value> </property> </configuration>
配置好連接信息,連接命令行:
# /opt/apache-hive-1.2.0-bin/bin/hive
7.Hive常用SQL命令
7.1 先創(chuàng)建一個(gè)測(cè)試庫(kù)
hive> create database test;
7.2 創(chuàng)建tb1表,并指定字段分隔符為tab鍵(否則會(huì)插入NULL)
hive> create table tb1(id int,name string)row format delimited fields terminated by '\t'
如果想再創(chuàng)建一個(gè)表,而且表結(jié)構(gòu)和tb1一樣,可以這樣:
hive> create table tb3 like tb1;
查看下表結(jié)構(gòu):
hive> describe tb3; OK id int name string Time taken: 0.091 seconds, Fetched: 2 row(s)
7.3 從本地文件中導(dǎo)入數(shù)據(jù)到Hive表
先創(chuàng)建數(shù)據(jù)文件,鍵值要以tab鍵空格:
# cat kv.txt 1 zhangsan 2 lisi 3 wangwu
再導(dǎo)入數(shù)據(jù):
hive> load data local inpath'/root/kv.txt' overwrite into table tb1;
7.4 從HDFS中導(dǎo)入數(shù)據(jù)到Hive表
# hadoop fs -cat /kv.txt #查看hdfs中要導(dǎo)入的數(shù)據(jù) 1 zhangsan 2 lisi 3 wangwu hive> load data inpath '/kv.txt'overwrite into table tb1;
7.5 查詢是否導(dǎo)入成功
hive> select * from tb1; OK 1 zhangsan 2 lisi 3 wangwu Time taken: 0.209 seconds,Fetched: 3 row(s)
博客地址:http://lizhenliang.blog.51cto.com
上面是基本表的簡(jiǎn)單操作,為了提高處理性能,Hive引入了分區(qū)機(jī)制,那我們就了解分區(qū)表概念:
1>.分區(qū)表是在創(chuàng)建表時(shí)指定的分區(qū)空間
2>.一個(gè)表可以有一個(gè)或多個(gè)分區(qū),意思把數(shù)據(jù)劃分成塊
3>.分區(qū)以字段的形式在表結(jié)構(gòu)中,不存放實(shí)際數(shù)據(jù)內(nèi)容
分區(qū)表優(yōu)點(diǎn):將表中數(shù)據(jù)根據(jù)條件分配到不同的分區(qū)中,縮小查詢范圍,提高檢索速度和處理性能。
單分區(qū)表:
7.6 創(chuàng)建單分區(qū)表tb2(HDFS表目錄下只有一級(jí)目錄):
hive> create table tb2(idint,name string) partitioned by (dt string) row format delimited fieldsterminated by '\t';
注:dt可以理解為分區(qū)名稱。
7.7 從文件中把數(shù)據(jù)導(dǎo)入到Hive分區(qū)表,并定義分區(qū)信息
hive> load data local inpath '/root/kv.txt' into table tb2 partition (dt='2015-06-26'); hive> load data local inpath '/root/kv.txt' into table tb2 partition (dt='2015-06-27');
7.8 查看表數(shù)據(jù)
hive> select * from tb2; OK 1 zhangsan 2015-06-26 2 lisi 2015-06-26 3 wangwu 2015-06-26 1 zhangsan 2015-06-27 2 lisi 2015-06-27 3 wangwu 2015-06-27 Time taken: 0.223 seconds,Fetched: 6 row(s)
7.9 查看HDFS倉(cāng)庫(kù)中表目錄變化
# hadoop fs -ls -R /user/hive/warehouse/test.db/tb2 drwxr-xr-x - root supergroup 0 2015-06-26 04:12/user/hive/warehouse/test.db/tb2/dt=2015-06-26 -rwxr-xr-x 3 root supergroup 27 2015-06-26 04:12 /user/hive/warehouse/test.db/tb2/dt=2015-06-26/kv.txt drwxr-xr-x - root supergroup 0 2015-06-26 04:15/user/hive/warehouse/test.db/tb2/dt=2015-06-27 -rwxr-xr-x 3 root supergroup 27 2015-06-26 04:15/user/hive/warehouse/test.db/tb2/dt=2015-06-27/kv.txt
可以看到tb2表導(dǎo)入的數(shù)據(jù)根據(jù)日期將數(shù)據(jù)劃分到不同目錄下。
多分區(qū)表:
7.10 創(chuàng)建多分區(qū)表tb3(HDFS表目錄下有一級(jí)目錄,一級(jí)目錄下再有子級(jí)目錄)
hive> create table tb3(idint,name string) partitioned by (dt string,location string) row formatdelimited fields terminated by '\t';
7.11 從文件中把數(shù)據(jù)導(dǎo)入到Hive分區(qū)表,并定義分區(qū)信息
hive> load data local inpath '/root/kv.txt' into table tb3 partition (dt='2015-06- 26',location='beijing'); hive> load data local inpath '/root/kv.txt' into table tb3 partition (dt='2015-06-27',location='shanghai');
7.12 查看表數(shù)據(jù)
hive> select * from tb3; OK 1 zhangsan 2015-06-26 beijing 2 lisi 2015-06-26 beijing 3 wangwu 2015-06-26 beijing 1 zhangsan 2015-06-26 shanghai 2 lisi 2015-06-26 shanghai 3 wangwu 2015-06-26 shanghai Time taken: 0.208 seconds,Fetched: 6 row(s)
7.13 查看HDFS倉(cāng)庫(kù)中表目錄變化
# hadoop fs -ls -R /user/hive/warehouse/test.db/tb3
drwxr-xr-x - root supergroup 0 2015-06-26 04:35/user/hive/warehouse/test.db/tb3/dt=2015-06-26
drwxr-xr-x - root supergroup 0 2015-06-26 04:35 /user/hive/warehouse/test.db/tb3/dt=2015-06-26/location=beijing
-rwxr-xr-x 3 root supergroup 27 2015-06-26 04:35/user/hive/warehouse/test.db/tb3/dt=2015-06-26/location=beijing/kv.txt
drwxr-xr-x - root supergroup 0 2015-06-26 04:45 /user/hive/warehouse/test.db/tb3/dt=2015-06-27
drwxr-xr-x - root supergroup 0 2015-06-26 04:45/user/hive/warehouse/test.db/tb3/dt=2015-06-27/location=shanghai
-rwxr-xr-x 3 root supergroup 27 2015-06-26 04:45/user/hive/warehouse/test.db/tb3/dt=2015-06-27/location=shanghai/kv.txt
可以看到表中一級(jí)dt分區(qū)目錄下又分成了location分區(qū)。
7.14 查看表分區(qū)信息
hive> show partitions tb2;
7.15 根據(jù)分區(qū)查詢數(shù)據(jù)
hive> select name from tb3 where dt='2015-06-27';
7.16 重命名分區(qū)
hive> alter table tb3 partition (dt='2015-06-27',location='shanghai') rename to partition(dt='20150627',location='shanghai');
7.17 刪除分區(qū)
hive> alter table tb3 droppartition (dt='2015-06-26',location='shanghai');
7.18 模糊搜索表
hive> show tables 'tb*';
7.19 給表新添加一列
hive> alter table tb1 addcolumns (commnet string);
7.20 重命名表
hive> alter table tb1 rename to new_tb1;
7.21 刪除表
hive> drop table new_tb1;
8.啟動(dòng)過(guò)程中遇到錯(cuò)誤
報(bào)錯(cuò)1:
[ERROR]Terminal initialization failed; falling back to unsupported
java.lang.IncompatibleClassChangeError:Found class jline.Terminal, but interface was expected
解決方法,將hive/lib下jline包拷貝到hadoop/yarn/lib下:
# cp /opt/apache-hive-1.2.0-bin/lib/jline-2.12.jar /opt/hadoop-2.6.0/share/hadoop/yarn/lib/ # rm /opt/hadoop-2.6.0/share/hadoop/yarn/lib/jline-0.9.94.jar
報(bào)錯(cuò)2:
javax.jdo.JDOFatalInternalException:Error creating transactional connection factory
解決方法,在百度下載java連接MySQL包放到hive/lib下:
# cp mysql-connector-java-5.1.10-bin.jar /opt/apache-hive-1.2.0-bin/lib
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)頁(yè)題目:基于Hadoop數(shù)據(jù)倉(cāng)庫(kù)Hive1.2部署及使用-創(chuàng)新互聯(lián)
瀏覽路徑:http://chinadenli.net/article36/cdohsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、標(biāo)簽優(yōu)化、App設(shè)計(jì)、用戶體驗(yàn)、虛擬主機(jī)、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容