說在前面的話html
如下三種狀況,最好是在3臺集羣裏作,好比,master、slave一、slave2的master和slave1都安裝了hive,將master做爲服務端,將slave1做爲服務端。java
如下,是針對CentOS版本的,如果Ubuntu版本,見個人博客 python
hive三種方式區別和搭建
Hive中metastore(元數據存儲)的三種方式:
a) 內嵌Derby方式
b) Local方式
c) Remote方式mysql
1.本地derby
這種方式是最簡單的存儲方式,只須要在hive-site.xml作以下配置即可web
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <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> <property> <name>hive.metastore.local</name> <value>true</value> </property> <property> <name>hive.metastore.warehouse.dir</name> <value>/user/hive/warehouse</value> </property> </configuration>
注:使用derby存儲方式時,運行hive會在當前目錄生成一個derby文件和一個metastore_db目錄。這種存儲方式的弊端是在同一個目錄下同時只能有一個hive客戶端能使用數據庫,不然會提示以下錯誤。sql
[html] view plaincopyprint? hive> show tables; FAILED: Error in metadata: javax.jdo.JDOFatalDataStoreException: Failed to start database 'metastore_db', see the next exception for details. NestedThrowables: java.sql.SQLException: Failed to start database 'metastore_db', see the next exception for details. FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask hive> show tables; FAILED: Error in metadata: javax.jdo.JDOFatalDataStoreException: Failed to start database 'metastore_db', see the next exception for details. NestedThrowables: java.sql.SQLException: Failed to start database 'metastore_db', see the next exception for details. FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask
2.本地mysql (好比master、slave一、slave2集羣。hive通常我是安裝在master上)(也叫做hive單用戶模式)數據庫
固然,你也來個master、slave一、slave2集羣,外加client專門來安裝hive、sqoop、azkaban這樣的。apache
或者,你也來個master、slave一、slave二、slave三、slave4集羣,hive通常我也是安裝在master上。編程
這種存儲方式須要在本地運行一個mysql服務器,並做以下配置(下面兩種使用mysql的方式,須要將mysql的jar包拷貝到$HIVE_HOME/lib目錄下)。 緩存
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <property> <name>hive.metastore.warehouse.dir</name> <value>/user/hive_remote/warehouse</value> </property> <property> <name>hive.metastore.local</name> <value>true</value> </property> <property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:mysql://localhost/hive_remote?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</value> </property> <property> <name>javax.jdo.option.ConnectionPassword</name> <value>password</value> </property> </configuration>
3.遠端mysql (在主從上配)(也叫做hive多用戶模式)
(好比master、slave一、slave2集羣。hive通常我是安裝在master和slave1上)
或者,你也來個master、slave一、slave二、slave三、slave4集羣,hive通常我也是安裝在master和slave1上。
一、remote一體
這種存儲方式須要在遠端服務器運行一個mysql服務器,而且須要在Hive服務器啓動meta服務。
這裏用mysql的測試服務器,ip位192.168.1.214,新建hive_remote數據庫,字符集位latine1
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <property> <name>hive.metastore.warehouse.dir</name> <value>/user/hive/warehouse</value> </property> <property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:mysql://192.168.57.6: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</value> </property> <property> <name>javax.jdo.option.ConnectionPassword</name> <value>password</value> </property> <property> <name>hive.metastore.local</name> <value>false</value> </property> <property> <name>hive.metastore.uris</name> <value>thrift://192.168.1.188:9083</value> </property> </configuration>
注:這裏把hive的服務端和客戶端都放在同一臺服務器上了。服務端和客戶端能夠拆開。
2.Remote分開
將hive-site.xml配置文件拆爲以下兩部分
1)、服務端配置文件(好比在master)
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <property> <name>hive.metastore.warehouse.dir</name> <value>/user/hive/warehouse</value> </property> <property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:mysql://192.168.57.6: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>root</value> </property> <property> <name>javax.jdo.option.ConnectionPassword</name> <value>123456</value> </property> </configuration>
2)、客戶端配置文件(好比在slave1)
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <property> <name>hive.metastore.warehouse.dir</name> <value>/user/hive/warehouse</value> </property> <property> <name>hive.metastore.local</name> <value>false</value> </property> <property> <name>hive.metastore.uris</name> <value>thrift://192.168.57.5:9083</value> </property> </configuration>
啓動hive服務端程序
hive --service metastore
或者
hive --servie metastore -9083
客戶端直接使用hive命令便可。
root@my188:~$ hive Hive history file=/tmp/root/hive_job_log_root_201301301416_955801255.txt hive> show tables; OK test_hive Time taken: 0.736 seconds hive>
看hive的官方文檔
http://hive.apache.org/
服務端這邊
步驟一:[root@sparkmaster local]# yum -y install mysql-server
步驟二:[root@sparkmaster local]# service mysqld start
步驟三:
[root@sparkmaster local ] mysql -uroot -p
mysql> CREATE USER 'hive'@'%' IDENTIFIED BY 'hive';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'hive'@'%' WITH GRANT OPTION;
mysql> flush privileges;
mysql> exit;
[root@sparkmaster local]#
步驟四:去hive的安裝目錄下的lib,下將 mysql-connector-java-5.1.21.jar 傳到這個目錄下。
步驟五:去hive的安裝目錄下的conf,下配置hive-site.xml,見上。
步驟六: 環境變量生效。這些很少贅述。
如果出現問題,則見
客戶端這邊
步驟一:注意,不是這個
是這個
[root@sparkslave1 local]# yum -y install mysql-server
步驟二:[root@sparkslave1 local]# service mysqld start
步驟三:
[root@sparkslave1 local ] mysql -uroot -p
mysql> CREATE USER 'hive'@'%' IDENTIFIED BY 'hive';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'hive'@'%' WITH GRANT OPTION;
mysql> flush privileges;
mysql> exit;
[root@sparkslave1 local]#
步驟四:去hive的安裝目錄下的lib,下將 mysql-connector-java-5.1.21.jar 傳到這個目錄下。
步驟五:去hive的安裝目錄下的conf,下配置hive-site.xml,見上。
步驟六: 環境變量生效。這些很少贅述。
演示:
服務端和客戶端啓動hive
啓動hive服務端程序
hive --service metastore
客戶端直接使用hive命令便可
$ hive
hive> show tables;
OK
test_hive
Time taken: 0.736 seconds
hive>
HiveServer2測試
在hive安裝目錄下的bin目錄下,執行nohup hive --service hiveserver2 &
或者,任一目錄下,執行 $HIVE_HOME/bin/hiveserver2 或者 $HIVE_HOME/bin/hive --service hiveserver2
或者 $HIVE_HOME/bin/hive --service hiveserver2 10001 >/dev/null 2>/dev/null &
HWI測試
在hive安裝目錄下的bin目錄下,執行 hive --service hwi
CLI測試
在hive安裝目錄下的bin目錄下,執行 hive --service cli 或 ./hive
HiveServer2 & HWI & beeline 三大詳細講解
知識準備:bin/hiveserver2,這個是thrift服務器。
bin/beeline,這個是客戶端cli
其實,去看下Hive的架構,一目瞭然了。
一、CLI(command line interface)即命令行接口。
二、Thrift Server是Facebook開發的一個軟件框架,它用來開發可擴展且跨語言的服務,Hive集成了該服務,能讓不一樣的編程語言調用Hive的接口。
三、Hive客戶端提供了經過網頁的方式訪問Hive提供的服務,這個接口對應Hive的HWI組件(Hive web interface),使用前要啓動HWI服務。
四、Metastore是Hive中的元數據存儲,主要存儲Hive中的元數據,
包括表的名稱、表的列和分區及其屬性、表的屬性(是否爲外部表等)、表的數據所在目錄等,通常使用MySQL或Derby數據庫。
參考連接:
http://www.aboutyun.com/thread-12278-1-1.html
在以前的學習和實踐Hive中,使用的都是CLI或者hive –e的方式,該方式僅容許使用HiveQL執行查詢、更新等操做,而且該方式比較笨拙單一。幸虧Hive提供了輕客戶端的實現,經過HiveServer或者HiveServer2,客戶端能夠在不啓動CLI的狀況下對Hive中的數據進行操做,二者都容許遠程客戶端使用多種編程語言如Java、Python向Hive提交請求,取回結果。
HiveServer或者HiveServer2都是基於Thrift的,但HiveSever有時被稱爲Thrift server,而HiveServer2卻不會。
既然已經存在HiveServer爲何還須要HiveServer2呢?這是由於HiveServer不能處理多於一個客戶端的併發請求,這是因爲HiveServer使用的Thrift接口所致使的限制,不能經過修改HiveServer的代碼修正。所以在Hive-0.11.0版本中重寫了HiveServer代碼獲得了HiveServer2,進而解決了該問題。HiveServer2支持多客戶端的併發和認證,爲開放API客戶端如JDBC、ODBC提供了更好的支持。
既然HiveServer2提供了更強大的功能,將會對其進行着重學習,但也會簡單瞭解一下HiveServer的使用方法。在命令中輸入hive --service help,結果以下。從結果能夠了解到,可使用hive <parameters> --service serviceName <serviceparameters>啓動特定的服務,如cli、hiverserver、hiveserver2等。
[hadoop@hadoop~]$ hive --service help
Usage ./hive<parameters> --service serviceName <service parameters>
Service List: beelinecli help hiveserver2 hiveserver hwi jar lineage metastore metatool orcfiledumprcfilecat schemaTool version
Parametersparsed:
--auxpath : Auxillary jars
--config : Hive configuration directory
--service : Starts specificservice/component. cli is default
Parameters used:
HADOOP_HOME or HADOOP_PREFIX : Hadoop installdirectory
HIVE_OPT : Hive options
For help on aparticular service:
./hive --service serviceName --help
Debug help: ./hive --debug --help
在命令行輸入hive --service hiveserver –help查看hiveserver的幫助信息:
[hadoop@hadoop~]$ hive --service hiveserver --help
Starting Hive Thrift Server
usage:hiveserver
-h,--help Print help information
--hiveconf <property=value> Use value for given property
--maxWorkerThreads <arg> maximum number of worker threads,
default:2147483647
--minWorkerThreads <arg> minimum number of worker threads,
default:100
-p <port> Hive Server portnumber, default:10000
-v,--verbose Verbose mode
啓動hiveserver服務,能夠得知默認hiveserver運行在端口10000,最小100工做線程,最大2147483647工做線程。
[hadoop@hadoop~]$ hive --service hiveserver -v
Starting Hive Thrift Server
14/08/01 11:07:09WARN conf.HiveConf: DEPRECATED: hive.metastore.ds.retry.* no longer has anyeffect. Use hive.hmshandler.retry.*instead
Starting hive serveron port 10000 with 100 min worker threads and 2147483647 maxworker threads
接下來學習更強大的hiveserver2。Hiveserver2容許在配置文件hive-site.xml中進行配置管理,具體的參數爲:
hive.server2.thrift.min.worker.threads– 最小工做線程數,默認爲5。
hive.server2.thrift.max.worker.threads – 最小工做線程數,默認爲500。
hive.server2.thrift.port– TCP 的監聽端口,默認爲10000。
hive.server2.thrift.bind.host– TCP綁定的主機,默認爲localhost。
也能夠設置環境變量HIVE_SERVER2_THRIFT_BIND_HOST和HIVE_SERVER2_THRIFT_PORT覆蓋hive-site.xml設置的主機和端口號。
從Hive-0.13.0開始,HiveServer2支持經過HTTP傳輸消息,該特性當客戶端和服務器之間存在代理中介時特別有用。與HTTP傳輸相關的參數以下:
hive.server2.transport.mode – 默認值爲binary(TCP),可選值HTTP。
hive.server2.thrift.http.port– HTTP的監聽端口,默認值爲10001。
hive.server2.thrift.http.path – 服務的端點名稱,默認爲 cliservice。
hive.server2.thrift.http.min.worker.threads– 服務池中的最小工做線程,默認爲5。
hive.server2.thrift.http.max.worker.threads– 服務池中的最小工做線程,默認爲500。
啓動Hiveserver2有兩種方式
一種是上面已經介紹過的hive --service hiveserver2
另外一種更爲簡潔,爲hiveserver2。
使用hive--service hiveserver2 –H或hive--service hiveserver2 –help查看幫助信息:
Starting HiveServer2
Unrecognizedoption: -h
usage:hiveserver2
-H,--help Print help information
--hiveconf <property=value> Use value for given property
默認狀況下,HiveServer2以提交查詢的用戶執行查詢(true),若是hive.server2.enable.doAs設置爲false,查詢將以運行hiveserver2進程的用戶運行。爲了防止非加密模式下的內存泄露,能夠經過設置下面的參數爲true禁用文件系統的緩存:
fs.hdfs.impl.disable.cache – 禁用HDFS文件系統緩存,默認值爲false。
fs.file.impl.disable.cache – 禁用本地文件系統緩存,默認值爲false。
HiveServer2
客戶端能夠在不啓動CLI的狀況下對Hive中的數據進行操做。
步驟一:配置HiveServer2,便是配置Hive的JDBC接口啦
去修改hive-site.xml文件,固然默認大部分都配置好了,若出現什麼問題,去網上搜索查查再具體配置。
見https://cwiki.apache.org/confluence/display/Hive/Setting+up+HiveServer2
步驟二:啓動HiveServer2,默認是10000,
在hive的安裝目錄下,執行bin/hive --server hiveserver2
或執行bin/hiveserver2
或執行bin/hive --service Hiveserver2 &
固然也能夠以下這樣
bin/hive --service hiveserver2 --hiveconf hive.server2.thrift.port=10001
Hive與JDBC示例(很是重要,公司裏必須這麼幹)
在使用 JDBC 開發 Hive 程序時, 必須首先開啓 Hive 的遠程服務接口。使用下面命令進行開啓:
步驟一:在hive的安裝目錄下
bin/hive --service Hiveserver2 & //Hive0.11.0以上版本提供了的服務是:Hiveserver2
我這裏使用的Hive1.2.1版本,故咱們使用Hiveserver2服務,下面我使用 Java 代碼經過JDBC鏈接Hiveserver。
步驟二:準備好,測試數據
本地目錄/home/hadoop/下的djt.txt文件內容(每行數據之間用tab鍵隔開)以下所示:
1 dajiangtai
2 hadoop
3 Hive
4 hbase
5 spark
在此,好比你是在Eclipse裏或MyEclipse裏編程,則須要
步驟三:編寫號,程序代碼
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Hive {
private static String driverName = "org.apache.Hive.jdbc.HiveDriver";//Hive驅動名稱
private static String url = "jdbc:hive2://djt11:10000/default";//鏈接Hive2服務的鏈接地址
private static String user = "spark";//對HDFS有操做權限的用戶
private static String password = "spark";//在非安全模式下,指定一個用戶運行查詢,忽略密碼
private static String sql = "";
private static ResultSet res;
public static void main(String[] args) {
try {
Class.forName(driverName);//加載HiveServer2驅動程序
Connection conn = DriverManager.getConnection(url, user, password);//根據URL鏈接指定的數據庫
Statement stmt = conn.createStatement();
//建立的表名
String tableName = "testHiveDriverTable";
/** 第一步:表存在就先刪除 **/
sql = "drop table " + tableName;
stmt.execute(sql);
/** 第二步:表不存在就建立 **/
sql = "create table " + tableName + " (key int, value string) row format delimited fields terminated by '\t' STORED AS TEXTFILE";
stmt.execute(sql);
// 執行「show tables」操做
sql = "show tables '" + tableName + "'";
res = stmt.executeQuery(sql);
if (res.next()) {
System.out.println(res.getString(1));
}
// 執行「describe table」操做
sql = "describe " + tableName;
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getString(1) + "\t" + res.getString(2));
}
// 執行「load data into table」操做
String filepath = "/home/hadoop/djt.txt";//Hive服務所在節點的本地文件路徑
sql = "load data local inpath '" + filepath + "' into table " + tableName;
stmt.execute(sql);
// 執行「select * query」操做
sql = "select * from " + tableName;
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getInt(1) + "\t" + res.getString(2));
}
// 執行「regular Hive query」操做,此查詢會轉換爲MapReduce程序來處理
sql = "select count(*) from " + tableName;
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getString(1));
}
conn.close();
conn = null;
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
} catch (SQLException e) {
e.printStackTrace();
System.exit(1);
}
}
}
運行結果(右擊-->Run as-->Run on Hadoop)
執行「show tables」運行結果:
testHivedrivertable
執行「describe table」運行結果:
key int
value string
執行「select * query」運行結果:
1 dajiangtai
2 hadoop
3 Hive
4 hbase
5 spark
執行「regular Hive query」運行結果:
5
HWI環境搭建方法一
第二步:
第三步:
第四步:
第五步:
Hwi環境搭建方法二(與上面同樣的,自行選擇)
HWI是Hive Web Interface的簡稱,是hive cli的一個web替換方案。
http://blog.csdn.net/ckfflyingdream/article/details/50515837 感謝!
Hive Web Interface(HWI)簡介:Hive自帶了一個Web-GUI。但在lib下,是一個hive-hwi-1.2.1.jar,須要咱們本身製做。
怎麼製做出hive-hwi-*.*.*.war?
這裏,以hive-1.2.1位例。
下載源碼
下載地址:http://www.apache.org/dyn/closer.cgi/hive/
獲得apache-hive-1.2.1-src.tar.gz
打包
將源碼解壓:
tar -zxvf apache-hive-1.2.1-src.tar.gz
進入解壓後的目錄,再進入hwi目錄下:
cd apache-hive-1.2.1-src/hwi/
生成war包:
jar cvM hive-hwi-1.2.1.war -C web .
將生成的war包,拷貝到hive的lib目錄下,重啓hwi服務。
報錯解決
如有以下報錯,需將jre下的tools.jar包拷到Hive的lib目錄下,重啓hwi服務:
cp /usr/java/jdk1.7.0_79/lib/tools.jar /home/Big.Data/Hive/apache-hive-1.2.1-bin/lib/.
sh bin/hive --service hwi
----------------------------------------------------------------------------------------------------------------------
Problem accessing /hwi/. Reason:
Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK.
It is currently set to "/usr/java/jdk1.7.0_79/jre"
怎麼製做出hive-hwi-*.*.*.war?
須要下載Hive的源碼文件,而後將hwi/web目錄下的文件用 jar cvf hive-hwi-1.2.1.war ./*
其實war包也是zip包,能夠經過。
cd hwi/web
zip hive-hwi-1.2.1.zip ./* //打包成.zip文件。
將zip包後綴改爲war
mv hive-hwi-1.2.1.zip hive-hwi-1.2.1.war
cp hive-hwi-1.2.1.war /opt/sxt/soft/apache-hive-1.2.1-bin/lib/
命令來打包成一個war包,而後放到Hive的lib目錄下便可。
<property>
<name>hive.hwi.listen.host</name>
<value>0.0.0.0</value>
<description>This is the host address the Hive Web Interface will listen on</description>
</property>
<property>
<name>hive.hwi.listen.port</name>
<value>9999</value>
<description>This is the port the Hive Web Interface will listen on</description>
</property>
<property>
<name>hive.hwi.war.file</name>
<value>${env:HWI_WAR_FILE}</value>
<description>This sets the path to the HWI war file, relative to ${HIVE_HOME}. </description>
</property>
這是hive-1.2.1自帶的,須要修改爲下面部分。
配置文件conf/hive-site.xml,添加hive.hwi.war.file的配置:
<property>
<name>hive.hwi.listen.host</name>
<value>0.0.0.0</value>
<description>This is the host address the Hive Web Interface will listen on</description>
</property>
<property>
<name>hive.hwi.listen.port</name>
<value>9999</value>
<description>This is the port the Hive Web Interface will listen on</description>
</property>
<property>
<name>hive.hwi.war.file</name>
<value>lib/hive-hwi-1.2.1.war</value>
<description>This sets the path to the HWI war file, relative to ${HIVE_HOME}. </description>
</property>
啓動
$ sh bin/hive --service hwi
----------------------------------------------------------------------------------------------------
沒有UI war包的,須要本身下載對應版本的源碼進行打包,後拷到lib下。
其實這裏/lib/hive-hwi-1.2.1war,就是hive安裝目錄下。soga!
在配置文件中,監聽端口默認是9999,也能夠經過hive配置文件對端口進行修改。當配置完成後,
在hive的安裝目錄下,執行bin/hive --server hwi
對應地,http://masterIP:9999/hwi
Hive網絡接口操做實例
如,數據庫及表信息查詢、Hive查詢、等
可參照http://blog.csdn.net/wulantian/article/details/38271803
beeline環境搭建
步驟一:
在hive的安裝目錄下,執行bin/beeline,進入beeline,執行如下
!connect jdbc:hive2://localhost:10000 root org.apache.hive.jdbc.HiveDriver
同時,你們能夠關注個人我的博客:
http://www.cnblogs.com/zlslch/ 和 http://www.cnblogs.com/lchzls/
以及對應本平臺的QQ羣:161156071(大數據躺過的坑)