說在前面的話
以下三種情況,最好是在3台集群里做,比如,master、slave1、slave2的master和slave1都安裝了hive,將master作為服務端,將slave1作為服務端。
以下,是針對CentOS版本的,若是Ubuntu版本,見我的博客
Ubuntu系統下安裝並配置hive-2.1.0
hive三種方式區別和搭建
Hive中metastore(元數據存儲)的三種方式:
a) 內嵌Derby方式
b) Local方式
c) Remote方式
1.本地derby
這種方式是最簡單的存儲方式,只需要在hive-site.xml做如下配置便可
<?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客戶端能使用數據庫,否則會提示如下錯誤。
[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、slave1、slave2集群。hive一般我是安裝在master上)(也叫作hive單用戶模式)
當然,你也來個master、slave1、slave2集群,外加client專門來安裝hive、sqoop、azkaban這樣的。
或者,你也來個master、slave1、slave2、slave3、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、slave1、slave2集群。hive一般我是安裝在master和slave1上)
或者,你也來個master、slave1、slave2、slave3、slave4集群,hive一般我也是安裝在master和slave1上。
1、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,見上。
步驟六: 環境變量生效。這些不多贅述。
若是出現問題,則見
1 復習ha相關 + weekend110的hive的元數據庫mysql方式安裝配置(完全正確配法)
客戶端這邊
步驟一:注意,不是這個
是這個
[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的架構,一目了然了。
1、CLI(command line interface)即命令行接口。
2、Thrift Server是Facebook開發的一個軟件框架,它用來開發可擴展且跨語言的服務,Hive集成了該服務,能讓不同的編程語言調用Hive的接口。
3、Hive客戶端提供了通過網頁的方式訪問Hive提供的服務,這個接口對應Hive的HWI組件(Hive web interface),使用前要啟動HWI服務。
4、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里編程,則需要
Hive項目開發環境搭建(Eclipse\MyEclipse + Maven)
步驟三:編寫號,程序代碼
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(大數據躺過的坑)