一文熟悉HiveServer2

1、HiveServer2基本概念

1.1、HiveServer2基本介紹

HiveServer2 (HS2) is a server interface that enables remote clients to execute queries against Hive and retrieve the results (a more detailed intro here). The current implementation, based on Thrift RPC, is an improved version of HiveServer and supports multi-client concurrency and authentication. It is designed to provide better support for open API clients like JDBC and ODBC.

​ HiveServer2是一個服務接口,能夠允許遠程的客戶端去執行SQL請求且得到檢索結果。HiveServer2的實現,依託於Thrift RPC,是HiveServer的提高版本,它被設計用來提供更好的支持對於open API例如JDBC和ODBC。

hiveserver2 端口號10000

10002端口,hiveserver2的webUI 界面

HiveServer is an optional service that allows a remote client to submit requests to Hive, using a variety of programming languages, and retrieve results. HiveServer is built on Apache ThriftTM (http://thrift.apache.org/), therefore it is sometimes called the Thrift server although this can lead to confusion because a newer service named HiveServer2 is also built on Thrift. Since the introduction of HiveServer2, HiveServer has also been called HiveServer1.

​ HiveServer是一個可選的服務,只允許一個遠程的客戶端去提交請求到hive中。(目前已被淘汰)

1.2、Beeline

​ HiveServer2 supports a command shell Beeline that works with HiveServer2. It’s a JDBC client that is based on the SQLLine CLI 。

​ HiveServer2提供了一種新的命令行接口,可以提交執行SQL語句。

2、hiveserver2的搭建使用

​ 在搭建hiveserver2服務的時候需要修改hdfs的超級用戶的管理權限,修改配置如下:

--在hdfs集羣的core-site.xml文件中添加如下配置文件
	<property>
		<name>hadoop.proxyuser.root.groups</name>	
		<value>*</value>
    </property>
    <property>
		<name>hadoop.proxyuser.root.hosts</name>	
		<value>*</value>
    </property>
    
--配置完成之後重新啓動集羣,或者在namenode的節點上執行如下命令
	hdfs dfsadmin -fs hdfs://node01:8020 -refreshSuperUserGroupsConfiguration
	hdfs dfsadmin -fs hdfs://node02:8020 -refreshSuperUserGroupsConfiguration

2.1、獨立hiveserver2模式

​ 1、將現有的所有hive的服務停止,不需要修改任何服務,在node03機器上執行hiveserver2或者hive --service hiveserver2的命令,開始啓動hiveserver2的服務,hiveserver2的服務也是一個阻塞式窗口,當開啓服務後,會開啓一個10000的端口,對外提供服務。

​ 2、在node04上使用beeline的方式進行登錄

2.2、共享metastore server的hiveserver2模式搭建

​ 1、在node03上執行hive --service metastore啓動元數據服務

​ 2、在node04上執行hiveserver2或者hive --service hiveserver2兩個命令其中一個都可以

​ 3、在任意一臺包含beeline腳本的虛擬機中執行beeline的命令進行連接

3、HiveServer2的訪問方式

3.1、beeline的訪問方式

​ (1)beeline -u jdbc:hive2://:/ -n name

​ (2)beeline進入到beeline的命令行
​ beeline> !connect jdbc:hive2://:/ root 123

​ 注意:

​ 1、使用beeline方式登錄的時候,默認的用戶名和密碼是不驗證的,也就是說隨便寫用戶名和密碼即可

​ 2、使用第一種beeline的方式訪問的時候,用戶名和密碼可以不輸入

​ 3、使用第二種beeline方式訪問的時候,必須輸入用戶名和密碼,但是用戶名和密碼是什麼無所謂

3.2、jdbc的訪問方式

​ 1、創建普通的java項目,將hive的jar包添加到classpath中,最精簡的jar包如下:

commons-lang-2.6.jar
commons-logging-1.2.jar
curator-client-2.7.1.jar
curator-framework-2.7.1.jar
guava-14.0.1.jar
hive-exec-2.3.4.jar
hive-jdbc-2.3.4.jar
hive-jdbc-handler-2.3.4.jar
hive-metastore-2.3.4.jar
hive-service-2.3.4.jar
hive-service-rpc-2.3.4.jar
httpclient-4.4.jar
httpcore-4.4.jar
libfb303-0.9.3.jar
libthrift-0.9.3.jar
log4j-1.2-api-2.6.2.jar
log4j-api-2.6.2.jar
log4j-core-2.6.2.jar
log4j-jul-2.5.jar
log4j-slf4j-impl-2.6.2.jar
log4j-web-2.6.2.jar
zookeeper-3.4.6.jar

​ 2、編輯如下代碼:


public class HiveJdbcClient {

	private static String driverName = "org.apache.hive.jdbc.HiveDriver";

	public static void main(String[] args) throws SQLException {
		try {
			Class.forName(driverName);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		Connection conn = DriverManager.getConnection("jdbc:hive2://node04:10000/default", "root", "");
		Statement stmt = conn.createStatement();
		String sql = "select * from psn limit 5";
		ResultSet res = stmt.executeQuery(sql);
		while (res.next()) {
			System.out.println(res.getString(1) + "-" + res.getString("name"));
		}
	}
}

運行之後,即可得到最終結果。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章