翻譯: https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Overview
版本:2.3.3
介紹
HiveServer2(HS2)是一種服務(wù),它使客戶端能夠?qū)ive執(zhí)行查詢。HiveServer2的前身是 HiveServer1已被棄用。HS2支持多客戶端并發(fā)和身份驗(yàn)證。它旨在為JDBC和ODBC等開(kāi)放API客戶端提供更好的支持。
HS2是作為組合服務(wù)運(yùn)行的單個(gè)進(jìn)程,其中包括基于Thrift的Hive服務(wù)(TCP或HTTP)和用于Web UI 的Jetty Web服務(wù)器。
HS2架構(gòu)
基于Thrift的Hive服務(wù)是HS2的核心,負(fù)責(zé)為Hive查詢(例如,來(lái)自Beeline)提供服務(wù)。Thrift是用于構(gòu)建跨平臺(tái)服務(wù)的RPC框架。它的堆棧由4層組成:服務(wù)器,傳輸,協(xié)議和處理器。你可以在https://thrift.apache.org/docs/concepts找到更多的細(xì)節(jié)。
下面介紹這些層在HS2實(shí)現(xiàn)中的用法。
Server
HS2使用TThreadPoolServer(來(lái)自Thrift)實(shí)現(xiàn)TCP模式,或使用Jetty服務(wù)器實(shí)現(xiàn)HTTP模式。
TThreadPoolServer為每個(gè)TCP連接分配一個(gè)工作線程。即使連接閑置,每個(gè)線程也始終與連接相關(guān)聯(lián)。因此,由于大量的并發(fā)連接而導(dǎo)致大量線程產(chǎn)生潛在的性能問(wèn)題。未來(lái),HS2可能會(huì)切換到另一種服務(wù)器模型來(lái)實(shí)現(xiàn)TCP模式,例如TThreadedSelectorServer。以下是關(guān)于不同Thrift Java服務(wù)器之間性能比較的文章。
Transport
在客戶端和服務(wù)器之間需要代理(例如,出于負(fù)載平衡或安全原因)時(shí),需要使用HTTP模式。這就是為什么它被支持。您可以通過(guò)Hive配置屬性hive.server2.transport.mode指定Thrift服務(wù)的傳輸模式。
協(xié)議
協(xié)議實(shí)現(xiàn)負(fù)責(zé)序列化和反序列化。HS2目前使用 TBinaryProtocol作為T(mén)hrift序列化協(xié)議。未來(lái)可能會(huì)考慮其他協(xié)議,例如基于更多性能評(píng)估的TCompactProtocol。
處理器
流程實(shí)現(xiàn)是處理請(qǐng)求的應(yīng)用程序邏輯。例如,ThriftCLIService.ExecuteStatement()方法實(shí)現(xiàn)編譯和執(zhí)行Hive查詢的邏輯。
HS2的依賴性
Metastore Metastore
可以配置為嵌入式(與HS2相同的進(jìn)程),也可以配置為遠(yuǎn)程服務(wù)器(也是基于Thrift的服務(wù))。HS2與Metastore會(huì)查詢編譯所需的元數(shù)據(jù)。Hadoop集群
HS2為各種執(zhí)行引擎(MapReduce / Tez / Spark)準(zhǔn)備物理執(zhí)行計(jì)劃并將作業(yè)提交給Hadoop集群執(zhí)行。
你可以找到HS2和它的依賴之間的相互作用的圖在這里。

JDBC客戶端
建議客戶端使用JDBC驅(qū)動(dòng)程序與HS2進(jìn)行交互。請(qǐng)注意,有些使用案例(例如Hadoop Hue)繞過(guò)JDBC直接使用Thrift客戶端。
下面是一系列API調(diào)用,用于進(jìn)行第一個(gè)查詢:
- JDBC客戶端(例如Beeline)通過(guò)初始化傳輸連接(例如,TCP連接),然后通過(guò)OpenSession API調(diào)用來(lái)獲得SessionHandle來(lái)創(chuàng)建HiveConnection 。會(huì)話是從服務(wù)器端創(chuàng)建的。
- 執(zhí)行HiveStatement(遵循JDBC標(biāo)準(zhǔn)),并從Thrift客戶端執(zhí)行ExecuteStatement API調(diào)用。在API調(diào)用中,SessionHandle信息與查詢信息一起被傳遞給服務(wù)器。
- HS2服務(wù)器收到請(qǐng)求并要求驅(qū)動(dòng)程序(它是一個(gè)CommandProcessor)進(jìn)行查詢分析和編譯。驅(qū)動(dòng)程序啟動(dòng)后臺(tái)作業(yè),與Hadoop進(jìn)行對(duì)話,然后立即向客戶端返回響應(yīng)。這是ExecuteStatement API的異步設(shè)計(jì)。響應(yīng)包含從服務(wù)器端創(chuàng)建的OperationHandle。
- 客戶端使用OperationHandle與HS2進(jìn)行通信來(lái)輪詢查詢執(zhí)行的狀態(tài)。
源代碼描述
以下各節(jié)幫助您在源代碼中找到HiveServer2的一些基本組件。
服務(wù)器端
Thrift IDL file for TCLIService: https://github.com/apache/hive/blob/master/service-rpc/if/TCLIService.thrift.
TCL****IService****.Iface implemented by: org.apache.hive.service.cli.thrift.ThriftCLIService class.
ThriftCLIService subclassed by: *org.apache.hive.service.cli.thrift.ThriftBinaryCLIService *and org.apache.hive.service.cli.thrift.ThriftHttpCLIService for TCP mode and HTTP mode respectively.
**org.apache.hive.service.cli.thrift.EmbeddedThriftBinaryCLIService class: **Embedded mode for HS2. Don't get confused with embedded metastore, which is a different service (although the embedded mode concept is similar).
org.apache.hive.service.cli.session.HiveSessionImpl class: Instances of this class are created on the server side and managed by an org.apache.accumulo.tserver.TabletServer.SessionManager instance.
org.apache.hive.service.cli.operation.Operation class: Defines an operation (e.g., a query). Instances of this class are created on the server and managed by an org.apache.hive.service.cli.operation.OperationManager instance.
org.apache.hive.service.auth.HiveAuthFactory class: A helper used by both HTTP and TCP mode for authentication. Refer to Setting Up HiveServer2 for various authentication options, in particular Authentication/Security Configuration and Cookie Based Authentication.
客戶端
- org.apache.hive.jdbc.HiveConnection class: Implements the java.sql.Connection interface (part of JDBC). An instance of this class holds a reference to a SessionHandle instance which is retrieved when making Thrift API calls to the server.
- org.apache.hive.jdbc.HiveStatement class: Implements the java.sql.Statement interface (part of JDBC). The client (e.g., Beeline) calls the HiveStatement.execute() method for the query. Inside the execute() method, the Thrift client is used to make API calls.
- org.apache.hive.jdbc.HiveDriver class: Implements the java.sql.Driver interface (part of JDBC). The core method is connect() which is used by the JDBC client to initiate a SQL connection.
客戶端和服務(wù)器之間的交互
- org.apache.hive.service.cli.SessionHandle類(lèi):會(huì)話標(biāo)識(shí)符。此類(lèi)的實(shí)例從服務(wù)器返回,并由客戶端用作Thrift API調(diào)用的輸入。
- org.apache.hive.service.cli.OperationHandle類(lèi):操作標(biāo)識(shí)符。此類(lèi)的實(shí)例從服務(wù)器返回并由客戶端用于輪詢操作的執(zhí)行狀態(tài)。
資源
如何設(shè)置HS2: 設(shè)置HiveServer2
HS2客戶端: HiveServer2客戶端
用戶界面: HiveServer2的Web UI
指標(biāo): Hive指標(biāo)
HS2上的Cloudera博客:http : //blog.cloudera.com/blog/2013/07/how-hiveserver2-brings-security-and-concurrency-to-apache-hive/