You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kyuubi.apache.org by ch...@apache.org on 2022/07/01 08:06:01 UTC

[incubator-kyuubi] branch master updated: [KYUUBI #2917] Remove Hive service dependencies from Kyuubi Hive JDBC

This is an automated email from the ASF dual-hosted git repository.

chengpan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-kyuubi.git


The following commit(s) were added to refs/heads/master by this push:
     new 145a18db5 [KYUUBI #2917] Remove Hive service dependencies from Kyuubi Hive JDBC
145a18db5 is described below

commit 145a18db50e96ae398babed5bd22afa9a84ef763
Author: jiaoqingbo <11...@qq.com>
AuthorDate: Fri Jul 1 16:05:50 2022 +0800

    [KYUUBI #2917] Remove Hive service dependencies from Kyuubi Hive JDBC
    
    ### _Why are the changes needed?_
    
    fix #2917
    
    ### _How was this patch tested?_
    - [ ] Add some test cases that check the changes thoroughly including negative and positive cases if possible
    
    - [ ] Add screenshots for manual tests if appropriate
    
    - [x] [Run test](https://kyuubi.apache.org/docs/latest/develop_tools/testing.html#running-tests) locally before make a pull request
    
    Closes #2959 from jiaoqingbo/kyuubi-2917.
    
    Closes #2917
    
    93b4e619 [jiaoqingbo] delete unused code
    466d482a [jiaoqingbo] fix compile failed
    be5accd7 [jiaoqingbo] fix compile failed and remove annotation
    1561fa56 [jiaoqingbo] code review 2
    a46ed261 [jiaoqingbo] code review
    efcae807 [jiaoqingbo] modify package
    fead185c [jiaoqingbo] modify license
    f5d8b195 [jiaoqingbo] [KYUUBI #2917] Remove Hive service dependencies from Kyuubi Hive JDBC
    
    Authored-by: jiaoqingbo <11...@qq.com>
    Signed-off-by: Cheng Pan <ch...@apache.org>
---
 kyuubi-hive-jdbc/pom.xml                           |  26 +-
 .../jdbc/hive/HttpKerberosRequestInterceptor.java  |   2 +-
 .../apache/kyuubi/jdbc/hive/KyuubiConnection.java  |  25 +-
 .../kyuubi/jdbc/hive/KyuubiDatabaseMetaData.java   |   9 +-
 .../kyuubi/jdbc/hive/KyuubiQueryResultSet.java     |   4 +-
 .../apache/kyuubi/jdbc/hive/KyuubiStatement.java   |  12 +-
 .../java/org/apache/kyuubi/jdbc/hive/Utils.java    |   2 +-
 .../jdbc/hive/ZooKeeperHiveClientHelper.java       |   6 +-
 .../hive/auth/AuthenticationProviderFactory.java   |  52 ++
 .../jdbc/hive/auth/DelegationTokenIdentifier.java  |  34 +
 .../hive/auth/DelegationTokenSecretManager.java    |  53 ++
 .../jdbc/hive/auth/HadoopThriftAuthBridge.java     | 272 ++++++++
 .../jdbc/hive/auth/HadoopThriftAuthBridge23.java   | 110 ++++
 .../kyuubi/jdbc/hive/auth/HttpAuthUtils.java       | 108 ++++
 .../kyuubi/jdbc/hive/auth/KerberosSaslHelper.java  |  85 +++
 .../kyuubi/jdbc/hive/auth/PlainSaslHelper.java     |  77 +++
 .../kyuubi/jdbc/hive/auth/PlainSaslServer.java     | 175 ++++++
 .../org/apache/kyuubi/jdbc/hive/auth/SaslQOP.java  |  59 ++
 .../jdbc/hive/auth/TSubjectAssumingTransport.java  |  69 ++
 .../kyuubi/jdbc/hive/cli/ColumnBasedSet.java       | 179 ++++++
 .../apache/kyuubi/jdbc/hive/cli/ColumnBuffer.java  | 428 +++++++++++++
 .../apache/kyuubi/jdbc/hive/cli/ColumnValue.java   | 274 ++++++++
 .../org/apache/kyuubi/jdbc/hive/cli/FetchType.java |  46 ++
 .../kyuubi/jdbc/hive/cli/HiveSQLException.java     | 150 +++++
 .../apache/kyuubi/jdbc/hive/cli/RowBasedSet.java   | 137 ++++
 .../org/apache/kyuubi/jdbc/hive/cli/RowSet.java    |  37 ++
 .../apache/kyuubi/jdbc/hive/cli/RowSetFactory.java |  35 ++
 .../apache/kyuubi/jdbc/hive/cli/SessionUtils.java  |  70 +++
 .../apache/kyuubi/jdbc/hive/cli/StringUtils.java   |  40 ++
 .../kyuubi/jdbc/hive/cli/TypeDescriptor.java       | 172 +++++
 .../kyuubi/jdbc/hive/cli/TypeQualifiers.java       | 111 ++++
 .../kyuubi/jdbc/hive/server/AddressTypes.java      |  47 ++
 .../apache/kyuubi/jdbc/hive/server/CharTypes.java  | 230 +++++++
 .../apache/kyuubi/jdbc/hive/server/Endpoint.java   | 209 ++++++
 .../hive/server/HS2ActivePassiveHARegistry.java    | 227 +++++++
 .../server/HS2ActivePassiveHARegistryClient.java   |  63 ++
 .../kyuubi/jdbc/hive/server/HiveServer2.java       |  34 +
 .../jdbc/hive/server/HiveServer2HAInstanceSet.java |  30 +
 .../jdbc/hive/server/HiveServer2Instance.java      | 117 ++++
 .../jdbc/hive/server/InvalidRecordException.java   |  30 +
 .../kyuubi/jdbc/hive/server/JsonSerDeser.java      | 183 ++++++
 .../kyuubi/jdbc/hive/server/NoRecordException.java |  31 +
 .../jdbc/hive/server/RegistryIOException.java      |  37 ++
 .../kyuubi/jdbc/hive/server/RegistryTypeUtils.java | 156 +++++
 .../kyuubi/jdbc/hive/server/RegistryUtils.java     | 129 ++++
 .../jdbc/hive/server/ServiceInstanceBase.java      | 107 ++++
 .../kyuubi/jdbc/hive/server/ServiceRecord.java     | 291 +++++++++
 .../kyuubi/jdbc/hive/server/ZkRegistryBase.java    | 697 +++++++++++++++++++++
 .../kyuubi/jdbc/hive/server/ZookeeperUtils.java    |  99 +++
 49 files changed, 5532 insertions(+), 44 deletions(-)

diff --git a/kyuubi-hive-jdbc/pom.xml b/kyuubi-hive-jdbc/pom.xml
index d5af97358..f60183903 100644
--- a/kyuubi-hive-jdbc/pom.xml
+++ b/kyuubi-hive-jdbc/pom.xml
@@ -112,17 +112,6 @@
             </exclusions>
         </dependency>
 
-        <dependency>
-            <groupId>org.apache.hive</groupId>
-            <artifactId>hive-service</artifactId>
-            <version>${hive.version}</version>
-            <exclusions>
-                <exclusion>
-                    <groupId>*</groupId>
-                    <artifactId>*</artifactId>
-                </exclusion>
-            </exclusions>
-        </dependency>
 
         <dependency>
             <groupId>commons-codec</groupId>
@@ -210,6 +199,21 @@
             <artifactId>hadoop-client-api</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>org.apache.curator</groupId>
+            <artifactId>curator-recipes</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>com.fasterxml.jackson.core</groupId>
+            <artifactId>jackson-core</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>com.fasterxml.jackson.core</groupId>
+            <artifactId>jackson-databind</artifactId>
+        </dependency>
+
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/HttpKerberosRequestInterceptor.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/HttpKerberosRequestInterceptor.java
index 5282f7958..c799fb343 100644
--- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/HttpKerberosRequestInterceptor.java
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/HttpKerberosRequestInterceptor.java
@@ -20,11 +20,11 @@ package org.apache.kyuubi.jdbc.hive;
 import java.util.Map;
 import java.util.concurrent.locks.ReentrantLock;
 import javax.security.auth.Subject;
-import org.apache.hive.service.auth.HttpAuthUtils;
 import org.apache.http.HttpException;
 import org.apache.http.HttpRequest;
 import org.apache.http.client.CookieStore;
 import org.apache.http.protocol.HttpContext;
+import org.apache.kyuubi.jdbc.hive.auth.HttpAuthUtils;
 
 /**
  * Authentication interceptor which adds Base64 encoded payload, containing the username and
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiConnection.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiConnection.java
index f0ff3dba5..a284b4842 100644
--- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiConnection.java
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiConnection.java
@@ -31,7 +31,6 @@ import java.security.SecureRandom;
 import java.sql.*;
 import java.util.*;
 import java.util.Map.Entry;
-import java.util.Properties;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.locks.ReentrantLock;
 import javax.net.ssl.KeyManagerFactory;
@@ -42,14 +41,6 @@ import javax.security.sasl.Sasl;
 import javax.security.sasl.SaslException;
 import org.apache.commons.lang.StringUtils;
 import org.apache.hadoop.hive.common.auth.HiveAuthUtils;
-import org.apache.hive.service.auth.HiveAuthConstants;
-import org.apache.hive.service.auth.KerberosSaslHelper;
-import org.apache.hive.service.auth.PlainSaslHelper;
-import org.apache.hive.service.auth.SaslQOP;
-import org.apache.hive.service.cli.FetchType;
-import org.apache.hive.service.cli.RowSet;
-import org.apache.hive.service.cli.RowSetFactory;
-import org.apache.hive.service.cli.session.SessionUtils;
 import org.apache.hive.service.rpc.thrift.*;
 import org.apache.http.HttpRequestInterceptor;
 import org.apache.http.HttpResponse;
@@ -70,6 +61,13 @@ import org.apache.http.protocol.HttpContext;
 import org.apache.http.ssl.SSLContexts;
 import org.apache.kyuubi.jdbc.hive.Utils.JdbcConnectionParams;
 import org.apache.kyuubi.jdbc.hive.adapter.SQLConnection;
+import org.apache.kyuubi.jdbc.hive.auth.KerberosSaslHelper;
+import org.apache.kyuubi.jdbc.hive.auth.PlainSaslHelper;
+import org.apache.kyuubi.jdbc.hive.auth.SaslQOP;
+import org.apache.kyuubi.jdbc.hive.cli.FetchType;
+import org.apache.kyuubi.jdbc.hive.cli.RowSet;
+import org.apache.kyuubi.jdbc.hive.cli.RowSetFactory;
+import org.apache.kyuubi.jdbc.hive.cli.SessionUtils;
 import org.apache.kyuubi.jdbc.hive.logs.KyuubiLoggable;
 import org.apache.thrift.TException;
 import org.apache.thrift.protocol.TBinaryProtocol;
@@ -83,6 +81,8 @@ import org.slf4j.LoggerFactory;
 public class KyuubiConnection implements SQLConnection, KyuubiLoggable {
   public static final Logger LOG = LoggerFactory.getLogger(KyuubiConnection.class.getName());
   public static final String BEELINE_MODE_PROPERTY = "BEELINE_MODE";
+  public static final String HS2_PROXY_USER = "hive.server2.proxy.user";
+  public static final String HS2_CLIENT_TOKEN = "hiveserver2ClientToken";
   public static int DEFAULT_ENGINE_LOG_THREAD_TIMEOUT = 10 * 1000;
 
   private String jdbcUriString;
@@ -764,7 +764,7 @@ public class KyuubiConnection implements SQLConnection, KyuubiLoggable {
         jdbcConnConf.get(JdbcConnectionParams.AUTH_TYPE))) {
       // check delegation token in job conf if any
       try {
-        tokenStr = SessionUtils.getTokenStrForm(HiveAuthConstants.HS2_CLIENT_TOKEN);
+        tokenStr = SessionUtils.getTokenStrForm(HS2_CLIENT_TOKEN);
       } catch (IOException e) {
         throw new SQLException("Error reading token ", e);
       }
@@ -799,9 +799,8 @@ public class KyuubiConnection implements SQLConnection, KyuubiLoggable {
 
     // set the session configuration
     Map<String, String> sessVars = connParams.getSessionVars();
-    if (sessVars.containsKey(HiveAuthConstants.HS2_PROXY_USER)) {
-      openConf.put(
-          HiveAuthConstants.HS2_PROXY_USER, sessVars.get(HiveAuthConstants.HS2_PROXY_USER));
+    if (sessVars.containsKey(HS2_PROXY_USER)) {
+      openConf.put(HS2_PROXY_USER, sessVars.get(HS2_PROXY_USER));
     }
     openReq.setConfiguration(openConf);
 
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiDatabaseMetaData.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiDatabaseMetaData.java
index 07a4ee4c0..4bfad6d70 100644
--- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiDatabaseMetaData.java
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiDatabaseMetaData.java
@@ -28,11 +28,10 @@ import java.util.Arrays;
 import java.util.Comparator;
 import java.util.List;
 import java.util.jar.Attributes;
-import org.apache.hive.service.cli.GetInfoType;
-import org.apache.hive.service.cli.HiveSQLException;
 import org.apache.hive.service.rpc.thrift.*;
 import org.apache.kyuubi.jdbc.KyuubiHiveDriver;
 import org.apache.kyuubi.jdbc.hive.adapter.SQLDatabaseMetaData;
+import org.apache.kyuubi.jdbc.hive.cli.HiveSQLException;
 import org.apache.thrift.TException;
 
 /** KyuubiDatabaseMetaData. */
@@ -280,7 +279,7 @@ public class KyuubiDatabaseMetaData implements SQLDatabaseMetaData {
 
   @Override
   public String getDatabaseProductName() throws SQLException {
-    TGetInfoResp resp = getServerInfo(GetInfoType.CLI_DBMS_NAME.toTGetInfoType());
+    TGetInfoResp resp = getServerInfo(TGetInfoType.CLI_DBMS_NAME);
     return resp.getInfoValue().getStringValue();
   }
 
@@ -290,7 +289,7 @@ public class KyuubiDatabaseMetaData implements SQLDatabaseMetaData {
       return dbVersion;
     }
 
-    TGetInfoResp resp = getServerInfo(GetInfoType.CLI_DBMS_VER.toTGetInfoType());
+    TGetInfoResp resp = getServerInfo(TGetInfoType.CLI_DBMS_VER);
     this.dbVersion = resp.getInfoValue().getStringValue();
     return dbVersion;
   }
@@ -573,7 +572,7 @@ public class KyuubiDatabaseMetaData implements SQLDatabaseMetaData {
     // Note: the definitions of what ODBC and JDBC keywords exclude are different in different
     //       places. For now, just return the ODBC version here; that excludes Hive keywords
     //       that are also ODBC reserved keywords. We could also exclude SQL:2003.
-    TGetInfoResp resp = getServerInfo(GetInfoType.CLI_ODBC_KEYWORDS.toTGetInfoType());
+    TGetInfoResp resp = getServerInfo(TGetInfoType.CLI_ODBC_KEYWORDS);
     return resp.getInfoValue().getStringValue();
   }
 
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiQueryResultSet.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiQueryResultSet.java
index fc2d0370c..ebe04b745 100644
--- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiQueryResultSet.java
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiQueryResultSet.java
@@ -23,9 +23,9 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.concurrent.locks.ReentrantLock;
 import org.apache.hadoop.hive.common.type.HiveDecimal;
-import org.apache.hive.service.cli.RowSet;
-import org.apache.hive.service.cli.RowSetFactory;
 import org.apache.hive.service.rpc.thrift.*;
+import org.apache.kyuubi.jdbc.hive.cli.RowSet;
+import org.apache.kyuubi.jdbc.hive.cli.RowSetFactory;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiStatement.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiStatement.java
index f0df0566b..e6586c895 100644
--- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiStatement.java
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiStatement.java
@@ -18,18 +18,14 @@
 package org.apache.kyuubi.jdbc.hive;
 
 import com.google.common.annotations.VisibleForTesting;
-import java.sql.Connection;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.SQLTimeoutException;
-import java.sql.SQLWarning;
+import java.sql.*;
 import java.util.*;
 import org.apache.commons.lang.StringUtils;
-import org.apache.hive.service.cli.FetchType;
-import org.apache.hive.service.cli.RowSet;
-import org.apache.hive.service.cli.RowSetFactory;
 import org.apache.hive.service.rpc.thrift.*;
 import org.apache.kyuubi.jdbc.hive.adapter.SQLStatement;
+import org.apache.kyuubi.jdbc.hive.cli.FetchType;
+import org.apache.kyuubi.jdbc.hive.cli.RowSet;
+import org.apache.kyuubi.jdbc.hive.cli.RowSetFactory;
 import org.apache.kyuubi.jdbc.hive.logs.InPlaceUpdateStream;
 import org.apache.kyuubi.jdbc.hive.logs.KyuubiLoggable;
 import org.apache.thrift.TException;
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/Utils.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/Utils.java
index 75bce651d..13a7bc0a6 100644
--- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/Utils.java
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/Utils.java
@@ -24,11 +24,11 @@ import java.sql.SQLException;
 import java.util.*;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
-import org.apache.hive.service.cli.HiveSQLException;
 import org.apache.hive.service.rpc.thrift.TStatus;
 import org.apache.hive.service.rpc.thrift.TStatusCode;
 import org.apache.http.client.CookieStore;
 import org.apache.http.cookie.Cookie;
+import org.apache.kyuubi.jdbc.hive.cli.HiveSQLException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java
index 1d2c5ae6b..2c456cae0 100644
--- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/ZooKeeperHiveClientHelper.java
@@ -31,10 +31,10 @@ import org.apache.curator.framework.CuratorFrameworkFactory;
 import org.apache.curator.retry.ExponentialBackoffRetry;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.hive.conf.HiveConf;
-import org.apache.hive.service.server.HS2ActivePassiveHARegistry;
-import org.apache.hive.service.server.HS2ActivePassiveHARegistryClient;
-import org.apache.hive.service.server.HiveServer2Instance;
 import org.apache.kyuubi.jdbc.hive.Utils.JdbcConnectionParams;
+import org.apache.kyuubi.jdbc.hive.server.HS2ActivePassiveHARegistry;
+import org.apache.kyuubi.jdbc.hive.server.HS2ActivePassiveHARegistryClient;
+import org.apache.kyuubi.jdbc.hive.server.HiveServer2Instance;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/AuthenticationProviderFactory.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/AuthenticationProviderFactory.java
new file mode 100644
index 000000000..1a724da28
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/AuthenticationProviderFactory.java
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.auth;
+
+import javax.security.sasl.AuthenticationException;
+
+public final class AuthenticationProviderFactory {
+
+  public enum AuthMethods {
+    LDAP("LDAP"),
+    PAM("PAM"),
+    CUSTOM("CUSTOM"),
+    NONE("NONE");
+
+    private final String authMethod;
+
+    AuthMethods(String authMethod) {
+      this.authMethod = authMethod;
+    }
+
+    public String getAuthMethod() {
+      return authMethod;
+    }
+
+    public static AuthMethods getValidAuthMethod(String authMethodStr)
+        throws AuthenticationException {
+      for (AuthMethods auth : AuthMethods.values()) {
+        if (authMethodStr.equals(auth.getAuthMethod())) {
+          return auth;
+        }
+      }
+      throw new AuthenticationException("Not a valid authentication method");
+    }
+  }
+
+  private AuthenticationProviderFactory() {}
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/DelegationTokenIdentifier.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/DelegationTokenIdentifier.java
new file mode 100644
index 000000000..df560efcd
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/DelegationTokenIdentifier.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.auth;
+
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.security.token.delegation.AbstractDelegationTokenIdentifier;
+
+/** A delegation token identifier that is specific to Hive. */
+public class DelegationTokenIdentifier extends AbstractDelegationTokenIdentifier {
+  public static final Text HIVE_DELEGATION_KIND = new Text("HIVE_DELEGATION_TOKEN");
+
+  /** Create an empty delegation token identifier for reading into. */
+  public DelegationTokenIdentifier() {}
+
+  @Override
+  public Text getKind() {
+    return HIVE_DELEGATION_KIND;
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/DelegationTokenSecretManager.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/DelegationTokenSecretManager.java
new file mode 100644
index 000000000..bb675f938
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/DelegationTokenSecretManager.java
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.auth;
+
+import org.apache.hadoop.security.token.delegation.AbstractDelegationTokenSecretManager;
+
+/**
+ * A Hive specific delegation token secret manager. The secret manager is responsible for generating
+ * and accepting the password for each token.
+ */
+public class DelegationTokenSecretManager
+    extends AbstractDelegationTokenSecretManager<DelegationTokenIdentifier> {
+
+  /**
+   * Create a secret manager
+   *
+   * @param delegationKeyUpdateInterval the number of seconds for rolling new secret keys.
+   * @param delegationTokenMaxLifetime the maximum lifetime of the delegation tokens
+   * @param delegationTokenRenewInterval how often the tokens must be renewed
+   * @param delegationTokenRemoverScanInterval how often the tokens are scanned for expired tokens
+   */
+  public DelegationTokenSecretManager(
+      long delegationKeyUpdateInterval,
+      long delegationTokenMaxLifetime,
+      long delegationTokenRenewInterval,
+      long delegationTokenRemoverScanInterval) {
+    super(
+        delegationKeyUpdateInterval,
+        delegationTokenMaxLifetime,
+        delegationTokenRenewInterval,
+        delegationTokenRemoverScanInterval);
+  }
+
+  @Override
+  public DelegationTokenIdentifier createIdentifier() {
+    return new DelegationTokenIdentifier();
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/HadoopThriftAuthBridge.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/HadoopThriftAuthBridge.java
new file mode 100644
index 000000000..2f5068793
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/HadoopThriftAuthBridge.java
@@ -0,0 +1,272 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.auth;
+
+import static org.apache.hadoop.fs.CommonConfigurationKeys.HADOOP_SECURITY_AUTHENTICATION;
+
+import java.io.IOException;
+import java.security.PrivilegedExceptionAction;
+import java.util.Locale;
+import java.util.Map;
+import javax.security.auth.callback.*;
+import javax.security.sasl.RealmCallback;
+import javax.security.sasl.RealmChoiceCallback;
+import javax.security.sasl.SaslException;
+import org.apache.commons.codec.binary.Base64;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hive.thrift.client.TUGIAssumingTransport;
+import org.apache.hadoop.security.SaslRpcServer;
+import org.apache.hadoop.security.SaslRpcServer.AuthMethod;
+import org.apache.hadoop.security.SecurityUtil;
+import org.apache.hadoop.security.UserGroupInformation;
+import org.apache.hadoop.security.UserGroupInformation.AuthenticationMethod;
+import org.apache.hadoop.security.token.Token;
+import org.apache.hadoop.security.token.TokenIdentifier;
+import org.apache.thrift.transport.TSaslClientTransport;
+import org.apache.thrift.transport.TTransport;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Functions that bridge Thrift's SASL transports to Hadoop's SASL callback handlers and
+ * authentication classes. HIVE-11378 This class is not directly used anymore. It now exists only as
+ * a shell to be extended by HadoopThriftAuthBridge23 in 0.23 shims. I have made it abstract to
+ * avoid maintenance errors.
+ */
+public abstract class HadoopThriftAuthBridge {
+  private static final Logger LOG = LoggerFactory.getLogger(HadoopThriftAuthBridge.class);
+
+  // We want to have only one auth bridge.  In the past this was handled by ShimLoader, but since
+  // we're no longer using that we'll do it here.
+  private static HadoopThriftAuthBridge self = null;
+
+  public static HadoopThriftAuthBridge getBridge() {
+    if (self == null) {
+      synchronized (HadoopThriftAuthBridge.class) {
+        if (self == null) self = new HadoopThriftAuthBridge23();
+      }
+    }
+    return self;
+  }
+
+  public Client createClientWithConf(String authMethod) {
+    UserGroupInformation ugi;
+    try {
+      ugi = UserGroupInformation.getLoginUser();
+    } catch (IOException e) {
+      throw new IllegalStateException("Unable to get current login user: " + e, e);
+    }
+    if (loginUserHasCurrentAuthMethod(ugi, authMethod)) {
+      LOG.debug("Not setting UGI conf as passed-in authMethod of " + authMethod + " = current.");
+      return new Client();
+    } else {
+      LOG.debug("Setting UGI conf as passed-in authMethod of " + authMethod + " != current.");
+      Configuration conf = new Configuration();
+      conf.set(HADOOP_SECURITY_AUTHENTICATION, authMethod);
+      UserGroupInformation.setConfiguration(conf);
+      return new Client();
+    }
+  }
+
+  public String getServerPrincipal(String principalConfig, String host) throws IOException {
+    String serverPrincipal = SecurityUtil.getServerPrincipal(principalConfig, host);
+    String names[] = SaslRpcServer.splitKerberosName(serverPrincipal);
+    if (names.length != 3) {
+      throw new IOException(
+          "Kerberos principal name does NOT have the expected hostname part: " + serverPrincipal);
+    }
+    return serverPrincipal;
+  }
+
+  public UserGroupInformation getCurrentUGIWithConf(String authMethod) throws IOException {
+    UserGroupInformation ugi;
+    try {
+      ugi = UserGroupInformation.getCurrentUser();
+    } catch (IOException e) {
+      throw new IllegalStateException("Unable to get current user: " + e, e);
+    }
+    if (loginUserHasCurrentAuthMethod(ugi, authMethod)) {
+      LOG.debug("Not setting UGI conf as passed-in authMethod of " + authMethod + " = current.");
+      return ugi;
+    } else {
+      LOG.debug("Setting UGI conf as passed-in authMethod of " + authMethod + " != current.");
+      Configuration conf = new Configuration();
+      conf.set(HADOOP_SECURITY_AUTHENTICATION, authMethod);
+      UserGroupInformation.setConfiguration(conf);
+      return UserGroupInformation.getCurrentUser();
+    }
+  }
+
+  /**
+   * Return true if the current login user is already using the given authMethod.
+   *
+   * <p>Used above to ensure we do not create a new Configuration object and as such lose other
+   * settings such as the cluster to which the JVM is connected. Required for oozie since it does
+   * not have a core-site.xml see HIVE-7682
+   */
+  private boolean loginUserHasCurrentAuthMethod(UserGroupInformation ugi, String sAuthMethod) {
+    AuthenticationMethod authMethod;
+    try {
+      // based on SecurityUtil.getAuthenticationMethod()
+      authMethod =
+          Enum.valueOf(AuthenticationMethod.class, sAuthMethod.toUpperCase(Locale.ENGLISH));
+    } catch (IllegalArgumentException iae) {
+      throw new IllegalArgumentException(
+          "Invalid attribute value for " + HADOOP_SECURITY_AUTHENTICATION + " of " + sAuthMethod,
+          iae);
+    }
+    LOG.debug("Current authMethod = " + ugi.getAuthenticationMethod());
+    return ugi.getAuthenticationMethod().equals(authMethod);
+  }
+
+  /**
+   * Read and return Hadoop SASL configuration which can be configured using "hadoop.rpc.protection"
+   *
+   * @param conf
+   * @return Hadoop SASL configuration
+   */
+  public abstract Map<String, String> getHadoopSaslProperties(Configuration conf);
+
+  public static class Client {
+    /**
+     * Create a client-side SASL transport that wraps an underlying transport.
+     *
+     * @param methodStr The authentication method to use. Currently only KERBEROS is supported.
+     * @param principalConfig The Kerberos principal of the target server.
+     * @param underlyingTransport The underlying transport mechanism, usually a TSocket.
+     * @param saslProps the sasl properties to create the client with
+     */
+    public TTransport createClientTransport(
+        String principalConfig,
+        String host,
+        String methodStr,
+        String tokenStrForm,
+        final TTransport underlyingTransport,
+        final Map<String, String> saslProps)
+        throws IOException {
+      final AuthMethod method = AuthMethod.valueOf(AuthMethod.class, methodStr);
+
+      TTransport saslTransport = null;
+      switch (method) {
+        case DIGEST:
+          Token<DelegationTokenIdentifier> t = new Token<>();
+          t.decodeFromUrlString(tokenStrForm);
+          saslTransport =
+              new TSaslClientTransport(
+                  method.getMechanismName(),
+                  null,
+                  null,
+                  SaslRpcServer.SASL_DEFAULT_REALM,
+                  saslProps,
+                  new SaslClientCallbackHandler(t),
+                  underlyingTransport);
+          return new TUGIAssumingTransport(saslTransport, UserGroupInformation.getCurrentUser());
+
+        case KERBEROS:
+          String serverPrincipal = SecurityUtil.getServerPrincipal(principalConfig, host);
+          final String names[] = SaslRpcServer.splitKerberosName(serverPrincipal);
+          if (names.length != 3) {
+            throw new IOException(
+                "Kerberos principal name does NOT have the expected hostname part: "
+                    + serverPrincipal);
+          }
+          try {
+            return UserGroupInformation.getCurrentUser()
+                .doAs(
+                    new PrivilegedExceptionAction<TUGIAssumingTransport>() {
+                      @Override
+                      public TUGIAssumingTransport run() throws IOException {
+                        TTransport saslTransport =
+                            new TSaslClientTransport(
+                                method.getMechanismName(),
+                                null,
+                                names[0],
+                                names[1],
+                                saslProps,
+                                null,
+                                underlyingTransport);
+                        return new TUGIAssumingTransport(
+                            saslTransport, UserGroupInformation.getCurrentUser());
+                      }
+                    });
+          } catch (InterruptedException | SaslException se) {
+            throw new IOException("Could not instantiate SASL transport", se);
+          }
+
+        default:
+          throw new IOException("Unsupported authentication method: " + method);
+      }
+    }
+
+    private static class SaslClientCallbackHandler implements CallbackHandler {
+      private final String userName;
+      private final char[] userPassword;
+
+      public SaslClientCallbackHandler(Token<? extends TokenIdentifier> token) {
+        this.userName = encodeIdentifier(token.getIdentifier());
+        this.userPassword = encodePassword(token.getPassword());
+      }
+
+      @Override
+      public void handle(Callback[] callbacks) throws UnsupportedCallbackException {
+        NameCallback nc = null;
+        PasswordCallback pc = null;
+        RealmCallback rc = null;
+        for (Callback callback : callbacks) {
+          if (callback instanceof RealmChoiceCallback) {
+            continue;
+          } else if (callback instanceof NameCallback) {
+            nc = (NameCallback) callback;
+          } else if (callback instanceof PasswordCallback) {
+            pc = (PasswordCallback) callback;
+          } else if (callback instanceof RealmCallback) {
+            rc = (RealmCallback) callback;
+          } else {
+            throw new UnsupportedCallbackException(callback, "Unrecognized SASL client callback");
+          }
+        }
+        if (nc != null) {
+          if (LOG.isDebugEnabled()) {
+            LOG.debug("SASL client callback: setting username: " + userName);
+          }
+          nc.setName(userName);
+        }
+        if (pc != null) {
+          if (LOG.isDebugEnabled()) {
+            LOG.debug("SASL client callback: setting userPassword");
+          }
+          pc.setPassword(userPassword);
+        }
+        if (rc != null) {
+          if (LOG.isDebugEnabled()) {
+            LOG.debug("SASL client callback: setting realm: " + rc.getDefaultText());
+          }
+          rc.setText(rc.getDefaultText());
+        }
+      }
+
+      static String encodeIdentifier(byte[] identifier) {
+        return new String(Base64.encodeBase64(identifier));
+      }
+
+      static char[] encodePassword(byte[] password) {
+        return new String(Base64.encodeBase64(password)).toCharArray();
+      }
+    }
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/HadoopThriftAuthBridge23.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/HadoopThriftAuthBridge23.java
new file mode 100644
index 000000000..17ae6bc33
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/HadoopThriftAuthBridge23.java
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.auth;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.Map;
+import org.apache.hadoop.conf.Configurable;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.security.SaslRpcServer;
+
+/**
+ * Functions that bridge Thrift's SASL transports to Hadoop's SASL callback handlers and
+ * authentication classes.
+ *
+ * <p>This is a 0.23/2.x specific implementation
+ */
+public class HadoopThriftAuthBridge23 extends HadoopThriftAuthBridge {
+
+  private static Field SASL_PROPS_FIELD;
+  private static Class<?> SASL_PROPERTIES_RESOLVER_CLASS;
+  private static Method RES_GET_INSTANCE_METHOD;
+  private static Method GET_DEFAULT_PROP_METHOD;
+
+  static {
+    SASL_PROPERTIES_RESOLVER_CLASS = null;
+    SASL_PROPS_FIELD = null;
+    final String SASL_PROP_RES_CLASSNAME = "org.apache.hadoop.security.SaslPropertiesResolver";
+    try {
+      SASL_PROPERTIES_RESOLVER_CLASS = Class.forName(SASL_PROP_RES_CLASSNAME);
+
+    } catch (ClassNotFoundException e) {
+    }
+
+    if (SASL_PROPERTIES_RESOLVER_CLASS != null) {
+      // found the class, so this would be hadoop version 2.4 or newer (See
+      // HADOOP-10221, HADOOP-10451)
+      try {
+        RES_GET_INSTANCE_METHOD =
+            SASL_PROPERTIES_RESOLVER_CLASS.getMethod("getInstance", Configuration.class);
+        GET_DEFAULT_PROP_METHOD = SASL_PROPERTIES_RESOLVER_CLASS.getMethod("getDefaultProperties");
+      } catch (Exception e) {
+        // this must be hadoop 2.4 , where getDefaultProperties was protected
+      }
+    }
+
+    if (SASL_PROPERTIES_RESOLVER_CLASS == null || GET_DEFAULT_PROP_METHOD == null) {
+      // this must be a hadoop 2.4 version or earlier.
+      // Resorting to the earlier method of getting the properties, which uses SASL_PROPS field
+      try {
+        SASL_PROPS_FIELD = SaslRpcServer.class.getField("SASL_PROPS");
+      } catch (NoSuchFieldException e) {
+        // Older version of hadoop should have had this field
+        throw new IllegalStateException(
+            "Error finding hadoop SASL_PROPS field in " + SaslRpcServer.class.getSimpleName(), e);
+      }
+    }
+  }
+
+  // TODO RIVEN switch this back to package level when we can move TestHadoopAuthBridge23 into
+  // riven.
+  // Package permission so that HadoopThriftAuthBridge can construct it but others cannot.
+  protected HadoopThriftAuthBridge23() {}
+
+  /**
+   * Read and return Hadoop SASL configuration which can be configured using "hadoop.rpc.protection"
+   *
+   * @param conf
+   * @return Hadoop SASL configuration
+   */
+  @SuppressWarnings("unchecked")
+  @Override
+  public Map<String, String> getHadoopSaslProperties(Configuration conf) {
+    if (SASL_PROPS_FIELD != null) {
+      // hadoop 2.4 and earlier way of finding the sasl property settings
+      // Initialize the SaslRpcServer to ensure QOP parameters are read from
+      // conf
+      SaslRpcServer.init(conf);
+      try {
+        return (Map<String, String>) SASL_PROPS_FIELD.get(null);
+      } catch (Exception e) {
+        throw new IllegalStateException("Error finding hadoop SASL properties", e);
+      }
+    }
+    // 2.5 and later way of finding sasl property
+    try {
+      Configurable saslPropertiesResolver =
+          (Configurable) RES_GET_INSTANCE_METHOD.invoke(null, conf);
+      saslPropertiesResolver.setConf(conf);
+      return (Map<String, String>) GET_DEFAULT_PROP_METHOD.invoke(saslPropertiesResolver);
+    } catch (Exception e) {
+      throw new IllegalStateException("Error finding hadoop SASL properties", e);
+    }
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/HttpAuthUtils.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/HttpAuthUtils.java
new file mode 100644
index 000000000..e41dd9ed4
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/HttpAuthUtils.java
@@ -0,0 +1,108 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.auth;
+
+import java.security.PrivilegedExceptionAction;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+import javax.security.auth.Subject;
+import org.apache.commons.codec.binary.Base64;
+import org.apache.hadoop.security.UserGroupInformation;
+import org.apache.http.protocol.BasicHttpContext;
+import org.apache.http.protocol.HttpContext;
+import org.ietf.jgss.GSSContext;
+import org.ietf.jgss.GSSManager;
+import org.ietf.jgss.GSSName;
+import org.ietf.jgss.Oid;
+
+/** Utility functions for HTTP mode authentication. */
+public final class HttpAuthUtils {
+  public static final String AUTHORIZATION = "Authorization";
+  public static final String NEGOTIATE = "Negotiate";
+  private static final String COOKIE_CLIENT_USER_NAME = "cu";
+  private static final String COOKIE_CLIENT_RAND_NUMBER = "rn";
+  private static final String COOKIE_KEY_VALUE_SEPARATOR = "=";
+  private static final Set<String> COOKIE_ATTRIBUTES =
+      new HashSet<String>(Arrays.asList(COOKIE_CLIENT_USER_NAME, COOKIE_CLIENT_RAND_NUMBER));
+
+  /**
+   * @return Stringified Base64 encoded kerberosAuthHeader on success
+   * @throws Exception
+   */
+  public static String getKerberosServiceTicket(
+      String principal, String host, String serverHttpUrl, Subject loggedInSubject)
+      throws Exception {
+    String serverPrincipal = HadoopThriftAuthBridge.getBridge().getServerPrincipal(principal, host);
+    if (loggedInSubject != null) {
+      return Subject.doAs(
+          loggedInSubject, new HttpKerberosClientAction(serverPrincipal, serverHttpUrl));
+    } else {
+      // JAAS login from ticket cache to setup the client UserGroupInformation
+      UserGroupInformation clientUGI =
+          HadoopThriftAuthBridge.getBridge().getCurrentUGIWithConf("kerberos");
+      return clientUGI.doAs(new HttpKerberosClientAction(serverPrincipal, serverHttpUrl));
+    }
+  }
+
+  private HttpAuthUtils() {
+    throw new UnsupportedOperationException("Can't initialize class");
+  }
+
+  /**
+   * We'll create an instance of this class within a doAs block so that the client's TGT credentials
+   * can be read from the Subject
+   */
+  public static class HttpKerberosClientAction implements PrivilegedExceptionAction<String> {
+    public static final String SERVER_HTTP_URL = "SERVER_HTTP_URL";
+    private final String serverPrincipal;
+    private final String serverHttpUrl;
+    private final Base64 base64codec;
+    private final HttpContext httpContext;
+
+    public HttpKerberosClientAction(String serverPrincipal, String serverHttpUrl) {
+      this.serverPrincipal = serverPrincipal;
+      this.serverHttpUrl = serverHttpUrl;
+      base64codec = new Base64(0);
+      httpContext = new BasicHttpContext();
+      httpContext.setAttribute(SERVER_HTTP_URL, serverHttpUrl);
+    }
+
+    @Override
+    public String run() throws Exception {
+      // This Oid for Kerberos GSS-API mechanism.
+      Oid mechOid = new Oid("1.2.840.113554.1.2.2");
+      // Oid for kerberos principal name
+      Oid krb5PrincipalOid = new Oid("1.2.840.113554.1.2.2.1");
+      GSSManager manager = GSSManager.getInstance();
+      // GSS name for server
+      GSSName serverName = manager.createName(serverPrincipal, krb5PrincipalOid);
+      // Create a GSSContext for authentication with the service.
+      // We're passing client credentials as null since we want them to be read from the Subject.
+      GSSContext gssContext =
+          manager.createContext(serverName, mechOid, null, GSSContext.DEFAULT_LIFETIME);
+      gssContext.requestMutualAuth(false);
+      // Establish context
+      byte[] inToken = new byte[0];
+      byte[] outToken = gssContext.initSecContext(inToken, 0, inToken.length);
+      gssContext.dispose();
+      // Base64 encoded and stringified token for server
+      return new String(base64codec.encode(outToken));
+    }
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/KerberosSaslHelper.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/KerberosSaslHelper.java
new file mode 100644
index 000000000..c40aa898d
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/KerberosSaslHelper.java
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.auth;
+
+import java.io.IOException;
+import java.util.Map;
+import javax.security.sasl.SaslException;
+import org.apache.thrift.transport.TSaslClientTransport;
+import org.apache.thrift.transport.TTransport;
+
+public final class KerberosSaslHelper {
+
+  public static TTransport getKerberosTransport(
+      String principal,
+      String host,
+      TTransport underlyingTransport,
+      Map<String, String> saslProps,
+      boolean assumeSubject)
+      throws SaslException {
+    try {
+      String[] names = principal.split("[/@]");
+      if (names.length != 3) {
+        throw new IllegalArgumentException("Kerberos principal should have 3 parts: " + principal);
+      }
+
+      if (assumeSubject) {
+        return createSubjectAssumedTransport(principal, underlyingTransport, saslProps);
+      } else {
+        HadoopThriftAuthBridge.Client authBridge =
+            HadoopThriftAuthBridge.getBridge().createClientWithConf("kerberos");
+        return authBridge.createClientTransport(
+            principal, host, "KERBEROS", null, underlyingTransport, saslProps);
+      }
+    } catch (IOException e) {
+      throw new SaslException("Failed to open client transport", e);
+    }
+  }
+
+  public static TTransport createSubjectAssumedTransport(
+      String principal, TTransport underlyingTransport, Map<String, String> saslProps)
+      throws IOException {
+    String[] names = principal.split("[/@]");
+    try {
+      TTransport saslTransport =
+          new TSaslClientTransport(
+              "GSSAPI", null, names[0], names[1], saslProps, null, underlyingTransport);
+      return new TSubjectAssumingTransport(saslTransport);
+    } catch (SaslException se) {
+      throw new IOException("Could not instantiate SASL transport", se);
+    }
+  }
+
+  public static TTransport getTokenTransport(
+      String tokenStr, String host, TTransport underlyingTransport, Map<String, String> saslProps)
+      throws SaslException {
+    HadoopThriftAuthBridge.Client authBridge =
+        HadoopThriftAuthBridge.getBridge().createClientWithConf("kerberos");
+
+    try {
+      return authBridge.createClientTransport(
+          null, host, "DIGEST", tokenStr, underlyingTransport, saslProps);
+    } catch (IOException e) {
+      throw new SaslException("Failed to open client transport", e);
+    }
+  }
+
+  private KerberosSaslHelper() {
+    throw new UnsupportedOperationException("Can't initialize class");
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/PlainSaslHelper.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/PlainSaslHelper.java
new file mode 100644
index 000000000..6da7d146e
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/PlainSaslHelper.java
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.auth;
+
+import java.io.IOException;
+import java.security.Security;
+import java.util.HashMap;
+import javax.security.auth.callback.*;
+import javax.security.sasl.SaslException;
+import org.apache.kyuubi.jdbc.hive.auth.PlainSaslServer.SaslPlainProvider;
+import org.apache.thrift.transport.TSaslClientTransport;
+import org.apache.thrift.transport.TTransport;
+
+public final class PlainSaslHelper {
+
+  // Register Plain SASL server provider
+  static {
+    Security.addProvider(new SaslPlainProvider());
+  }
+
+  public static TTransport getPlainTransport(
+      String username, String password, TTransport underlyingTransport) throws SaslException {
+    return new TSaslClientTransport(
+        "PLAIN",
+        null,
+        null,
+        null,
+        new HashMap<String, String>(),
+        new PlainCallbackHandler(username, password),
+        underlyingTransport);
+  }
+
+  private PlainSaslHelper() {
+    throw new UnsupportedOperationException("Can't initialize class");
+  }
+
+  public static class PlainCallbackHandler implements CallbackHandler {
+
+    private final String username;
+    private final String password;
+
+    public PlainCallbackHandler(String username, String password) {
+      this.username = username;
+      this.password = password;
+    }
+
+    @Override
+    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
+      for (Callback callback : callbacks) {
+        if (callback instanceof NameCallback) {
+          NameCallback nameCallback = (NameCallback) callback;
+          nameCallback.setName(username);
+        } else if (callback instanceof PasswordCallback) {
+          PasswordCallback passCallback = (PasswordCallback) callback;
+          passCallback.setPassword(password.toCharArray());
+        } else {
+          throw new UnsupportedCallbackException(callback);
+        }
+      }
+    }
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/PlainSaslServer.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/PlainSaslServer.java
new file mode 100644
index 000000000..91784b1ec
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/PlainSaslServer.java
@@ -0,0 +1,175 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.auth;
+
+import java.io.IOException;
+import java.security.Provider;
+import java.util.ArrayDeque;
+import java.util.Deque;
+import java.util.Map;
+import javax.security.auth.callback.*;
+import javax.security.sasl.AuthorizeCallback;
+import javax.security.sasl.SaslException;
+import javax.security.sasl.SaslServer;
+import javax.security.sasl.SaslServerFactory;
+
+/**
+ * Sun JDK only provides a PLAIN client and no server. This class implements the Plain SASL server
+ * conforming to RFC #4616 (http://www.ietf.org/rfc/rfc4616.txt).
+ */
+public class PlainSaslServer implements SaslServer {
+
+  public static final String PLAIN_METHOD = "PLAIN";
+  private String user;
+  private final CallbackHandler handler;
+
+  PlainSaslServer(CallbackHandler handler, String authMethodStr) throws SaslException {
+    this.handler = handler;
+    AuthenticationProviderFactory.AuthMethods.getValidAuthMethod(authMethodStr);
+  }
+
+  @Override
+  public String getMechanismName() {
+    return PLAIN_METHOD;
+  }
+
+  @Override
+  public byte[] evaluateResponse(byte[] response) throws SaslException {
+    try {
+      // parse the response
+      // message   = [authzid] UTF8NUL authcid UTF8NUL passwd'
+
+      Deque<String> tokenList = new ArrayDeque<String>();
+      StringBuilder messageToken = new StringBuilder();
+      for (byte b : response) {
+        if (b == 0) {
+          tokenList.addLast(messageToken.toString());
+          messageToken = new StringBuilder();
+        } else {
+          messageToken.append((char) b);
+        }
+      }
+      tokenList.addLast(messageToken.toString());
+
+      // validate response
+      if (tokenList.size() < 2 || tokenList.size() > 3) {
+        throw new SaslException("Invalid message format");
+      }
+      String passwd = tokenList.removeLast();
+      user = tokenList.removeLast();
+      // optional authzid
+      String authzId;
+      if (tokenList.isEmpty()) {
+        authzId = user;
+      } else {
+        authzId = tokenList.removeLast();
+      }
+      if (user == null || user.isEmpty()) {
+        throw new SaslException("No user name provided");
+      }
+      if (passwd == null || passwd.isEmpty()) {
+        throw new SaslException("No password name provided");
+      }
+
+      NameCallback nameCallback = new NameCallback("User");
+      nameCallback.setName(user);
+      PasswordCallback pcCallback = new PasswordCallback("Password", false);
+      pcCallback.setPassword(passwd.toCharArray());
+      AuthorizeCallback acCallback = new AuthorizeCallback(user, authzId);
+
+      Callback[] cbList = {nameCallback, pcCallback, acCallback};
+      handler.handle(cbList);
+      if (!acCallback.isAuthorized()) {
+        throw new SaslException("Authentication failed");
+      }
+    } catch (IllegalStateException eL) {
+      throw new SaslException("Invalid message format", eL);
+    } catch (IOException eI) {
+      throw new SaslException("Error validating the login", eI);
+    } catch (UnsupportedCallbackException eU) {
+      throw new SaslException("Error validating the login", eU);
+    }
+    return null;
+  }
+
+  @Override
+  public boolean isComplete() {
+    return user != null;
+  }
+
+  @Override
+  public String getAuthorizationID() {
+    return user;
+  }
+
+  @Override
+  public byte[] unwrap(byte[] incoming, int offset, int len) {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public byte[] wrap(byte[] outgoing, int offset, int len) {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public Object getNegotiatedProperty(String propName) {
+    return null;
+  }
+
+  @Override
+  public void dispose() {}
+
+  public static class SaslPlainServerFactory implements SaslServerFactory {
+
+    @Override
+    public SaslServer createSaslServer(
+        String mechanism,
+        String protocol,
+        String serverName,
+        Map<String, ?> props,
+        CallbackHandler cbh) {
+      if (PLAIN_METHOD.equals(mechanism)) {
+        try {
+          return new PlainSaslServer(cbh, protocol);
+        } catch (SaslException e) {
+          /* This is to fulfill the contract of the interface which states that an exception shall
+          be thrown when a SaslServer cannot be created due to an error but null should be
+          returned when a Server can't be created due to the parameters supplied. And the only
+          thing PlainSaslServer can fail on is a non-supported authentication mechanism.
+          That's why we return null instead of throwing the Exception */
+          return null;
+        }
+      }
+      return null;
+    }
+
+    @Override
+    public String[] getMechanismNames(Map<String, ?> props) {
+      return new String[] {PLAIN_METHOD};
+    }
+  }
+
+  public static class SaslPlainProvider extends Provider {
+
+    public SaslPlainProvider() {
+      super("HiveSaslPlain", 1.0, "Hive Plain SASL provider");
+      put("SaslServerFactory.PLAIN", SaslPlainServerFactory.class.getName());
+    }
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/SaslQOP.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/SaslQOP.java
new file mode 100644
index 000000000..ed842e50b
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/SaslQOP.java
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.auth;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/** Possible values of SASL quality-of-protection value. */
+public enum SaslQOP {
+  AUTH("auth"), // Authentication only.
+  AUTH_INT("auth-int"), // Authentication and integrity checking by using signatures.
+  AUTH_CONF("auth-conf"); // Authentication, integrity and confidentiality checking
+  // by using signatures and encryption.
+
+  public final String saslQop;
+
+  private static final Map<String, SaslQOP> STR_TO_ENUM = new HashMap<String, SaslQOP>();
+
+  static {
+    for (SaslQOP saslQop : values()) {
+      STR_TO_ENUM.put(saslQop.toString(), saslQop);
+    }
+  }
+
+  SaslQOP(String saslQop) {
+    this.saslQop = saslQop;
+  }
+
+  public String toString() {
+    return saslQop;
+  }
+
+  public static SaslQOP fromString(String str) {
+    if (str != null) {
+      str = str.toLowerCase();
+    }
+    SaslQOP saslQOP = STR_TO_ENUM.get(str);
+    if (saslQOP == null) {
+      throw new IllegalArgumentException(
+          "Unknown auth type: " + str + " Allowed values are: " + STR_TO_ENUM.keySet());
+    }
+    return saslQOP;
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/TSubjectAssumingTransport.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/TSubjectAssumingTransport.java
new file mode 100644
index 000000000..96781cb12
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/TSubjectAssumingTransport.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.auth;
+
+import java.security.AccessControlContext;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
+import javax.security.auth.Subject;
+import org.apache.hadoop.hive.thrift.TFilterTransport;
+import org.apache.thrift.transport.TTransport;
+import org.apache.thrift.transport.TTransportException;
+
+/**
+ * This is used on the client side, where the API explicitly opens a transport to the server using
+ * the Subject.doAs().
+ */
+public class TSubjectAssumingTransport extends TFilterTransport {
+
+  public TSubjectAssumingTransport(TTransport wrapped) {
+    super(wrapped);
+  }
+
+  @Override
+  public void open() throws TTransportException {
+    try {
+      AccessControlContext context = AccessController.getContext();
+      Subject subject = Subject.getSubject(context);
+      Subject.doAs(
+          subject,
+          new PrivilegedExceptionAction<Void>() {
+            public Void run() {
+              try {
+                wrapped.open();
+              } catch (TTransportException tte) {
+                // Wrap the transport exception in an RTE, since Subject.doAs() then goes
+                // and unwraps this for us out of the doAs block. We then unwrap one
+                // more time in our catch clause to get back the TTE. (ugh)
+                throw new RuntimeException(tte);
+              }
+              return null;
+            }
+          });
+    } catch (PrivilegedActionException ioe) {
+      throw new RuntimeException("Received an ioe we never threw!", ioe);
+    } catch (RuntimeException rte) {
+      if (rte.getCause() instanceof TTransportException) {
+        throw (TTransportException) rte.getCause();
+      } else {
+        throw rte;
+      }
+    }
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/ColumnBasedSet.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/ColumnBasedSet.java
new file mode 100644
index 000000000..b39fb8e94
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/ColumnBasedSet.java
@@ -0,0 +1,179 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.cli;
+
+import java.io.ByteArrayInputStream;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import org.apache.hadoop.hive.common.type.HiveDecimal;
+import org.apache.hive.service.rpc.thrift.TColumn;
+import org.apache.hive.service.rpc.thrift.TRow;
+import org.apache.hive.service.rpc.thrift.TRowSet;
+import org.apache.hive.service.rpc.thrift.TTypeId;
+import org.apache.thrift.TException;
+import org.apache.thrift.protocol.TCompactProtocol;
+import org.apache.thrift.protocol.TProtocol;
+import org.apache.thrift.transport.TIOStreamTransport;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** ColumnBasedSet. */
+public class ColumnBasedSet implements RowSet {
+
+  private long startOffset;
+
+  private final TypeDescriptor[] descriptors; // non-null only for writing (server-side)
+  private final List<ColumnBuffer> columns;
+  private byte[] blob;
+  private boolean isBlobBased = false;
+  public static final Logger LOG = LoggerFactory.getLogger(ColumnBasedSet.class);
+
+  public ColumnBasedSet(TRowSet tRowSet) throws TException {
+    descriptors = null;
+    columns = new ArrayList<ColumnBuffer>();
+    // Use TCompactProtocol to read serialized TColumns
+    if (tRowSet.isSetBinaryColumns()) {
+      TProtocol protocol =
+          new TCompactProtocol(
+              new TIOStreamTransport(new ByteArrayInputStream(tRowSet.getBinaryColumns())));
+      // Read from the stream using the protocol for each column in final schema
+      for (int i = 0; i < tRowSet.getColumnCount(); i++) {
+        TColumn tvalue = new TColumn();
+        try {
+          tvalue.read(protocol);
+        } catch (TException e) {
+          LOG.error(e.getMessage(), e);
+          throw new TException("Error reading column value from the row set blob", e);
+        }
+        columns.add(new ColumnBuffer(tvalue));
+      }
+    } else {
+      if (tRowSet.getColumns() != null) {
+        for (TColumn tvalue : tRowSet.getColumns()) {
+          columns.add(new ColumnBuffer(tvalue));
+        }
+      }
+    }
+    startOffset = tRowSet.getStartRowOffset();
+  }
+
+  private ColumnBasedSet(
+      TypeDescriptor[] descriptors, List<ColumnBuffer> columns, long startOffset) {
+    this.descriptors = descriptors;
+    this.columns = columns;
+    this.startOffset = startOffset;
+  }
+
+  @Override
+  public ColumnBasedSet addRow(Object[] fields) {
+    if (isBlobBased) {
+      this.blob = (byte[]) fields[0];
+    } else {
+      for (int i = 0; i < fields.length; i++) {
+        TypeDescriptor descriptor = descriptors[i];
+        Object field = fields[i];
+        if (field != null && descriptor.getType() == TTypeId.DECIMAL_TYPE) {
+          int scale = descriptor.getDecimalDigits();
+          field = ((HiveDecimal) field).toFormatString(scale);
+        }
+        columns.get(i).addValue(descriptor.getType(), field);
+      }
+    }
+    return this;
+  }
+
+  public List<ColumnBuffer> getColumns() {
+    return columns;
+  }
+
+  @Override
+  public int numColumns() {
+    return columns.size();
+  }
+
+  @Override
+  public int numRows() {
+    return columns.isEmpty() ? 0 : columns.get(0).size();
+  }
+
+  @Override
+  public ColumnBasedSet extractSubset(int maxRows) {
+    int numRows = Math.min(numRows(), maxRows);
+
+    List<ColumnBuffer> subset = new ArrayList<ColumnBuffer>();
+    for (int i = 0; i < columns.size(); i++) {
+      subset.add(columns.get(i).extractSubset(numRows));
+    }
+    ColumnBasedSet result = new ColumnBasedSet(descriptors, subset, startOffset);
+    startOffset += numRows;
+    return result;
+  }
+
+  @Override
+  public long getStartOffset() {
+    return startOffset;
+  }
+
+  @Override
+  public void setStartOffset(long startOffset) {
+    this.startOffset = startOffset;
+  }
+
+  public TRowSet toTRowSet() {
+    TRowSet tRowSet = new TRowSet(startOffset, new ArrayList<TRow>());
+    if (isBlobBased) {
+      tRowSet.setColumns(null);
+      tRowSet.setBinaryColumns(blob);
+      tRowSet.setColumnCount(numColumns());
+    } else {
+      for (int i = 0; i < columns.size(); i++) {
+        tRowSet.addToColumns(columns.get(i).toTColumn());
+      }
+    }
+    return tRowSet;
+  }
+
+  @Override
+  public Iterator<Object[]> iterator() {
+    return new Iterator<Object[]>() {
+
+      private int index;
+      private final Object[] convey = new Object[numColumns()];
+
+      @Override
+      public boolean hasNext() {
+        return index < numRows();
+      }
+
+      @Override
+      public Object[] next() {
+        for (int i = 0; i < columns.size(); i++) {
+          convey[i] = columns.get(i).get(index);
+        }
+        index++;
+        return convey;
+      }
+
+      @Override
+      public void remove() {
+        throw new UnsupportedOperationException("remove");
+      }
+    };
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/ColumnBuffer.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/ColumnBuffer.java
new file mode 100644
index 000000000..287605653
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/ColumnBuffer.java
@@ -0,0 +1,428 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.cli;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.primitives.*;
+import java.nio.ByteBuffer;
+import java.util.*;
+import org.apache.hive.service.rpc.thrift.*;
+
+/** ColumnBuffer */
+public class ColumnBuffer extends AbstractList {
+
+  private static final int DEFAULT_SIZE = 100;
+
+  private final TTypeId type;
+
+  private BitSet nulls;
+
+  private int size;
+  private boolean[] boolVars;
+  private byte[] byteVars;
+  private short[] shortVars;
+  private int[] intVars;
+  private long[] longVars;
+  private double[] doubleVars;
+  private List<String> stringVars;
+  private List<ByteBuffer> binaryVars;
+
+  public ColumnBuffer(TTypeId type, BitSet nulls, Object values) {
+    this.type = type;
+    this.nulls = nulls;
+    if (type == TTypeId.BOOLEAN_TYPE) {
+      boolVars = (boolean[]) values;
+      size = boolVars.length;
+    } else if (type == TTypeId.TINYINT_TYPE) {
+      byteVars = (byte[]) values;
+      size = byteVars.length;
+    } else if (type == TTypeId.SMALLINT_TYPE) {
+      shortVars = (short[]) values;
+      size = shortVars.length;
+    } else if (type == TTypeId.INT_TYPE) {
+      intVars = (int[]) values;
+      size = intVars.length;
+    } else if (type == TTypeId.BIGINT_TYPE) {
+      longVars = (long[]) values;
+      size = longVars.length;
+    } else if (type == TTypeId.DOUBLE_TYPE || type == TTypeId.FLOAT_TYPE) {
+      doubleVars = (double[]) values;
+      size = doubleVars.length;
+    } else if (type == TTypeId.BINARY_TYPE) {
+      binaryVars = (List<ByteBuffer>) values;
+      size = binaryVars.size();
+    } else if (type == TTypeId.STRING_TYPE) {
+      stringVars = (List<String>) values;
+      size = stringVars.size();
+    } else {
+      throw new IllegalStateException("invalid union object");
+    }
+  }
+
+  public ColumnBuffer(TTypeId type) {
+    nulls = new BitSet();
+    switch (type) {
+      case BOOLEAN_TYPE:
+        boolVars = new boolean[DEFAULT_SIZE];
+        break;
+      case TINYINT_TYPE:
+        byteVars = new byte[DEFAULT_SIZE];
+        break;
+      case SMALLINT_TYPE:
+        shortVars = new short[DEFAULT_SIZE];
+        break;
+      case INT_TYPE:
+        intVars = new int[DEFAULT_SIZE];
+        break;
+      case BIGINT_TYPE:
+        longVars = new long[DEFAULT_SIZE];
+        break;
+      case FLOAT_TYPE:
+        type = TTypeId.FLOAT_TYPE;
+        doubleVars = new double[DEFAULT_SIZE];
+        break;
+      case DOUBLE_TYPE:
+        type = TTypeId.DOUBLE_TYPE;
+        doubleVars = new double[DEFAULT_SIZE];
+        break;
+      case BINARY_TYPE:
+        binaryVars = new ArrayList<ByteBuffer>();
+        break;
+      default:
+        type = TTypeId.STRING_TYPE;
+        stringVars = new ArrayList<String>();
+    }
+    this.type = type;
+  }
+
+  public ColumnBuffer(TColumn colValues) {
+    if (colValues.isSetBoolVal()) {
+      type = TTypeId.BOOLEAN_TYPE;
+      nulls = toBitset(colValues.getBoolVal().getNulls());
+      boolVars = Booleans.toArray(colValues.getBoolVal().getValues());
+      size = boolVars.length;
+    } else if (colValues.isSetByteVal()) {
+      type = TTypeId.TINYINT_TYPE;
+      nulls = toBitset(colValues.getByteVal().getNulls());
+      byteVars = Bytes.toArray(colValues.getByteVal().getValues());
+      size = byteVars.length;
+    } else if (colValues.isSetI16Val()) {
+      type = TTypeId.SMALLINT_TYPE;
+      nulls = toBitset(colValues.getI16Val().getNulls());
+      shortVars = Shorts.toArray(colValues.getI16Val().getValues());
+      size = shortVars.length;
+    } else if (colValues.isSetI32Val()) {
+      type = TTypeId.INT_TYPE;
+      nulls = toBitset(colValues.getI32Val().getNulls());
+      intVars = Ints.toArray(colValues.getI32Val().getValues());
+      size = intVars.length;
+    } else if (colValues.isSetI64Val()) {
+      type = TTypeId.BIGINT_TYPE;
+      nulls = toBitset(colValues.getI64Val().getNulls());
+      longVars = Longs.toArray(colValues.getI64Val().getValues());
+      size = longVars.length;
+    } else if (colValues.isSetDoubleVal()) {
+      type = TTypeId.DOUBLE_TYPE;
+      nulls = toBitset(colValues.getDoubleVal().getNulls());
+      doubleVars = Doubles.toArray(colValues.getDoubleVal().getValues());
+      size = doubleVars.length;
+    } else if (colValues.isSetBinaryVal()) {
+      type = TTypeId.BINARY_TYPE;
+      nulls = toBitset(colValues.getBinaryVal().getNulls());
+      binaryVars = colValues.getBinaryVal().getValues();
+      size = binaryVars.size();
+    } else if (colValues.isSetStringVal()) {
+      type = TTypeId.STRING_TYPE;
+      nulls = toBitset(colValues.getStringVal().getNulls());
+      stringVars = colValues.getStringVal().getValues();
+      size = stringVars.size();
+    } else {
+      throw new IllegalStateException("invalid union object");
+    }
+  }
+
+  /**
+   * Get a subset of this ColumnBuffer, starting from the 1st value.
+   *
+   * @param end index after the last value to include
+   */
+  public ColumnBuffer extractSubset(int end) {
+    BitSet subNulls = nulls.get(0, end);
+    if (type == TTypeId.BOOLEAN_TYPE) {
+      ColumnBuffer subset = new ColumnBuffer(type, subNulls, Arrays.copyOfRange(boolVars, 0, end));
+      boolVars = Arrays.copyOfRange(boolVars, end, size);
+      nulls = nulls.get(end, size);
+      size = boolVars.length;
+      return subset;
+    }
+    if (type == TTypeId.TINYINT_TYPE) {
+      ColumnBuffer subset = new ColumnBuffer(type, subNulls, Arrays.copyOfRange(byteVars, 0, end));
+      byteVars = Arrays.copyOfRange(byteVars, end, size);
+      nulls = nulls.get(end, size);
+      size = byteVars.length;
+      return subset;
+    }
+    if (type == TTypeId.SMALLINT_TYPE) {
+      ColumnBuffer subset = new ColumnBuffer(type, subNulls, Arrays.copyOfRange(shortVars, 0, end));
+      shortVars = Arrays.copyOfRange(shortVars, end, size);
+      nulls = nulls.get(end, size);
+      size = shortVars.length;
+      return subset;
+    }
+    if (type == TTypeId.INT_TYPE) {
+      ColumnBuffer subset = new ColumnBuffer(type, subNulls, Arrays.copyOfRange(intVars, 0, end));
+      intVars = Arrays.copyOfRange(intVars, end, size);
+      nulls = nulls.get(end, size);
+      size = intVars.length;
+      return subset;
+    }
+    if (type == TTypeId.BIGINT_TYPE) {
+      ColumnBuffer subset = new ColumnBuffer(type, subNulls, Arrays.copyOfRange(longVars, 0, end));
+      longVars = Arrays.copyOfRange(longVars, end, size);
+      nulls = nulls.get(end, size);
+      size = longVars.length;
+      return subset;
+    }
+    if (type == TTypeId.DOUBLE_TYPE || type == TTypeId.FLOAT_TYPE) {
+      ColumnBuffer subset =
+          new ColumnBuffer(type, subNulls, Arrays.copyOfRange(doubleVars, 0, end));
+      doubleVars = Arrays.copyOfRange(doubleVars, end, size);
+      nulls = nulls.get(end, size);
+      size = doubleVars.length;
+      return subset;
+    }
+    if (type == TTypeId.BINARY_TYPE) {
+      ColumnBuffer subset = new ColumnBuffer(type, subNulls, binaryVars.subList(0, end));
+      binaryVars = binaryVars.subList(end, binaryVars.size());
+      nulls = nulls.get(end, size);
+      size = binaryVars.size();
+      return subset;
+    }
+    if (type == TTypeId.STRING_TYPE) {
+      ColumnBuffer subset = new ColumnBuffer(type, subNulls, stringVars.subList(0, end));
+      stringVars = stringVars.subList(end, stringVars.size());
+      nulls = nulls.get(end, size);
+      size = stringVars.size();
+      return subset;
+    }
+    throw new IllegalStateException("invalid union object");
+  }
+
+  @VisibleForTesting
+  BitSet getNulls() {
+    return nulls;
+  }
+
+  private static final byte[] MASKS =
+      new byte[] {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, (byte) 0x80};
+
+  private static BitSet toBitset(byte[] nulls) {
+    BitSet bitset = new BitSet();
+    int bits = nulls.length * 8;
+    for (int i = 0; i < bits; i++) {
+      bitset.set(i, (nulls[i / 8] & MASKS[i % 8]) != 0);
+    }
+    return bitset;
+  }
+
+  private static byte[] toBinary(BitSet bitset) {
+    byte[] nulls = new byte[1 + (bitset.length() / 8)];
+    for (int i = 0; i < bitset.length(); i++) {
+      nulls[i / 8] |= bitset.get(i) ? MASKS[i % 8] : 0;
+    }
+    return nulls;
+  }
+
+  public TTypeId getType() {
+    return type;
+  }
+
+  @Override
+  public Object get(int index) {
+    if (nulls.get(index)) {
+      return null;
+    }
+    switch (type) {
+      case BOOLEAN_TYPE:
+        return boolVars[index];
+      case TINYINT_TYPE:
+        return byteVars[index];
+      case SMALLINT_TYPE:
+        return shortVars[index];
+      case INT_TYPE:
+        return intVars[index];
+      case BIGINT_TYPE:
+        return longVars[index];
+      case FLOAT_TYPE:
+      case DOUBLE_TYPE:
+        return doubleVars[index];
+      case STRING_TYPE:
+        return stringVars.get(index);
+      case BINARY_TYPE:
+        return binaryVars.get(index).array();
+    }
+    return null;
+  }
+
+  @Override
+  public int size() {
+    return size;
+  }
+
+  public TColumn toTColumn() {
+    TColumn value = new TColumn();
+    ByteBuffer nullMasks = ByteBuffer.wrap(toBinary(nulls));
+    switch (type) {
+      case BOOLEAN_TYPE:
+        value.setBoolVal(
+            new TBoolColumn(Booleans.asList(Arrays.copyOfRange(boolVars, 0, size)), nullMasks));
+        break;
+      case TINYINT_TYPE:
+        value.setByteVal(
+            new TByteColumn(Bytes.asList(Arrays.copyOfRange(byteVars, 0, size)), nullMasks));
+        break;
+      case SMALLINT_TYPE:
+        value.setI16Val(
+            new TI16Column(Shorts.asList(Arrays.copyOfRange(shortVars, 0, size)), nullMasks));
+        break;
+      case INT_TYPE:
+        value.setI32Val(
+            new TI32Column(Ints.asList(Arrays.copyOfRange(intVars, 0, size)), nullMasks));
+        break;
+      case BIGINT_TYPE:
+        value.setI64Val(
+            new TI64Column(Longs.asList(Arrays.copyOfRange(longVars, 0, size)), nullMasks));
+        break;
+      case FLOAT_TYPE:
+      case DOUBLE_TYPE:
+        value.setDoubleVal(
+            new TDoubleColumn(Doubles.asList(Arrays.copyOfRange(doubleVars, 0, size)), nullMasks));
+        break;
+      case STRING_TYPE:
+        value.setStringVal(new TStringColumn(stringVars, nullMasks));
+        break;
+      case BINARY_TYPE:
+        value.setBinaryVal(new TBinaryColumn(binaryVars, nullMasks));
+        break;
+    }
+    return value;
+  }
+
+  private static final ByteBuffer EMPTY_BINARY = ByteBuffer.allocate(0);
+  private static final String EMPTY_STRING = "";
+
+  public void addValue(Object field) {
+    addValue(this.type, field);
+  }
+
+  public void addValue(TTypeId type, Object field) {
+    switch (type) {
+      case BOOLEAN_TYPE:
+        nulls.set(size, field == null);
+        boolVars()[size] = field == null ? true : (Boolean) field;
+        break;
+      case TINYINT_TYPE:
+        nulls.set(size, field == null);
+        byteVars()[size] = field == null ? 0 : (Byte) field;
+        break;
+      case SMALLINT_TYPE:
+        nulls.set(size, field == null);
+        shortVars()[size] = field == null ? 0 : (Short) field;
+        break;
+      case INT_TYPE:
+        nulls.set(size, field == null);
+        intVars()[size] = field == null ? 0 : (Integer) field;
+        break;
+      case BIGINT_TYPE:
+        nulls.set(size, field == null);
+        longVars()[size] = field == null ? 0 : (Long) field;
+        break;
+      case FLOAT_TYPE:
+        nulls.set(size, field == null);
+        doubleVars()[size] = field == null ? 0 : new Double(field.toString());
+        break;
+      case DOUBLE_TYPE:
+        nulls.set(size, field == null);
+        doubleVars()[size] = field == null ? 0 : (Double) field;
+        break;
+      case BINARY_TYPE:
+        nulls.set(binaryVars.size(), field == null);
+        binaryVars.add(field == null ? EMPTY_BINARY : ByteBuffer.wrap((byte[]) field));
+        break;
+      default:
+        nulls.set(stringVars.size(), field == null);
+        stringVars.add(field == null ? EMPTY_STRING : String.valueOf(field));
+        break;
+    }
+    size++;
+  }
+
+  private boolean[] boolVars() {
+    if (boolVars.length == size) {
+      boolean[] newVars = new boolean[size << 1];
+      System.arraycopy(boolVars, 0, newVars, 0, size);
+      return boolVars = newVars;
+    }
+    return boolVars;
+  }
+
+  private byte[] byteVars() {
+    if (byteVars.length == size) {
+      byte[] newVars = new byte[size << 1];
+      System.arraycopy(byteVars, 0, newVars, 0, size);
+      return byteVars = newVars;
+    }
+    return byteVars;
+  }
+
+  private short[] shortVars() {
+    if (shortVars.length == size) {
+      short[] newVars = new short[size << 1];
+      System.arraycopy(shortVars, 0, newVars, 0, size);
+      return shortVars = newVars;
+    }
+    return shortVars;
+  }
+
+  private int[] intVars() {
+    if (intVars.length == size) {
+      int[] newVars = new int[size << 1];
+      System.arraycopy(intVars, 0, newVars, 0, size);
+      return intVars = newVars;
+    }
+    return intVars;
+  }
+
+  private long[] longVars() {
+    if (longVars.length == size) {
+      long[] newVars = new long[size << 1];
+      System.arraycopy(longVars, 0, newVars, 0, size);
+      return longVars = newVars;
+    }
+    return longVars;
+  }
+
+  private double[] doubleVars() {
+    if (doubleVars.length == size) {
+      double[] newVars = new double[size << 1];
+      System.arraycopy(doubleVars, 0, newVars, 0, size);
+      return doubleVars = newVars;
+    }
+    return doubleVars;
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/ColumnValue.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/ColumnValue.java
new file mode 100644
index 000000000..1d0b6b971
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/ColumnValue.java
@@ -0,0 +1,274 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.cli;
+
+import java.sql.Date;
+import java.sql.Timestamp;
+import org.apache.hadoop.hive.common.type.*;
+import org.apache.hive.service.rpc.thrift.*;
+
+/** Protocols before HIVE_CLI_SERVICE_PROTOCOL_V6 (used by RowBasedSet) */
+public class ColumnValue {
+
+  private static TColumnValue booleanValue(Boolean value) {
+    TBoolValue tBoolValue = new TBoolValue();
+    if (value != null) {
+      tBoolValue.setValue(value);
+    }
+    return TColumnValue.boolVal(tBoolValue);
+  }
+
+  private static TColumnValue byteValue(Byte value) {
+    TByteValue tByteValue = new TByteValue();
+    if (value != null) {
+      tByteValue.setValue(value);
+    }
+    return TColumnValue.byteVal(tByteValue);
+  }
+
+  private static TColumnValue shortValue(Short value) {
+    TI16Value tI16Value = new TI16Value();
+    if (value != null) {
+      tI16Value.setValue(value);
+    }
+    return TColumnValue.i16Val(tI16Value);
+  }
+
+  private static TColumnValue intValue(Integer value) {
+    TI32Value tI32Value = new TI32Value();
+    if (value != null) {
+      tI32Value.setValue(value);
+    }
+    return TColumnValue.i32Val(tI32Value);
+  }
+
+  private static TColumnValue longValue(Long value) {
+    TI64Value tI64Value = new TI64Value();
+    if (value != null) {
+      tI64Value.setValue(value);
+    }
+    return TColumnValue.i64Val(tI64Value);
+  }
+
+  private static TColumnValue floatValue(Float value) {
+    TDoubleValue tDoubleValue = new TDoubleValue();
+    if (value != null) {
+      tDoubleValue.setValue(value);
+    }
+    return TColumnValue.doubleVal(tDoubleValue);
+  }
+
+  private static TColumnValue doubleValue(Double value) {
+    TDoubleValue tDoubleValue = new TDoubleValue();
+    if (value != null) {
+      tDoubleValue.setValue(value);
+    }
+    return TColumnValue.doubleVal(tDoubleValue);
+  }
+
+  private static TColumnValue stringValue(String value) {
+    TStringValue tStringValue = new TStringValue();
+    if (value != null) {
+      tStringValue.setValue(value);
+    }
+    return TColumnValue.stringVal(tStringValue);
+  }
+
+  private static TColumnValue stringValue(HiveChar value) {
+    TStringValue tStringValue = new TStringValue();
+    if (value != null) {
+      tStringValue.setValue(value.toString());
+    }
+    return TColumnValue.stringVal(tStringValue);
+  }
+
+  private static TColumnValue stringValue(HiveVarchar value) {
+    TStringValue tStringValue = new TStringValue();
+    if (value != null) {
+      tStringValue.setValue(value.toString());
+    }
+    return TColumnValue.stringVal(tStringValue);
+  }
+
+  private static TColumnValue dateValue(Date value) {
+    TStringValue tStringValue = new TStringValue();
+    if (value != null) {
+      tStringValue.setValue(value.toString());
+    }
+    return new TColumnValue(TColumnValue.stringVal(tStringValue));
+  }
+
+  private static TColumnValue timestampValue(Timestamp value) {
+    TStringValue tStringValue = new TStringValue();
+    if (value != null) {
+      tStringValue.setValue(value.toString());
+    }
+    return TColumnValue.stringVal(tStringValue);
+  }
+
+  private static TColumnValue timestampTZValue(TimestampTZ value) {
+    TStringValue tStringValue = new TStringValue();
+    if (value != null) {
+      tStringValue.setValue(value.toString());
+    }
+    return TColumnValue.stringVal(tStringValue);
+  }
+
+  private static TColumnValue stringValue(HiveDecimal value, TypeDescriptor typeDescriptor) {
+    TStringValue tStrValue = new TStringValue();
+    if (value != null) {
+      int scale = typeDescriptor.getDecimalDigits();
+      tStrValue.setValue(value.toFormatString(scale));
+    }
+    return TColumnValue.stringVal(tStrValue);
+  }
+
+  private static TColumnValue stringValue(HiveIntervalYearMonth value) {
+    TStringValue tStrValue = new TStringValue();
+    if (value != null) {
+      tStrValue.setValue(value.toString());
+    }
+    return TColumnValue.stringVal(tStrValue);
+  }
+
+  private static TColumnValue stringValue(HiveIntervalDayTime value) {
+    TStringValue tStrValue = new TStringValue();
+    if (value != null) {
+      tStrValue.setValue(value.toString());
+    }
+    return TColumnValue.stringVal(tStrValue);
+  }
+
+  public static TColumnValue toTColumnValue(TypeDescriptor typeDescriptor, Object value) {
+    TTypeId type = typeDescriptor.getType();
+
+    switch (type) {
+      case BOOLEAN_TYPE:
+        return booleanValue((Boolean) value);
+      case TINYINT_TYPE:
+        return byteValue((Byte) value);
+      case SMALLINT_TYPE:
+        return shortValue((Short) value);
+      case INT_TYPE:
+        return intValue((Integer) value);
+      case BIGINT_TYPE:
+        return longValue((Long) value);
+      case FLOAT_TYPE:
+        return floatValue((Float) value);
+      case DOUBLE_TYPE:
+        return doubleValue((Double) value);
+      case CHAR_TYPE:
+        return stringValue((HiveChar) value);
+      case VARCHAR_TYPE:
+        return stringValue((HiveVarchar) value);
+      case DATE_TYPE:
+        return dateValue((Date) value);
+      case TIMESTAMP_TYPE:
+        return timestampValue((Timestamp) value);
+      case TIMESTAMPLOCALTZ_TYPE:
+        return timestampTZValue((TimestampTZ) value);
+      case INTERVAL_YEAR_MONTH_TYPE:
+        return stringValue((HiveIntervalYearMonth) value);
+      case INTERVAL_DAY_TIME_TYPE:
+        return stringValue((HiveIntervalDayTime) value);
+      case DECIMAL_TYPE:
+        return stringValue((HiveDecimal) value, typeDescriptor);
+      case BINARY_TYPE:
+      case ARRAY_TYPE:
+      case MAP_TYPE:
+      case STRUCT_TYPE:
+      case UNION_TYPE:
+      case USER_DEFINED_TYPE:
+      case NULL_TYPE:
+      case STRING_TYPE:
+        return stringValue((String) value);
+      default:
+        return null;
+    }
+  }
+
+  private static Boolean getBooleanValue(TBoolValue tBoolValue) {
+    if (tBoolValue.isSetValue()) {
+      return tBoolValue.isValue();
+    }
+    return null;
+  }
+
+  private static Byte getByteValue(TByteValue tByteValue) {
+    if (tByteValue.isSetValue()) {
+      return tByteValue.getValue();
+    }
+    return null;
+  }
+
+  private static Short getShortValue(TI16Value tI16Value) {
+    if (tI16Value.isSetValue()) {
+      return tI16Value.getValue();
+    }
+    return null;
+  }
+
+  private static Integer getIntegerValue(TI32Value tI32Value) {
+    if (tI32Value.isSetValue()) {
+      return tI32Value.getValue();
+    }
+    return null;
+  }
+
+  private static Long getLongValue(TI64Value tI64Value) {
+    if (tI64Value.isSetValue()) {
+      return tI64Value.getValue();
+    }
+    return null;
+  }
+
+  private static Double getDoubleValue(TDoubleValue tDoubleValue) {
+    if (tDoubleValue.isSetValue()) {
+      return tDoubleValue.getValue();
+    }
+    return null;
+  }
+
+  private static String getStringValue(TStringValue tStringValue) {
+    if (tStringValue.isSetValue()) {
+      return tStringValue.getValue();
+    }
+    return null;
+  }
+
+  public static Object toColumnValue(TColumnValue value) {
+    TColumnValue._Fields field = value.getSetField();
+    switch (field) {
+      case BOOL_VAL:
+        return getBooleanValue(value.getBoolVal());
+      case BYTE_VAL:
+        return getByteValue(value.getByteVal());
+      case I16_VAL:
+        return getShortValue(value.getI16Val());
+      case I32_VAL:
+        return getIntegerValue(value.getI32Val());
+      case I64_VAL:
+        return getLongValue(value.getI64Val());
+      case DOUBLE_VAL:
+        return getDoubleValue(value.getDoubleVal());
+      case STRING_VAL:
+        return getStringValue(value.getStringVal());
+    }
+    throw new IllegalArgumentException("never");
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/FetchType.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/FetchType.java
new file mode 100644
index 000000000..2e81ec580
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/FetchType.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.cli;
+
+/**
+ * FetchType indicates the type of fetchResults request. It maps the TFetchType, which is generated
+ * from Thrift interface.
+ */
+public enum FetchType {
+  QUERY_OUTPUT((short) 0),
+  LOG((short) 1);
+
+  private final short tFetchType;
+
+  FetchType(short tFetchType) {
+    this.tFetchType = tFetchType;
+  }
+
+  public static FetchType getFetchType(short tFetchType) {
+    for (FetchType fetchType : values()) {
+      if (tFetchType == fetchType.toTFetchType()) {
+        return fetchType;
+      }
+    }
+    return QUERY_OUTPUT;
+  }
+
+  public short toTFetchType() {
+    return tFetchType;
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/HiveSQLException.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/HiveSQLException.java
new file mode 100644
index 000000000..57bf10eef
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/HiveSQLException.java
@@ -0,0 +1,150 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.cli;
+
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.hive.service.rpc.thrift.TStatus;
+
+/** HiveSQLException. */
+public class HiveSQLException extends SQLException {
+
+  /** */
+  private static final long serialVersionUID = -6095254671958748094L;
+
+  public HiveSQLException(TStatus status) {
+    // TODO: set correct vendorCode field
+    super(status.getErrorMessage(), status.getSqlState(), status.getErrorCode());
+    if (status.getInfoMessages() != null) {
+      initCause(toCause(status.getInfoMessages()));
+    }
+  }
+
+  /**
+   * Converts a {@link Throwable} object into a flattened list of texts including its stack trace
+   * and the stack traces of the nested causes.
+   *
+   * @param ex a {@link Throwable} object
+   * @return a flattened list of texts including the {@link Throwable} object's stack trace and the
+   *     stack traces of the nested causes.
+   */
+  public static List<String> toString(Throwable ex) {
+    return toString(ex, null);
+  }
+
+  private static List<String> toString(Throwable cause, StackTraceElement[] parent) {
+    StackTraceElement[] trace = cause.getStackTrace();
+    int m = trace.length - 1;
+    if (parent != null) {
+      int n = parent.length - 1;
+      while (m >= 0 && n >= 0 && trace[m].equals(parent[n])) {
+        m--;
+        n--;
+      }
+    }
+    List<String> detail = enroll(cause, trace, m);
+    cause = cause.getCause();
+    if (cause != null) {
+      detail.addAll(toString(cause, trace));
+    }
+    return detail;
+  }
+
+  private static List<String> enroll(Throwable ex, StackTraceElement[] trace, int max) {
+    List<String> details = new ArrayList<String>();
+    StringBuilder builder = new StringBuilder();
+    builder.append('*').append(ex.getClass().getName()).append(':');
+    builder.append(ex.getMessage()).append(':');
+    builder.append(trace.length).append(':').append(max);
+    details.add(builder.toString());
+    for (int i = 0; i <= max; i++) {
+      builder.setLength(0);
+      builder.append(trace[i].getClassName()).append(':');
+      builder.append(trace[i].getMethodName()).append(':');
+      String fileName = trace[i].getFileName();
+      builder.append(fileName == null ? "" : fileName).append(':');
+      builder.append(trace[i].getLineNumber());
+      details.add(builder.toString());
+    }
+    return details;
+  }
+
+  /**
+   * Converts a flattened list of texts including the stack trace and the stack traces of the nested
+   * causes into a {@link Throwable} object.
+   *
+   * @param details a flattened list of texts including the stack trace and the stack traces of the
+   *     nested causes
+   * @return a {@link Throwable} object
+   */
+  public static Throwable toCause(List<String> details) {
+    return toStackTrace(details, null, 0);
+  }
+
+  private static Throwable toStackTrace(
+      List<String> details, StackTraceElement[] parent, int index) {
+    String detail = details.get(index++);
+    if (!detail.startsWith("*")) {
+      return null; // should not be happened. ignore remaining
+    }
+    int i1 = detail.indexOf(':');
+    int i3 = detail.lastIndexOf(':');
+    int i2 = detail.substring(0, i3).lastIndexOf(':');
+    String exceptionClass = detail.substring(1, i1);
+    String exceptionMessage = detail.substring(i1 + 1, i2);
+    Throwable ex = newInstance(exceptionClass, exceptionMessage);
+
+    int length = Integer.parseInt(detail.substring(i2 + 1, i3));
+    int unique = Integer.parseInt(detail.substring(i3 + 1));
+
+    int i = 0;
+    StackTraceElement[] trace = new StackTraceElement[length];
+    for (; i <= unique; i++) {
+      detail = details.get(index++);
+      int j1 = detail.indexOf(':');
+      int j3 = detail.lastIndexOf(':');
+      int j2 = detail.substring(0, j3).lastIndexOf(':');
+      String className = detail.substring(0, j1);
+      String methodName = detail.substring(j1 + 1, j2);
+      String fileName = detail.substring(j2 + 1, j3);
+      if (fileName.isEmpty()) {
+        fileName = null;
+      }
+      int lineNumber = Integer.parseInt(detail.substring(j3 + 1));
+      trace[i] = new StackTraceElement(className, methodName, fileName, lineNumber);
+    }
+    int common = trace.length - i;
+    if (common > 0) {
+      System.arraycopy(parent, parent.length - common, trace, trace.length - common, common);
+    }
+    if (details.size() > index) {
+      ex.initCause(toStackTrace(details, trace, index));
+    }
+    ex.setStackTrace(trace);
+    return ex;
+  }
+
+  private static Throwable newInstance(String className, String message) {
+    try {
+      return (Throwable) Class.forName(className).getConstructor(String.class).newInstance(message);
+    } catch (Exception e) {
+      return new RuntimeException(className + ":" + message);
+    }
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/RowBasedSet.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/RowBasedSet.java
new file mode 100644
index 000000000..42bc87580
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/RowBasedSet.java
@@ -0,0 +1,137 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.cli;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import org.apache.hive.service.rpc.thrift.TColumnValue;
+import org.apache.hive.service.rpc.thrift.TRow;
+import org.apache.hive.service.rpc.thrift.TRowSet;
+
+/** RowBasedSet */
+public class RowBasedSet implements RowSet {
+
+  private long startOffset;
+
+  private final TypeDescriptor[] descriptors; // non-null only for writing (server-side)
+  private final RemovableList<TRow> rows;
+
+  public RowBasedSet(TRowSet tRowSet) {
+    descriptors = null;
+    rows = new RemovableList<TRow>(tRowSet.getRows());
+    startOffset = tRowSet.getStartRowOffset();
+  }
+
+  private RowBasedSet(TypeDescriptor[] descriptors, List<TRow> rows, long startOffset) {
+    this.descriptors = descriptors;
+    this.rows = new RemovableList<TRow>(rows);
+    this.startOffset = startOffset;
+  }
+
+  @Override
+  public RowBasedSet addRow(Object[] fields) {
+    TRow tRow = new TRow();
+    for (int i = 0; i < fields.length; i++) {
+      tRow.addToColVals(ColumnValue.toTColumnValue(descriptors[i], fields[i]));
+    }
+    rows.add(tRow);
+    return this;
+  }
+
+  @Override
+  public int numColumns() {
+    return rows.isEmpty() ? 0 : rows.get(0).getColVals().size();
+  }
+
+  @Override
+  public int numRows() {
+    return rows.size();
+  }
+
+  public RowBasedSet extractSubset(int maxRows) {
+    int numRows = Math.min(numRows(), maxRows);
+    RowBasedSet result = new RowBasedSet(descriptors, rows.subList(0, numRows), startOffset);
+    rows.removeRange(0, numRows);
+    startOffset += numRows;
+    return result;
+  }
+
+  public long getStartOffset() {
+    return startOffset;
+  }
+
+  public void setStartOffset(long startOffset) {
+    this.startOffset = startOffset;
+  }
+
+  public int getSize() {
+    return rows.size();
+  }
+
+  public TRowSet toTRowSet() {
+    TRowSet tRowSet = new TRowSet();
+    tRowSet.setStartRowOffset(startOffset);
+    tRowSet.setRows(new ArrayList<TRow>(rows));
+    return tRowSet;
+  }
+
+  @Override
+  public Iterator<Object[]> iterator() {
+    return new Iterator<Object[]>() {
+
+      final Iterator<TRow> iterator = rows.iterator();
+      final Object[] convey = new Object[numColumns()];
+
+      @Override
+      public boolean hasNext() {
+        return iterator.hasNext();
+      }
+
+      @Override
+      public Object[] next() {
+        TRow row = iterator.next();
+        List<TColumnValue> values = row.getColVals();
+        for (int i = 0; i < values.size(); i++) {
+          convey[i] = ColumnValue.toColumnValue(values.get(i));
+        }
+        return convey;
+      }
+
+      @Override
+      public void remove() {
+        throw new UnsupportedOperationException("remove");
+      }
+    };
+  }
+
+  private static class RemovableList<E> extends ArrayList<E> {
+    public RemovableList() {
+      super();
+    }
+
+    public RemovableList(List<E> rows) {
+      super(rows);
+    }
+
+    @Override
+    public void removeRange(int fromIndex, int toIndex) {
+      super.removeRange(fromIndex, toIndex);
+    }
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/RowSet.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/RowSet.java
new file mode 100644
index 000000000..bdf823fc1
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/RowSet.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.cli;
+
+import org.apache.hive.service.rpc.thrift.TRowSet;
+
+public interface RowSet extends Iterable<Object[]> {
+
+  RowSet addRow(Object[] fields);
+
+  RowSet extractSubset(int maxRows);
+
+  int numColumns();
+
+  int numRows();
+
+  long getStartOffset();
+
+  void setStartOffset(long startOffset);
+
+  TRowSet toTRowSet();
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/RowSetFactory.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/RowSetFactory.java
new file mode 100644
index 000000000..48b7e4ad8
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/RowSetFactory.java
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.cli;
+
+import static org.apache.hive.service.rpc.thrift.TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V6;
+
+import org.apache.hive.service.rpc.thrift.TProtocolVersion;
+import org.apache.hive.service.rpc.thrift.TRowSet;
+import org.apache.thrift.TException;
+
+public class RowSetFactory {
+
+  // This call is accessed from client (jdbc) side
+  public static RowSet create(TRowSet results, TProtocolVersion version) throws TException {
+    if (version.getValue() >= HIVE_CLI_SERVICE_PROTOCOL_V6.getValue()) {
+      return new ColumnBasedSet(results);
+    }
+    return new RowBasedSet(results);
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/SessionUtils.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/SessionUtils.java
new file mode 100644
index 000000000..64ad4d752
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/SessionUtils.java
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.cli;
+
+import java.io.IOException;
+import org.apache.hadoop.hdfs.security.token.delegation.DelegationTokenSelector;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.mapreduce.security.token.delegation.DelegationTokenIdentifier;
+import org.apache.hadoop.security.UserGroupInformation;
+import org.apache.hadoop.security.token.Token;
+import org.apache.hadoop.security.token.TokenIdentifier;
+import org.apache.hadoop.security.token.TokenSelector;
+
+public class SessionUtils {
+  /**
+   * Get the string form of the token given a token signature. The signature is used as the value of
+   * the "service" field in the token for lookup. Ref: AbstractDelegationTokenSelector in Hadoop. If
+   * there exists such a token in the token cache (credential store) of the job, the lookup returns
+   * that. This is relevant only when running against a "secure" hadoop release The method gets hold
+   * of the tokens if they are set up by hadoop - this should happen on the map/reduce tasks if the
+   * client added the tokens into hadoop's credential store in the front end during job submission.
+   * The method will select the hive delegation token among the set of tokens and return the string
+   * form of it
+   *
+   * @param tokenSignature
+   * @return the string form of the token found
+   * @throws IOException
+   */
+  public static String getTokenStrForm(String tokenSignature) throws IOException {
+    UserGroupInformation ugi = UserGroupInformation.getCurrentUser();
+    TokenSelector<? extends TokenIdentifier> tokenSelector = new DelegationTokenSelector();
+
+    Token<? extends TokenIdentifier> token =
+        tokenSelector.selectToken(
+            tokenSignature == null ? new Text() : new Text(tokenSignature), ugi.getTokens());
+    return token != null ? token.encodeToUrlString() : null;
+  }
+
+  /**
+   * Create a new token using the given string and service
+   *
+   * @param tokenStr
+   * @param tokenService
+   * @return
+   * @throws IOException
+   */
+  private static Token<DelegationTokenIdentifier> createToken(String tokenStr, String tokenService)
+      throws IOException {
+    Token<DelegationTokenIdentifier> delegationToken = new Token<DelegationTokenIdentifier>();
+    delegationToken.decodeFromUrlString(tokenStr);
+    delegationToken.setService(new Text(tokenService));
+    return delegationToken;
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/StringUtils.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/StringUtils.java
new file mode 100644
index 000000000..d4acdd94e
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/StringUtils.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.cli;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+public class StringUtils {
+  public StringUtils() {}
+
+  public static Set<String> asSet(String... elements) {
+    if (elements == null) {
+      return new HashSet();
+    } else {
+      Set<String> set = new HashSet(elements.length);
+      Collections.addAll(set, elements);
+      return set;
+    }
+  }
+
+  public static boolean isEmpty(CharSequence cs) {
+    return cs == null || cs.length() == 0;
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/TypeDescriptor.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/TypeDescriptor.java
new file mode 100644
index 000000000..dff8c4729
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/TypeDescriptor.java
@@ -0,0 +1,172 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.cli;
+
+import java.util.List;
+import org.apache.hadoop.hive.common.type.HiveDecimal;
+import org.apache.hive.service.rpc.thrift.TPrimitiveTypeEntry;
+import org.apache.hive.service.rpc.thrift.TTypeDesc;
+import org.apache.hive.service.rpc.thrift.TTypeEntry;
+import org.apache.hive.service.rpc.thrift.TTypeId;
+
+/** TypeDescriptor. */
+public class TypeDescriptor {
+
+  private final TTypeId type;
+  private TypeQualifiers typeQualifiers = null;
+
+  public TypeDescriptor(TTypeDesc tTypeDesc) {
+    List<TTypeEntry> tTypeEntries = tTypeDesc.getTypes();
+    TPrimitiveTypeEntry top = tTypeEntries.get(0).getPrimitiveEntry();
+    this.type = top.getType();
+    if (top.isSetTypeQualifiers()) {
+      setTypeQualifiers(TypeQualifiers.fromTTypeQualifiers(top.getTypeQualifiers()));
+    }
+  }
+
+  public TTypeId getType() {
+    return type;
+  }
+
+  public TTypeDesc toTTypeDesc() {
+    TPrimitiveTypeEntry primitiveEntry = new TPrimitiveTypeEntry(type);
+    if (getTypeQualifiers() != null) {
+      primitiveEntry.setTypeQualifiers(getTypeQualifiers().toTTypeQualifiers());
+    }
+    TTypeEntry entry = TTypeEntry.primitiveEntry(primitiveEntry);
+
+    TTypeDesc desc = new TTypeDesc();
+    desc.addToTypes(entry);
+    return desc;
+  }
+
+  public TypeQualifiers getTypeQualifiers() {
+    return typeQualifiers;
+  }
+
+  public void setTypeQualifiers(TypeQualifiers typeQualifiers) {
+    this.typeQualifiers = typeQualifiers;
+  }
+
+  /**
+   * The column size for this type. For numeric data this is the maximum precision. For character
+   * data this is the length in characters. For datetime types this is the length in characters of
+   * the String representation (assuming the maximum allowed precision of the fractional seconds
+   * component). For binary data this is the length in bytes. Null is returned for for data types
+   * where the column size is not applicable.
+   */
+  public Integer getColumnSize() {
+    if (isNumericType()) {
+      return getPrecision();
+    }
+    switch (type) {
+      case STRING_TYPE:
+      case BINARY_TYPE:
+        return Integer.MAX_VALUE;
+      case CHAR_TYPE:
+      case VARCHAR_TYPE:
+        return typeQualifiers.getCharacterMaximumLength();
+      case DATE_TYPE:
+        return 10;
+      case TIMESTAMP_TYPE:
+        return 29;
+      case TIMESTAMPLOCALTZ_TYPE:
+        return 31;
+      default:
+        return null;
+    }
+  }
+
+  /**
+   * Maximum precision for numeric types. Returns null for non-numeric types.
+   *
+   * @return
+   */
+  public Integer getPrecision() {
+    if (this.type == TTypeId.DECIMAL_TYPE) {
+      return typeQualifiers.getPrecision();
+    }
+    return getMaxPrecision();
+  }
+
+  /**
+   * The number of fractional digits for this type. Null is returned for data types where this is
+   * not applicable.
+   */
+  public Integer getDecimalDigits() {
+    switch (this.type) {
+      case BOOLEAN_TYPE:
+      case TINYINT_TYPE:
+      case SMALLINT_TYPE:
+      case INT_TYPE:
+      case BIGINT_TYPE:
+        return 0;
+      case FLOAT_TYPE:
+        return 7;
+      case DOUBLE_TYPE:
+        return 15;
+      case DECIMAL_TYPE:
+        return typeQualifiers.getScale();
+      case TIMESTAMP_TYPE:
+        return 9;
+      default:
+        return null;
+    }
+  }
+
+  private boolean isNumericType() {
+    switch (type) {
+      case TINYINT_TYPE:
+      case SMALLINT_TYPE:
+      case INT_TYPE:
+      case BIGINT_TYPE:
+      case FLOAT_TYPE:
+      case DOUBLE_TYPE:
+      case DECIMAL_TYPE:
+        return true;
+      default:
+        return false;
+    }
+  }
+
+  /**
+   * Maximum precision for numeric types. Returns null for non-numeric types.
+   *
+   * @return
+   */
+  private Integer getMaxPrecision() {
+    switch (type) {
+      case TINYINT_TYPE:
+        return 3;
+      case SMALLINT_TYPE:
+        return 5;
+      case INT_TYPE:
+        return 10;
+      case BIGINT_TYPE:
+        return 19;
+      case FLOAT_TYPE:
+        return 7;
+      case DOUBLE_TYPE:
+        return 15;
+      case DECIMAL_TYPE:
+        return HiveDecimal.MAX_PRECISION;
+      default:
+        return null;
+    }
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/TypeQualifiers.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/TypeQualifiers.java
new file mode 100644
index 000000000..56a959869
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/cli/TypeQualifiers.java
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.cli;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.hive.service.rpc.thrift.TCLIServiceConstants;
+import org.apache.hive.service.rpc.thrift.TTypeQualifierValue;
+import org.apache.hive.service.rpc.thrift.TTypeQualifiers;
+
+/**
+ * This class holds type qualifier information for a primitive type, such as char/varchar length or
+ * decimal precision/scale.
+ */
+public class TypeQualifiers {
+  private Integer characterMaximumLength;
+  private Integer precision;
+  private Integer scale;
+
+  public TypeQualifiers() {}
+
+  public Integer getCharacterMaximumLength() {
+    return characterMaximumLength;
+  }
+
+  public void setCharacterMaximumLength(int characterMaximumLength) {
+    this.characterMaximumLength = characterMaximumLength;
+  }
+
+  public TTypeQualifiers toTTypeQualifiers() {
+    TTypeQualifiers ret = null;
+
+    Map<String, TTypeQualifierValue> qMap = new HashMap<String, TTypeQualifierValue>();
+    if (getCharacterMaximumLength() != null) {
+      TTypeQualifierValue val = new TTypeQualifierValue();
+      val.setI32Value(getCharacterMaximumLength().intValue());
+      qMap.put(TCLIServiceConstants.CHARACTER_MAXIMUM_LENGTH, val);
+    }
+
+    if (precision != null) {
+      TTypeQualifierValue val = new TTypeQualifierValue();
+      val.setI32Value(precision.intValue());
+      qMap.put(TCLIServiceConstants.PRECISION, val);
+    }
+
+    if (scale != null) {
+      TTypeQualifierValue val = new TTypeQualifierValue();
+      val.setI32Value(scale.intValue());
+      qMap.put(TCLIServiceConstants.SCALE, val);
+    }
+
+    if (qMap.size() > 0) {
+      ret = new TTypeQualifiers(qMap);
+    }
+
+    return ret;
+  }
+
+  public static TypeQualifiers fromTTypeQualifiers(TTypeQualifiers ttq) {
+    TypeQualifiers ret = null;
+    if (ttq != null) {
+      ret = new TypeQualifiers();
+      Map<String, TTypeQualifierValue> tqMap = ttq.getQualifiers();
+
+      if (tqMap.containsKey(TCLIServiceConstants.CHARACTER_MAXIMUM_LENGTH)) {
+        ret.setCharacterMaximumLength(
+            tqMap.get(TCLIServiceConstants.CHARACTER_MAXIMUM_LENGTH).getI32Value());
+      }
+
+      if (tqMap.containsKey(TCLIServiceConstants.PRECISION)) {
+        ret.setPrecision(tqMap.get(TCLIServiceConstants.PRECISION).getI32Value());
+      }
+
+      if (tqMap.containsKey(TCLIServiceConstants.SCALE)) {
+        ret.setScale(tqMap.get(TCLIServiceConstants.SCALE).getI32Value());
+      }
+    }
+    return ret;
+  }
+
+  public Integer getPrecision() {
+    return precision;
+  }
+
+  public void setPrecision(Integer precision) {
+    this.precision = precision;
+  }
+
+  public Integer getScale() {
+    return scale;
+  }
+
+  public void setScale(Integer scale) {
+    this.scale = scale;
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/AddressTypes.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/AddressTypes.java
new file mode 100644
index 000000000..726a22539
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/AddressTypes.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.server;
+
+/**
+ * Enum of address types -as integers. Why integers and not enums? Cross platform serialization as
+ * JSON
+ */
+public interface AddressTypes {
+
+  /**
+   * hostname/FQDN and port pair: {@value}. The host/domain name and port are set as separate
+   * strings in the address list, e.g.
+   *
+   * <pre>
+   *   ["namenode.example.org", "9870"]
+   * </pre>
+   */
+  String ADDRESS_HOSTNAME_AND_PORT = "host/port";
+
+  String ADDRESS_HOSTNAME_FIELD = "host";
+  String ADDRESS_PORT_FIELD = "port";
+
+  /**
+   * URI entries: {@value}.
+   *
+   * <pre>
+   *   ["http://example.org"]
+   * </pre>
+   */
+  String ADDRESS_URI = "uri";
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/CharTypes.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/CharTypes.java
new file mode 100644
index 000000000..30a1428fc
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/CharTypes.java
@@ -0,0 +1,230 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.server;
+
+import java.util.Arrays;
+
+public final class CharTypes {
+  protected static final char[] HC = "0123456789ABCDEF".toCharArray();
+  protected static final byte[] HB;
+  /**
+   * Value used for lookup tables to indicate that matching characters do not need to be escaped.
+   */
+  public static final int ESCAPE_NONE = 0;
+
+  /**
+   * Value used for lookup tables to indicate that matching characters are to be escaped using
+   * standard escaping; for JSON this means (for example) using "backslash - u" escape method.
+   */
+  public static final int ESCAPE_STANDARD = -1;
+
+  static {
+    int len = HC.length;
+    HB = new byte[len];
+    for (int i = 0; i < len; ++i) {
+      HB[i] = (byte) HC[i];
+    }
+  }
+
+  /**
+   * Lookup table used for determining which input characters need special handling when contained
+   * in text segment.
+   */
+  protected static final int[] sInputCodes;
+
+  static {
+    /* 96 would do for most cases (backslash is ASCII 94)
+     * but if we want to do lookups by raw bytes it's better
+     * to have full table
+     */
+    final int[] table = new int[256];
+    // Control chars and non-space white space are not allowed unquoted
+    for (int i = 0; i < 32; ++i) {
+      table[i] = -1;
+    }
+    // And then string end and quote markers are special too
+    table['"'] = 1;
+    table['\\'] = 1;
+    sInputCodes = table;
+  }
+
+  /** Additionally we can combine UTF-8 decoding info into similar data table. */
+  protected static final int[] sInputCodesUTF8;
+
+  static {
+    final int[] table = new int[sInputCodes.length];
+    System.arraycopy(sInputCodes, 0, table, 0, table.length);
+    for (int c = 128; c < 256; ++c) {
+      int code;
+
+      // We'll add number of bytes needed for decoding
+      if ((c & 0xE0) == 0xC0) { // 2 bytes (0x0080 - 0x07FF)
+        code = 2;
+      } else if ((c & 0xF0) == 0xE0) { // 3 bytes (0x0800 - 0xFFFF)
+        code = 3;
+      } else if ((c & 0xF8) == 0xF0) {
+        // 4 bytes; double-char with surrogates and all...
+        code = 4;
+      } else {
+        // And -1 seems like a good "universal" error marker...
+        code = -1;
+      }
+      table[c] = code;
+    }
+    sInputCodesUTF8 = table;
+  }
+
+  /**
+   * To support non-default (and -standard) unquoted field names mode, need to have alternate
+   * checking. Basically this is list of 8-bit ASCII characters that are legal as part of Javascript
+   * identifier
+   */
+  protected static final int[] sInputCodesJsNames;
+
+  static {
+    final int[] table = new int[256];
+    // Default is "not a name char", mark ones that are
+    Arrays.fill(table, -1);
+    // Assume rules with JS same as Java (change if/as needed)
+    for (int i = 33; i < 256; ++i) {
+      if (Character.isJavaIdentifierPart((char) i)) {
+        table[i] = 0;
+      }
+    }
+    /* As per [JACKSON-267], '@', '#' and '*' are also to be accepted as well.
+     * And '-' (for hyphenated names); and '+' for sake of symmetricity...
+     */
+    table['@'] = 0;
+    table['#'] = 0;
+    table['*'] = 0;
+    table['-'] = 0;
+    table['+'] = 0;
+    sInputCodesJsNames = table;
+  }
+
+  /**
+   * This table is similar to Latin-1, except that it marks all "high-bit" code as ok. They will be
+   * validated at a later point, when decoding name
+   */
+  protected static final int[] sInputCodesUtf8JsNames;
+
+  static {
+    final int[] table = new int[256];
+    // start with 8-bit JS names
+    System.arraycopy(sInputCodesJsNames, 0, table, 0, table.length);
+    Arrays.fill(table, 128, 128, 0);
+    sInputCodesUtf8JsNames = table;
+  }
+
+  /**
+   * Decoding table used to quickly determine characters that are relevant within comment content.
+   */
+  protected static final int[] sInputCodesComment;
+
+  static {
+    final int[] buf = new int[256];
+    // but first: let's start with UTF-8 multi-byte markers:
+    System.arraycopy(sInputCodesUTF8, 128, buf, 128, 128);
+
+    // default (0) means "ok" (skip); -1 invalid, others marked by char itself
+    Arrays.fill(buf, 0, 32, -1); // invalid white space
+    buf['\t'] = 0; // tab is still fine
+    buf['\n'] = '\n'; // lf/cr need to be observed, ends cpp comment
+    buf['\r'] = '\r';
+    buf['*'] = '*'; // end marker for c-style comments
+    sInputCodesComment = buf;
+  }
+
+  /**
+   * Decoding table used for skipping white space and comments.
+   *
+   * @since 2.3
+   */
+  protected static final int[] sInputCodesWS;
+
+  static {
+    // but first: let's start with UTF-8 multi-byte markers:
+    final int[] buf = new int[256];
+    System.arraycopy(sInputCodesUTF8, 128, buf, 128, 128);
+
+    // default (0) means "not whitespace" (end); 1 "whitespace", -1 invalid,
+    // 2-4 UTF-8 multi-bytes, others marked by char itself
+    //
+    Arrays.fill(buf, 0, 32, -1); // invalid white space
+    buf[' '] = 1;
+    buf['\t'] = 1;
+    buf['\n'] = '\n'; // lf/cr need to be observed, ends cpp comment
+    buf['\r'] = '\r';
+    buf['/'] = '/'; // start marker for c/cpp comments
+    buf['#'] = '#'; // start marker for YAML comments
+    sInputCodesWS = buf;
+  }
+
+  /**
+   * Lookup table used for determining which output characters in 7-bit ASCII range need to be
+   * quoted.
+   */
+  protected static final int[] sOutputEscapes128;
+
+  static {
+    int[] table = new int[128];
+    // Control chars need generic escape sequence
+    for (int i = 0; i < 32; ++i) {
+      // 04-Mar-2011, tatu: Used to use "-(i + 1)", replaced with constant
+      table[i] = ESCAPE_STANDARD;
+    }
+    // Others (and some within that range too) have explicit shorter sequences
+    table['"'] = '"';
+    table['\\'] = '\\';
+    // Escaping of slash is optional, so let's not add it
+    table[0x08] = 'b';
+    table[0x09] = 't';
+    table[0x0C] = 'f';
+    table[0x0A] = 'n';
+    table[0x0D] = 'r';
+    sOutputEscapes128 = table;
+  }
+
+  /**
+   * Lookup table for the first 256 Unicode characters (ASCII / UTF-8) range. For actual hex digits,
+   * contains corresponding value; for others -1.
+   *
+   * <p>NOTE: before 2.10.1, was of size 128, extended for simpler handling
+   */
+  protected static final int[] sHexValues = new int[256];
+
+  static {
+    Arrays.fill(sHexValues, -1);
+    for (int i = 0; i < 10; ++i) {
+      sHexValues['0' + i] = i;
+    }
+    for (int i = 0; i < 6; ++i) {
+      sHexValues['a' + i] = 10 + i;
+      sHexValues['A' + i] = 10 + i;
+    }
+  }
+
+  public static int[] getInputCodeLatin1JsNames() {
+    return sInputCodesJsNames;
+  }
+
+  public static int[] getInputCodeUtf8JsNames() {
+    return sInputCodesUtf8JsNames;
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/Endpoint.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/Endpoint.java
new file mode 100644
index 000000000..a980ea324
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/Endpoint.java
@@ -0,0 +1,209 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.server;
+
+import com.google.common.base.Preconditions;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Description of a single service/component endpoint. It is designed to be marshalled as JSON.
+ *
+ * <p>Every endpoint can have more than one address entry, such as a list of URLs to a replicated
+ * service, or a (hostname, port) pair. Each of these address entries is represented as a string
+ * list, as that is the only reliably marshallable form of a tuple JSON can represent.
+ */
+public final class Endpoint implements Cloneable {
+
+  /** API implemented at the end of the binding */
+  public String api;
+
+  /** Type of address. The standard types are defined in {@link AddressTypes} */
+  public String addressType;
+
+  /** Protocol type. Some standard types are defined in {@link ProtocolTypes} */
+  public String protocolType;
+
+  /** a list of address tuples —tuples whose format depends on the address type */
+  public List<Map<String, String>> addresses;
+
+  /** Create an empty instance. */
+  public Endpoint() {}
+
+  /**
+   * Create an endpoint from another endpoint. This is a deep clone with a new list of addresses.
+   *
+   * @param that the endpoint to copy from
+   */
+  public Endpoint(Endpoint that) {
+    this.api = that.api;
+    this.addressType = that.addressType;
+    this.protocolType = that.protocolType;
+    this.addresses = newAddresses(that.addresses.size());
+    for (Map<String, String> address : that.addresses) {
+      Map<String, String> addr2 = new HashMap<String, String>(address.size());
+      addr2.putAll(address);
+      addresses.add(addr2);
+    }
+  }
+
+  /**
+   * Build an endpoint with a list of addresses
+   *
+   * @param api API name
+   * @param addressType address type
+   * @param protocolType protocol type
+   * @param addrs addresses
+   */
+  public Endpoint(
+      String api, String addressType, String protocolType, List<Map<String, String>> addrs) {
+    this.api = api;
+    this.addressType = addressType;
+    this.protocolType = protocolType;
+    this.addresses = newAddresses(0);
+    if (addrs != null) {
+      addresses.addAll(addrs);
+    }
+  }
+
+  /**
+   * Build an endpoint with an empty address list
+   *
+   * @param api API name
+   * @param addressType address type
+   * @param protocolType protocol type
+   */
+  public Endpoint(String api, String addressType, String protocolType) {
+    this.api = api;
+    this.addressType = addressType;
+    this.protocolType = protocolType;
+    this.addresses = newAddresses(0);
+  }
+
+  /**
+   * Build an endpoint with a single address entry.
+   *
+   * <p>This constructor is superfluous given the varags constructor is equivalent for a single
+   * element argument. However, type-erasure in java generics causes javac to warn about unchecked
+   * generic array creation. This constructor, which represents the common "one address" case, does
+   * not generate compile-time warnings.
+   *
+   * @param api API name
+   * @param addressType address type
+   * @param protocolType protocol type
+   * @param addr address. May be null —in which case it is not added
+   */
+  public Endpoint(String api, String addressType, String protocolType, Map<String, String> addr) {
+    this(api, addressType, protocolType);
+    if (addr != null) {
+      addresses.add(addr);
+    }
+  }
+
+  /**
+   * Build an endpoint with a list of addresses
+   *
+   * @param api API name
+   * @param addressType address type
+   * @param protocolType protocol type
+   * @param addrs addresses. Null elements will be skipped
+   */
+  public Endpoint(
+      String api, String addressType, String protocolType, Map<String, String>... addrs) {
+    this(api, addressType, protocolType);
+    for (Map<String, String> addr : addrs) {
+      if (addr != null) {
+        addresses.add(addr);
+      }
+    }
+  }
+
+  /**
+   * Create a new address structure of the requested size
+   *
+   * @param size size to create
+   * @return the new list
+   */
+  private List<Map<String, String>> newAddresses(int size) {
+    return new ArrayList<Map<String, String>>(size);
+  }
+
+  /**
+   * Build an endpoint from a list of URIs; each URI is ASCII-encoded and added to the list of
+   * addresses.
+   *
+   * @param api API name
+   * @param protocolType protocol type
+   * @param uris URIs to convert to a list of tup;les
+   */
+  public Endpoint(String api, String protocolType, URI... uris) {
+    this.api = api;
+    this.addressType = AddressTypes.ADDRESS_URI;
+
+    this.protocolType = protocolType;
+    List<Map<String, String>> addrs = newAddresses(uris.length);
+    for (URI uri : uris) {
+      addrs.add(RegistryTypeUtils.uri(uri.toString()));
+    }
+    this.addresses = addrs;
+  }
+
+  @Override
+  public String toString() {
+    return marshalToString.toString(this);
+  }
+
+  /**
+   * Validate the record by checking for null fields and other invalid conditions
+   *
+   * @throws NullPointerException if a field is null when it MUST be set.
+   * @throws RuntimeException on invalid entries
+   */
+  public void validate() {
+    Preconditions.checkNotNull(api, "null API field");
+    Preconditions.checkNotNull(addressType, "null addressType field");
+    Preconditions.checkNotNull(protocolType, "null protocolType field");
+    Preconditions.checkNotNull(addresses, "null addresses field");
+    for (Map<String, String> address : addresses) {
+      Preconditions.checkNotNull(address, "null element in address");
+    }
+  }
+
+  /**
+   * Shallow clone: the lists of addresses are shared
+   *
+   * @return a cloned instance
+   * @throws CloneNotSupportedException
+   */
+  @Override
+  public Object clone() throws CloneNotSupportedException {
+    return super.clone();
+  }
+
+  /** Static instance of service record marshalling */
+  private static class Marshal extends JsonSerDeser<Endpoint> {
+    private Marshal() {
+      super(Endpoint.class);
+    }
+  }
+
+  private static final Marshal marshalToString = new Marshal();
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/HS2ActivePassiveHARegistry.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/HS2ActivePassiveHARegistry.java
new file mode 100644
index 000000000..c569231bd
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/HS2ActivePassiveHARegistry.java
@@ -0,0 +1,227 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.server;
+
+import com.google.common.base.Preconditions;
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.util.Collection;
+import java.util.Set;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.curator.framework.recipes.cache.PathChildrenCache;
+import org.apache.curator.framework.recipes.leader.LeaderLatch;
+import org.apache.curator.utils.CloseableUtils;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.llap.registry.ServiceRegistry;
+import org.apache.hadoop.hive.registry.ServiceInstanceSet;
+import org.apache.hadoop.hive.registry.ServiceInstanceStateChangeListener;
+import org.apache.hadoop.yarn.api.records.ApplicationId;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class HS2ActivePassiveHARegistry extends ZkRegistryBase<HiveServer2Instance>
+    implements ServiceRegistry<HiveServer2Instance>, HiveServer2HAInstanceSet {
+  private static final Logger LOG = LoggerFactory.getLogger(HS2ActivePassiveHARegistry.class);
+  static final String ACTIVE_ENDPOINT = "activeEndpoint";
+  static final String PASSIVE_ENDPOINT = "passiveEndpoint";
+  private static final String SASL_LOGIN_CONTEXT_NAME = "HS2ActivePassiveHAZooKeeperClient";
+  private static final String INSTANCE_PREFIX = "instance-";
+  private static final String INSTANCE_GROUP = "instances";
+  private static final String LEADER_LATCH_PATH = "/_LEADER";
+  private LeaderLatch leaderLatch;
+  private String latchPath;
+  private ServiceRecord srv;
+  private final String uniqueId;
+
+  // There are 2 paths under which the instances get registered
+  // 1) Standard path used by ZkRegistryBase where all instances register themselves (also stores
+  // metadata)
+  // Secure: /hs2ActivePassiveHA-sasl/instances/instance-0000000000
+  // Unsecure: /hs2ActivePassiveHA-unsecure/instances/instance-0000000000
+  // 2) Leader latch path used for HS2 HA Active/Passive configuration where all instances register
+  // under _LEADER
+  //    path but only one among them is the leader
+  // Secure: /hs2ActivePassiveHA-sasl/_LEADER/xxxx-latch-0000000000
+  // Unsecure: /hs2ActivePassiveHA-unsecure/_LEADER/xxxx-latch-0000000000
+  static HS2ActivePassiveHARegistry create(Configuration conf) {
+    String zkNameSpace =
+        HiveConf.getVar(conf, HiveConf.ConfVars.HIVE_SERVER2_ACTIVE_PASSIVE_HA_REGISTRY_NAMESPACE);
+    Preconditions.checkArgument(
+        !StringUtils.isBlank(zkNameSpace),
+        HiveConf.ConfVars.HIVE_SERVER2_ACTIVE_PASSIVE_HA_REGISTRY_NAMESPACE.varname
+            + " cannot be null or empty");
+    String principal = HiveConf.getVar(conf, HiveConf.ConfVars.HIVE_SERVER2_KERBEROS_PRINCIPAL);
+    String keytab = HiveConf.getVar(conf, HiveConf.ConfVars.HIVE_SERVER2_KERBEROS_KEYTAB);
+    String zkNameSpacePrefix = zkNameSpace + "-";
+    return new HS2ActivePassiveHARegistry(
+        null, zkNameSpacePrefix, LEADER_LATCH_PATH, principal, keytab, null, conf);
+  }
+
+  private HS2ActivePassiveHARegistry(
+      final String instanceName,
+      final String zkNamespacePrefix,
+      final String leaderLatchPath,
+      final String krbPrincipal,
+      final String krbKeytab,
+      final String saslContextName,
+      final Configuration conf) {
+    super(
+        instanceName,
+        conf,
+        null,
+        zkNamespacePrefix,
+        null,
+        INSTANCE_PREFIX,
+        INSTANCE_GROUP,
+        saslContextName,
+        krbPrincipal,
+        krbKeytab,
+        null);
+    if (HiveConf.getBoolVar(conf, HiveConf.ConfVars.HIVE_IN_TEST)
+        && conf.get(ZkRegistryBase.UNIQUE_IDENTIFIER) != null) {
+      this.uniqueId = conf.get(ZkRegistryBase.UNIQUE_IDENTIFIER);
+    } else {
+      this.uniqueId = UNIQUE_ID.toString();
+    }
+    this.latchPath = leaderLatchPath;
+    this.leaderLatch = getNewLeaderLatchPath();
+  }
+
+  @Override
+  public void start() throws IOException {
+    super.start();
+    populateCache();
+    LOG.info("Populating instances cache for client");
+  }
+
+  @Override
+  protected void unregisterInternal() {
+    super.unregisterInternal();
+  }
+
+  @Override
+  public String register() throws IOException {
+    updateEndpoint(srv, PASSIVE_ENDPOINT);
+    return registerServiceRecord(srv, uniqueId);
+  }
+
+  @Override
+  public void unregister() {
+    CloseableUtils.closeQuietly(leaderLatch);
+    unregisterInternal();
+  }
+
+  private void populateCache() throws IOException {
+    PathChildrenCache pcc = ensureInstancesCache(0);
+    populateCache(pcc, false);
+  }
+
+  @Override
+  public ServiceInstanceSet<HiveServer2Instance> getInstances(
+      final String component, final long clusterReadyTimeoutMs) throws IOException {
+    throw new IOException("Not supported to get instances by component name");
+  }
+
+  private void updateEndpoint(final ServiceRecord srv, final String endpointName) {
+    final String instanceUri = srv.get(HiveServer2.INSTANCE_URI_CONFIG);
+    final String[] tokens = instanceUri.split(":");
+    final String hostname = tokens[0];
+    final int port = Integer.parseInt(tokens[1]);
+    Endpoint urlEndpoint =
+        RegistryTypeUtils.ipcEndpoint(endpointName, new InetSocketAddress(hostname, port));
+    srv.addInternalEndpoint(urlEndpoint);
+    LOG.info("Added {} endpoint to service record", urlEndpoint);
+  }
+
+  @Override
+  public void stop() {
+    CloseableUtils.closeQuietly(leaderLatch);
+    super.stop();
+  }
+
+  @Override
+  protected HiveServer2Instance createServiceInstance(final ServiceRecord srv) throws IOException {
+    Endpoint activeEndpoint = srv.getInternalEndpoint(HS2ActivePassiveHARegistry.ACTIVE_ENDPOINT);
+    return new HiveServer2Instance(
+        srv, activeEndpoint != null ? ACTIVE_ENDPOINT : PASSIVE_ENDPOINT);
+  }
+
+  @Override
+  public synchronized void registerStateChangeListener(
+      final ServiceInstanceStateChangeListener<HiveServer2Instance> listener) throws IOException {
+    super.registerStateChangeListener(listener);
+  }
+
+  @Override
+  public ApplicationId getApplicationId() throws IOException {
+    throw new IOException("Not supported until HS2 runs as YARN application");
+  }
+
+  @Override
+  protected String getZkPathUser(final Configuration conf) {
+    return RegistryUtils.currentUser();
+  }
+
+  /**
+   * Returns a new instance of leader latch path but retains the same uniqueId. This is only used
+   * when HS2 startsup or when a manual failover is triggered (in which case uniqueId will still
+   * remain as the instance has not restarted)
+   *
+   * @return - new leader latch
+   */
+  private LeaderLatch getNewLeaderLatchPath() {
+    return new LeaderLatch(
+        zooKeeperClient, latchPath, uniqueId, LeaderLatch.CloseMode.NOTIFY_LEADER);
+  }
+
+  @Override
+  public HiveServer2Instance getLeader() {
+    for (HiveServer2Instance hs2Instance : getAll()) {
+      if (hs2Instance.isLeader()) {
+        return hs2Instance;
+      }
+    }
+    return null;
+  }
+
+  @Override
+  public Collection<HiveServer2Instance> getAll() {
+    return getAllInternal();
+  }
+
+  @Override
+  public HiveServer2Instance getInstance(final String instanceId) {
+    for (HiveServer2Instance hs2Instance : getAll()) {
+      if (hs2Instance.getWorkerIdentity().equals(instanceId)) {
+        return hs2Instance;
+      }
+    }
+    return null;
+  }
+
+  @Override
+  public Set<HiveServer2Instance> getByHost(final String host) {
+    return getByHostInternal(host);
+  }
+
+  @Override
+  public int size() {
+    return sizeInternal();
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/HS2ActivePassiveHARegistryClient.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/HS2ActivePassiveHARegistryClient.java
new file mode 100644
index 000000000..e0ae541e7
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/HS2ActivePassiveHARegistryClient.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.server;
+
+import com.google.common.base.Preconditions;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.registry.impl.ZkRegistryBase;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class HS2ActivePassiveHARegistryClient {
+  private static final Logger LOG = LoggerFactory.getLogger(HS2ActivePassiveHARegistryClient.class);
+  private static final Map<String, HS2ActivePassiveHARegistry> hs2Registries = new HashMap<>();
+
+  /**
+   * Helper method to get a HiveServer2HARegistry instance to read from the registry. Only used by
+   * clients (JDBC), service discovery to connect to active HS2 instance in Active/Passive HA
+   * configuration.
+   *
+   * @param conf {@link Configuration} instance which contains service registry information.
+   * @return HiveServer2HARegistry
+   */
+  public static synchronized HS2ActivePassiveHARegistry getClient(Configuration conf)
+      throws IOException {
+    String namespace =
+        HiveConf.getVar(conf, HiveConf.ConfVars.HIVE_SERVER2_ACTIVE_PASSIVE_HA_REGISTRY_NAMESPACE);
+    Preconditions.checkArgument(
+        !StringUtils.isBlank(namespace),
+        HiveConf.ConfVars.HIVE_SERVER2_ACTIVE_PASSIVE_HA_REGISTRY_NAMESPACE.varname
+            + " cannot be null or empty");
+    String nsKey = ZkRegistryBase.getRootNamespace(null, namespace + "-");
+    HS2ActivePassiveHARegistry registry = hs2Registries.get(nsKey);
+    if (registry == null) {
+      registry = HS2ActivePassiveHARegistry.create(conf);
+      registry.start();
+      hs2Registries.put(nsKey, registry);
+      LOG.info("Added registry client to cache with namespace: {}", nsKey);
+    } else {
+      LOG.info("Returning cached registry client for namespace: {}", nsKey);
+    }
+    return registry;
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/HiveServer2.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/HiveServer2.java
new file mode 100644
index 000000000..41394bf59
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/HiveServer2.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.server;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
+
+/** HiveServer2. */
+public class HiveServer2 {
+  public static final String INSTANCE_URI_CONFIG = "hive.server2.instance.uri";
+
+  public static boolean isKerberosAuthMode(Configuration hiveConf) {
+    String authMode = hiveConf.get(ConfVars.HIVE_SERVER2_AUTHENTICATION.varname);
+    if (authMode != null && (authMode.equalsIgnoreCase("KERBEROS"))) {
+      return true;
+    }
+    return false;
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/HiveServer2HAInstanceSet.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/HiveServer2HAInstanceSet.java
new file mode 100644
index 000000000..d109fb12b
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/HiveServer2HAInstanceSet.java
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.server;
+
+import org.apache.hadoop.hive.registry.ServiceInstanceSet;
+
+public interface HiveServer2HAInstanceSet extends ServiceInstanceSet<HiveServer2Instance> {
+
+  /**
+   * In Active/Passive setup, returns current active leader.
+   *
+   * @return leader instance
+   */
+  HiveServer2Instance getLeader();
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/HiveServer2Instance.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/HiveServer2Instance.java
new file mode 100644
index 000000000..92f9117d7
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/HiveServer2Instance.java
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.server;
+
+import com.google.common.base.Preconditions;
+import java.io.IOException;
+import java.util.Objects;
+import org.apache.hadoop.hive.conf.HiveConf;
+
+public class HiveServer2Instance extends ServiceInstanceBase {
+  private boolean isLeader;
+  private String transportMode;
+  private String httpEndpoint;
+
+  // empty c'tor to make jackson happy
+  public HiveServer2Instance() {}
+
+  public HiveServer2Instance(final ServiceRecord srv, final String endPointName)
+      throws IOException {
+    super(srv, endPointName);
+
+    Endpoint activeEndpoint = srv.getInternalEndpoint(HS2ActivePassiveHARegistry.ACTIVE_ENDPOINT);
+    Endpoint passiveEndpoint = srv.getInternalEndpoint(HS2ActivePassiveHARegistry.PASSIVE_ENDPOINT);
+    this.isLeader = activeEndpoint != null;
+    Preconditions.checkArgument(
+        activeEndpoint == null || passiveEndpoint == null,
+        "Incorrect service record. Both active and passive endpoints cannot be non-null!");
+    this.transportMode = srv.get(HiveConf.ConfVars.HIVE_SERVER2_TRANSPORT_MODE.varname);
+    if (transportMode.equalsIgnoreCase("http")) {
+      this.httpEndpoint = srv.get(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_HTTP_PATH.varname);
+    } else {
+      this.httpEndpoint = "";
+    }
+  }
+
+  public boolean isLeader() {
+    return isLeader;
+  }
+
+  public String getTransportMode() {
+    return transportMode;
+  }
+
+  public String getHttpEndpoint() {
+    return httpEndpoint;
+  }
+
+  public void setLeader(final boolean leader) {
+    isLeader = leader;
+  }
+
+  public void setTransportMode(final String transportMode) {
+    this.transportMode = transportMode;
+  }
+
+  public void setHttpEndpoint(final String httpEndpoint) {
+    this.httpEndpoint = httpEndpoint;
+  }
+
+  @Override
+  public boolean equals(final Object o) {
+    if (this == o) {
+      return true;
+    }
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
+
+    HiveServer2Instance other = (HiveServer2Instance) o;
+    return super.equals(o)
+        && isLeader == other.isLeader
+        && Objects.equals(transportMode, other.transportMode)
+        && Objects.equals(httpEndpoint, other.httpEndpoint);
+  }
+
+  @Override
+  public int hashCode() {
+    return super.hashCode()
+        + Objects.hashCode(isLeader)
+        + Objects.hashCode(transportMode)
+        + Objects.hashCode(httpEndpoint);
+  }
+
+  @Override
+  public String toString() {
+    String result =
+        "instanceId: "
+            + getWorkerIdentity()
+            + " isLeader: "
+            + isLeader
+            + " host: "
+            + getHost()
+            + " port: "
+            + getRpcPort()
+            + " transportMode: "
+            + transportMode;
+    if (httpEndpoint != null) {
+      result += " httpEndpoint: " + httpEndpoint;
+    }
+    return result;
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/InvalidRecordException.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/InvalidRecordException.java
new file mode 100644
index 000000000..d3d59365f
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/InvalidRecordException.java
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.server;
+
+/** Raised if an attempt to parse a record failed. */
+public class InvalidRecordException extends RegistryIOException {
+
+  public InvalidRecordException(String path, String error) {
+    super(path, error);
+  }
+
+  public InvalidRecordException(String path, String error, Throwable cause) {
+    super(path, error, cause);
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/JsonSerDeser.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/JsonSerDeser.java
new file mode 100644
index 000000000..9b60f6230
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/JsonSerDeser.java
@@ -0,0 +1,183 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.server;
+
+import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+import com.google.common.base.Preconditions;
+import java.io.EOFException;
+import java.io.IOException;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.util.JsonSerialization;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Support for marshalling objects to and from JSON.
+ *
+ * <p>This extends {@link JsonSerialization} with the notion of a marker field in the JSON file,
+ * with
+ *
+ * <ol>
+ *   <li>a fail-fast check for it before even trying to parse.
+ *   <li>Specific IOException subclasses for a failure.
+ * </ol>
+ *
+ * <p>The rationale for this is not only to support different things in the, registry, but the fact
+ * that all ZK nodes have a size &gt; 0 when examined.
+ *
+ * @param <T> Type to marshal.
+ */
+public class JsonSerDeser<T> {
+  private static final Logger LOG = LoggerFactory.getLogger(JsonSerDeser.class);
+  private static final String UTF_8 = "UTF-8";
+  public static final String E_NO_DATA = "No data at path";
+  public static final String E_DATA_TOO_SHORT = "Data at path too short";
+  public static final String E_MISSING_MARKER_STRING = "Missing marker string: ";
+
+  private final Class<T> classType;
+  private final ObjectMapper mapper;
+
+  /**
+   * Create an instance bound to a specific type
+   *
+   * @param classType class to marshall
+   */
+  public JsonSerDeser(Class<T> classType) {
+    Preconditions.checkArgument(classType != null, "null classType");
+    this.classType = classType;
+    this.mapper = new ObjectMapper();
+    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+    mapper.configure(SerializationFeature.INDENT_OUTPUT, false);
+  }
+
+  /**
+   * Deserialize from a byte array
+   *
+   * @param path path the data came from
+   * @param bytes byte array
+   * @throws IOException all problems
+   * @throws EOFException not enough data
+   * @throws InvalidRecordException if the parsing failed -the record is invalid
+   * @throws NoRecordException if the data is not considered a record: either it is too short or it
+   *     did not contain the marker string.
+   */
+  public T fromBytes(String path, byte[] bytes) throws IOException {
+    return fromBytes(path, bytes, "");
+  }
+
+  /**
+   * Deserialize from a byte array, optionally checking for a marker string.
+   *
+   * <p>If the marker parameter is supplied (and not empty), then its presence will be verified
+   * before the JSON parsing takes place; it is a fast-fail check. If not found, an {@link
+   * InvalidRecordException} exception will be raised
+   *
+   * @param path path the data came from
+   * @param bytes byte array
+   * @param marker an optional string which, if set, MUST be present in the UTF-8 parsed payload.
+   * @return The parsed record
+   * @throws IOException all problems
+   * @throws EOFException not enough data
+   * @throws InvalidRecordException if the JSON parsing failed.
+   * @throws NoRecordException if the data is not considered a record: either it is too short or it
+   *     did not contain the marker string.
+   */
+  public T fromBytes(String path, byte[] bytes, String marker) throws IOException {
+    int len = bytes.length;
+    if (len == 0) {
+      throw new NoRecordException(path, E_NO_DATA);
+    }
+    if (StringUtils.isNotEmpty(marker) && len < marker.length()) {
+      throw new NoRecordException(path, E_DATA_TOO_SHORT);
+    }
+    String json = new String(bytes, 0, len, UTF_8);
+    if (StringUtils.isNotEmpty(marker) && !json.contains(marker)) {
+      throw new NoRecordException(path, E_MISSING_MARKER_STRING + marker);
+    }
+    try {
+      return fromJson(json);
+    } catch (JsonProcessingException e) {
+      throw new InvalidRecordException(path, e.toString(), e);
+    }
+  }
+
+  /**
+   * Convert an instance to a string form for output. This is a robust operation which will convert
+   * any JSON-generating exceptions into error text.
+   *
+   * @param instance non-null instance
+   * @return a JSON string
+   */
+  public String toString(T instance) {
+    Preconditions.checkArgument(instance != null, "Null instance argument");
+    try {
+      return toJson(instance);
+    } catch (JsonProcessingException e) {
+      return "Failed to convert to a string: " + e;
+    }
+  }
+
+  /**
+   * Convert an instance to a JSON string.
+   *
+   * @param instance instance to convert
+   * @return a JSON string description
+   * @throws JsonProcessingException Json generation problems
+   */
+  public synchronized String toJson(T instance) throws JsonProcessingException {
+    return mapper.writeValueAsString(instance);
+  }
+
+  /**
+   * Convert from JSON.
+   *
+   * @param json input
+   * @return the parsed JSON
+   * @throws IOException IO problems
+   * @throws JsonParseException If the input is not well-formatted
+   * @throws JsonMappingException failure to map from the JSON to this class
+   */
+  public synchronized T fromJson(String json)
+      throws IOException, JsonParseException, JsonMappingException {
+    if (json.isEmpty()) {
+      throw new EOFException("No data");
+    }
+    try {
+      return mapper.readValue(json, classType);
+    } catch (IOException e) {
+      LOG.error("Exception while parsing json : {}\n{}", e, json, e);
+      throw e;
+    }
+  }
+
+  /**
+   * Convert JSON to bytes.
+   *
+   * @param instance instance to convert
+   * @return a byte array
+   * @throws IOException IO problems
+   */
+  public byte[] toBytes(T instance) throws IOException {
+    return mapper.writeValueAsBytes(instance);
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/NoRecordException.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/NoRecordException.java
new file mode 100644
index 000000000..5e6a665e1
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/NoRecordException.java
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.server;
+
+/**
+ * Raised if there is no {@link ServiceRecord} resolved at the end of the specified path.
+ *
+ * <p>There may be valid data of some form at the end of the path, but it does not appear to be a
+ * Service Record.
+ */
+public class NoRecordException extends RegistryIOException {
+
+  public NoRecordException(String path, String error) {
+    super(path, error);
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/RegistryIOException.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/RegistryIOException.java
new file mode 100644
index 000000000..07127999e
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/RegistryIOException.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.server;
+
+import org.apache.hadoop.fs.PathIOException;
+
+/**
+ * Base exception for registry operations.
+ *
+ * <p>These exceptions include the path of the failing operation wherever possible; this can be
+ * retrieved via {@link PathIOException#getPath()}.
+ */
+public class RegistryIOException extends PathIOException {
+
+  public RegistryIOException(String path, String error) {
+    super(path, error);
+  }
+
+  public RegistryIOException(String path, String error, Throwable cause) {
+    super(path, error, cause);
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/RegistryTypeUtils.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/RegistryTypeUtils.java
new file mode 100644
index 000000000..d174df621
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/RegistryTypeUtils.java
@@ -0,0 +1,156 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.server;
+
+import static org.apache.kyuubi.jdbc.hive.server.AddressTypes.*;
+
+import java.net.InetSocketAddress;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Static methods to work with registry types —primarily endpoints and the list representation of
+ * addresses.
+ */
+public class RegistryTypeUtils {
+
+  /**
+   * Create an IPC endpoint
+   *
+   * @param api API
+   * @param address the address as a tuple of (hostname, port)
+   * @return the new endpoint
+   */
+  public static Endpoint ipcEndpoint(String api, InetSocketAddress address) {
+    return new Endpoint(
+        api,
+        ADDRESS_HOSTNAME_AND_PORT,
+        "hadoop/IPC",
+        address == null ? null : hostnamePortPair(address));
+  }
+
+  /**
+   * Create a single entry map
+   *
+   * @param key map entry key
+   * @param val map entry value
+   * @return a 1 entry map.
+   */
+  public static Map<String, String> map(String key, String val) {
+    Map<String, String> map = new HashMap<String, String>(1);
+    map.put(key, val);
+    return map;
+  }
+
+  /**
+   * Create a URI
+   *
+   * @param uri value
+   * @return a 1 entry map.
+   */
+  public static Map<String, String> uri(String uri) {
+    return map(ADDRESS_URI, uri);
+  }
+
+  /**
+   * Create a (hostname, port) address pair
+   *
+   * @param hostname hostname
+   * @param port port
+   * @return a 1 entry map.
+   */
+  public static Map<String, String> hostnamePortPair(String hostname, int port) {
+    Map<String, String> map = map(ADDRESS_HOSTNAME_FIELD, hostname);
+    map.put(ADDRESS_PORT_FIELD, Integer.toString(port));
+    return map;
+  }
+
+  /**
+   * Create a (hostname, port) address pair
+   *
+   * @param address socket address whose hostname and port are used for the generated address.
+   * @return a 1 entry map.
+   */
+  public static Map<String, String> hostnamePortPair(InetSocketAddress address) {
+    return hostnamePortPair(address.getHostName(), address.getPort());
+  }
+
+  /**
+   * Get a specific field from an address -raising an exception if the field is not present
+   *
+   * @param address address to query
+   * @param field field to resolve
+   * @return the resolved value. Guaranteed to be non-null.
+   * @throws InvalidRecordException if the field did not resolve
+   */
+  public static String getAddressField(Map<String, String> address, String field)
+      throws InvalidRecordException {
+    String val = address.get(field);
+    if (val == null) {
+      throw new InvalidRecordException("", "Missing address field: " + field);
+    }
+    return val;
+  }
+
+  /**
+   * Validate the record by checking for null fields and other invalid conditions
+   *
+   * @param path path for exceptions
+   * @param record record to validate. May be null
+   * @throws InvalidRecordException on invalid entries
+   */
+  public static void validateServiceRecord(String path, ServiceRecord record)
+      throws InvalidRecordException {
+    if (record == null) {
+      throw new InvalidRecordException(path, "Null record");
+    }
+    if (!ServiceRecord.RECORD_TYPE.equals(record.type)) {
+      throw new InvalidRecordException(path, "invalid record type field: \"" + record.type + "\"");
+    }
+
+    if (record.external != null) {
+      for (Endpoint endpoint : record.external) {
+        validateEndpoint(path, endpoint);
+      }
+    }
+    if (record.internal != null) {
+      for (Endpoint endpoint : record.internal) {
+        validateEndpoint(path, endpoint);
+      }
+    }
+  }
+
+  /**
+   * Validate the endpoint by checking for null fields and other invalid conditions
+   *
+   * @param path path for exceptions
+   * @param endpoint endpoint to validate. May be null
+   * @throws InvalidRecordException on invalid entries
+   */
+  public static void validateEndpoint(String path, Endpoint endpoint)
+      throws InvalidRecordException {
+    if (endpoint == null) {
+      throw new InvalidRecordException(path, "Null endpoint");
+    }
+    try {
+      endpoint.validate();
+    } catch (RuntimeException e) {
+      throw new InvalidRecordException(path, e.toString());
+    }
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/RegistryUtils.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/RegistryUtils.java
new file mode 100644
index 000000000..0b78bf0aa
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/RegistryUtils.java
@@ -0,0 +1,129 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.server;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.io.IOException;
+import java.net.IDN;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.security.UserGroupInformation;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Utility methods for working with a registry. */
+public class RegistryUtils {
+  private static final Logger LOG = LoggerFactory.getLogger(RegistryUtils.class);
+
+  /**
+   * Convert the username to that which can be used for registry entries. Lower cases it, Strip the
+   * kerberos realm off a username if needed, and any "/" hostname entries
+   *
+   * @param username user
+   * @return the converted username
+   */
+  public static String convertUsername(String username) {
+    String converted = org.apache.hadoop.util.StringUtils.toLowerCase(username);
+    int atSymbol = converted.indexOf('@');
+    if (atSymbol > 0) {
+      converted = converted.substring(0, atSymbol);
+    }
+    int slashSymbol = converted.indexOf('/');
+    if (slashSymbol > 0) {
+      converted = converted.substring(0, slashSymbol);
+    }
+    return converted;
+  }
+
+  /**
+   * Get the current username, before any encoding has been applied.
+   *
+   * @return the current user from the kerberos identity, falling back to the user and/or env
+   *     variables.
+   */
+  private static String currentUsernameUnencoded() {
+    String env_hadoop_username = System.getenv("HADOOP_USER_NAME");
+    return getCurrentUsernameUnencoded(env_hadoop_username);
+  }
+
+  /**
+   * Get the current username, using the value of the parameter <code>env_hadoop_username</code> if
+   * it is set on an insecure cluster. This ensures that the username propagates correctly across
+   * processes started by YARN.
+   *
+   * <p>This method is primarly made visible for testing.
+   *
+   * @param env_hadoop_username the environment variable
+   * @return the selected username
+   * @throws RuntimeException if there is a problem getting the short user name of the current user.
+   */
+  @VisibleForTesting
+  public static String getCurrentUsernameUnencoded(String env_hadoop_username) {
+    String shortUserName = null;
+    if (!UserGroupInformation.isSecurityEnabled()) {
+      shortUserName = env_hadoop_username;
+    }
+    if (StringUtils.isEmpty(shortUserName)) {
+      try {
+        shortUserName = UserGroupInformation.getCurrentUser().getShortUserName();
+      } catch (IOException e) {
+        throw new RuntimeException(e);
+      }
+    }
+    return shortUserName;
+  }
+
+  /**
+   * Get the current user path formatted for the registry
+   *
+   * <p>In an insecure cluster, the environment variable <code>HADOOP_USER_NAME </code> is queried
+   * <i>first</i>.
+   *
+   * <p>This means that in a YARN container where the creator set this environment variable to
+   * propagate their identity, the defined user name is used in preference to the actual user.
+   *
+   * <p>In a secure cluster, the kerberos identity of the current user is used.
+   *
+   * @return the encoded shortname of the current user
+   * @throws RuntimeException if the current user identity cannot be determined from the
+   *     OS/kerberos.
+   */
+  public static String currentUser() {
+    String shortUserName = currentUsernameUnencoded();
+    return registryUser(shortUserName);
+  }
+
+  /**
+   * Convert the given user name formatted for the registry.
+   *
+   * @param shortUserName
+   * @return converted user name
+   */
+  public static String registryUser(String shortUserName) {
+    String encodedName = IDN.toASCII(shortUserName);
+    // DNS name doesn't allow "_", replace it with "-"
+    encodedName = RegistryUtils.convertUsername(encodedName);
+    return encodedName.replace("_", "-");
+  }
+
+  /** Static instance of service record marshalling */
+  public static class ServiceRecordMarshal extends JsonSerDeser<ServiceRecord> {
+    public ServiceRecordMarshal() {
+      super(ServiceRecord.class);
+    }
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/ServiceInstanceBase.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/ServiceInstanceBase.java
new file mode 100644
index 000000000..5041c8cc2
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/ServiceInstanceBase.java
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.server;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.Objects;
+import org.apache.hadoop.hive.registry.ServiceInstance;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ServiceInstanceBase implements ServiceInstance {
+  private static final Logger LOG = LoggerFactory.getLogger(ServiceInstanceBase.class);
+  private String host;
+  private int rpcPort;
+  private String workerIdentity;
+  private Map<String, String> properties;
+
+  public ServiceInstanceBase() {}
+
+  public ServiceInstanceBase(ServiceRecord srv, String rpcName) throws IOException {
+    if (LOG.isTraceEnabled()) {
+      LOG.trace("Working with ServiceRecord: {}", srv);
+    }
+
+    Endpoint rpc = srv.getInternalEndpoint(rpcName);
+    this.host = RegistryTypeUtils.getAddressField((Map) rpc.addresses.get(0), "host");
+    this.rpcPort =
+        Integer.parseInt(RegistryTypeUtils.getAddressField((Map) rpc.addresses.get(0), "port"));
+    this.workerIdentity = srv.get("registry.unique.id");
+    this.properties = srv.attributes();
+  }
+
+  public boolean equals(Object o) {
+    if (this == o) {
+      return true;
+    } else if (o != null && this.getClass() == o.getClass()) {
+      ServiceInstanceBase other = (ServiceInstanceBase) o;
+      return Objects.equals(this.getWorkerIdentity(), other.getWorkerIdentity())
+          && Objects.equals(this.host, other.host)
+          && Objects.equals(this.rpcPort, other.rpcPort);
+    } else {
+      return false;
+    }
+  }
+
+  public int hashCode() {
+    return this.getWorkerIdentity().hashCode() + 31 * this.host.hashCode() + 31 * this.rpcPort;
+  }
+
+  public String getWorkerIdentity() {
+    return this.workerIdentity;
+  }
+
+  public String getHost() {
+    return this.host;
+  }
+
+  public int getRpcPort() {
+    return this.rpcPort;
+  }
+
+  public Map<String, String> getProperties() {
+    return this.properties;
+  }
+
+  public void setHost(String host) {
+    this.host = host;
+  }
+
+  public void setRpcPort(int rpcPort) {
+    this.rpcPort = rpcPort;
+  }
+
+  public void setWorkerIdentity(String workerIdentity) {
+    this.workerIdentity = workerIdentity;
+  }
+
+  public void setProperties(Map<String, String> properties) {
+    this.properties = properties;
+  }
+
+  public String toString() {
+    return "DynamicServiceInstance [id="
+        + this.getWorkerIdentity()
+        + ", host="
+        + this.host
+        + ":"
+        + this.rpcPort
+        + "]";
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/ServiceRecord.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/ServiceRecord.java
new file mode 100644
index 000000000..4a41b15f6
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/ServiceRecord.java
@@ -0,0 +1,291 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.server;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * JSON-marshallable description of a single component. It supports the deserialization of unknown
+ * attributes, but does not support their creation.
+ */
+public class ServiceRecord implements Cloneable {
+
+  /**
+   * A type string which MUST be in the serialized json. This permits fast discarding of invalid
+   * entries
+   */
+  public static final String RECORD_TYPE = "JSONServiceRecord";
+
+  /** The type field. This must be the string {@link #RECORD_TYPE} */
+  public String type = RECORD_TYPE;
+
+  /** Description string */
+  public String description;
+
+  /** map to handle unknown attributes. */
+  private Map<String, String> attributes = new HashMap<String, String>(4);
+
+  /** List of endpoints intended for use to external callers */
+  public List<Endpoint> external = new ArrayList<Endpoint>();
+
+  /** List of endpoints for use <i>within</i> an application. */
+  public List<Endpoint> internal = new ArrayList<Endpoint>();
+
+  /**
+   * Create a service record with no ID, description or registration time. Endpoint lists are set to
+   * empty lists.
+   */
+  public ServiceRecord() {}
+
+  /**
+   * Deep cloning constructor
+   *
+   * @param that service record source
+   */
+  public ServiceRecord(ServiceRecord that) {
+    this.description = that.description;
+    // others
+    Map<String, String> thatAttrs = that.attributes;
+    for (Map.Entry<String, String> entry : thatAttrs.entrySet()) {
+      attributes.put(entry.getKey(), entry.getValue());
+    }
+    // endpoints
+    List<Endpoint> src = that.internal;
+    if (src != null) {
+      internal = new ArrayList<Endpoint>(src.size());
+      for (Endpoint endpoint : src) {
+        internal.add(new Endpoint(endpoint));
+      }
+    }
+    src = that.external;
+    if (src != null) {
+      external = new ArrayList<Endpoint>(src.size());
+      for (Endpoint endpoint : src) {
+        external.add(new Endpoint(endpoint));
+      }
+    }
+  }
+
+  /**
+   * Add an external endpoint
+   *
+   * @param endpoint endpoint to set
+   */
+  public void addExternalEndpoint(Endpoint endpoint) {
+    Preconditions.checkArgument(endpoint != null);
+    endpoint.validate();
+    external.add(endpoint);
+  }
+
+  /**
+   * Add an internal endpoint
+   *
+   * @param endpoint endpoint to set
+   */
+  public void addInternalEndpoint(Endpoint endpoint) {
+    Preconditions.checkArgument(endpoint != null);
+    endpoint.validate();
+    internal.add(endpoint);
+  }
+
+  /**
+   * Look up an internal endpoint
+   *
+   * @param api API
+   * @return the endpoint or null if there was no match
+   */
+  public Endpoint getInternalEndpoint(String api) {
+    return findByAPI(internal, api);
+  }
+
+  /**
+   * Look up an external endpoint
+   *
+   * @param api API
+   * @return the endpoint or null if there was no match
+   */
+  public Endpoint getExternalEndpoint(String api) {
+    return findByAPI(external, api);
+  }
+
+  /**
+   * Handle unknown attributes by storing them in the {@link #attributes} map
+   *
+   * @param key attribute name
+   * @param value attribute value.
+   */
+  public void set(String key, Object value) {
+    attributes.put(key, value.toString());
+  }
+
+  /**
+   * The map of "other" attributes set when parsing. These are not included in the JSON value of
+   * this record when it is generated.
+   *
+   * @return a map of any unknown attributes in the deserialized JSON.
+   */
+  public Map<String, String> attributes() {
+    return attributes;
+  }
+
+  /**
+   * Get the "other" attribute with a specific key
+   *
+   * @param key key to look up
+   * @return the value or null
+   */
+  public String get(String key) {
+    return attributes.get(key);
+  }
+
+  /**
+   * Get the "other" attribute with a specific key.
+   *
+   * @param key key to look up
+   * @param defVal default value
+   * @return the value as a string, or <code>defval</code> if the value was not present
+   */
+  public String get(String key, String defVal) {
+    String val = attributes.get(key);
+    return val != null ? val : defVal;
+  }
+
+  /**
+   * Find an endpoint by its API
+   *
+   * @param list list
+   * @param api api name
+   * @return the endpoint or null if there was no match
+   */
+  private Endpoint findByAPI(List<Endpoint> list, String api) {
+    for (Endpoint endpoint : list) {
+      if (endpoint.api.equals(api)) {
+        return endpoint;
+      }
+    }
+    return null;
+  }
+
+  @Override
+  public String toString() {
+    final StringBuilder sb = new StringBuilder("ServiceRecord{");
+    sb.append("description='").append(description).append('\'');
+    sb.append("; external endpoints: {");
+    for (Endpoint endpoint : external) {
+      sb.append(endpoint).append("; ");
+    }
+    sb.append("}; internal endpoints: {");
+    for (Endpoint endpoint : internal) {
+      sb.append(endpoint != null ? endpoint.toString() : "NULL ENDPOINT");
+      sb.append("; ");
+    }
+    sb.append('}');
+
+    if (!attributes.isEmpty()) {
+      sb.append(", attributes: {");
+      for (Map.Entry<String, String> attr : attributes.entrySet()) {
+        sb.append("\"").append(attr.getKey()).append("\"=\"").append(attr.getValue()).append("\" ");
+      }
+    } else {
+
+      sb.append(", attributes: {");
+    }
+    sb.append('}');
+
+    sb.append('}');
+    return sb.toString();
+  }
+
+  /**
+   * Shallow clone: all endpoints will be shared across instances
+   *
+   * @return a clone of the instance
+   * @throws CloneNotSupportedException
+   */
+  @Override
+  protected Object clone() throws CloneNotSupportedException {
+    return super.clone();
+  }
+
+  @Override
+  public int hashCode() {
+    // Generated by eclipse
+    final int prime = 31;
+    int result = 1;
+    result = prime * result + ((attributes == null) ? 0 : attributes.hashCode());
+    result = prime * result + ((description == null) ? 0 : description.hashCode());
+    result = prime * result + ((external == null) ? 0 : external.hashCode());
+    result = prime * result + ((internal == null) ? 0 : internal.hashCode());
+    result = prime * result + ((type == null) ? 0 : type.hashCode());
+    return result;
+  }
+
+  @Override
+  public boolean equals(Object obj) {
+    if (this == obj) {
+      return true;
+    }
+    if (obj == null) {
+      return false;
+    }
+    if (getClass() != obj.getClass()) {
+      return false;
+    }
+    ServiceRecord other = (ServiceRecord) obj;
+    if (attributes == null) {
+      if (other.attributes != null) {
+        return false;
+      }
+    } else if (!attributes.equals(other.attributes)) {
+      return false;
+    }
+    if (description == null) {
+      if (other.description != null) {
+        return false;
+      }
+    } else if (!description.equals(other.description)) {
+      return false;
+    }
+    if (external == null) {
+      if (other.external != null) {
+        return false;
+      }
+    } else if (!external.equals(other.external)) {
+      return false;
+    }
+    if (internal == null) {
+      if (other.internal != null) {
+        return false;
+      }
+    } else if (!internal.equals(other.internal)) {
+      return false;
+    }
+    if (type == null) {
+      if (other.type != null) {
+        return false;
+      }
+    } else if (!type.equals(other.type)) {
+      return false;
+    }
+    return true;
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/ZkRegistryBase.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/ZkRegistryBase.java
new file mode 100644
index 000000000..144c35780
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/ZkRegistryBase.java
@@ -0,0 +1,697 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.server;
+
+import com.google.common.base.Joiner;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+import java.io.IOException;
+import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+import org.apache.curator.framework.CuratorFramework;
+import org.apache.curator.framework.CuratorFrameworkFactory;
+import org.apache.curator.framework.api.ACLProvider;
+import org.apache.curator.framework.imps.CuratorFrameworkState;
+import org.apache.curator.framework.recipes.cache.ChildData;
+import org.apache.curator.framework.recipes.cache.PathChildrenCache;
+import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
+import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
+import org.apache.curator.framework.recipes.nodes.PersistentEphemeralNode;
+import org.apache.curator.framework.recipes.nodes.PersistentEphemeralNode.Mode;
+import org.apache.curator.framework.state.ConnectionState;
+import org.apache.curator.framework.state.ConnectionStateListener;
+import org.apache.curator.retry.ExponentialBackoffRetry;
+import org.apache.curator.utils.CloseableUtils;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
+import org.apache.hadoop.hive.registry.RegistryUtilities;
+import org.apache.hadoop.hive.registry.ServiceInstance;
+import org.apache.hadoop.hive.registry.ServiceInstanceStateChangeListener;
+import org.apache.hadoop.security.UserGroupInformation;
+import org.apache.hadoop.yarn.conf.YarnConfiguration;
+import org.apache.kyuubi.jdbc.hive.server.RegistryUtils.ServiceRecordMarshal;
+import org.apache.zookeeper.KeeperException.InvalidACLException;
+import org.apache.zookeeper.ZooDefs;
+import org.apache.zookeeper.data.ACL;
+import org.apache.zookeeper.data.Id;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This is currently used for implementation inheritance only; it doesn't provide a unified flow
+ * into which one can just plug a few abstract method implementations, because providing one with
+ * getInstance method is a huge pain involving lots of generics. Also, different registries may have
+ * slightly different usage patterns anyway and noone would use a registry without knowing what type
+ * it is. So, it's mostly a grab bag of methods used by ServiceInstanceSet and other parts of each
+ * implementation.
+ */
+public abstract class ZkRegistryBase<InstanceType extends ServiceInstance> {
+  private static final Logger LOG = LoggerFactory.getLogger(ZkRegistryBase.class);
+  private static final String SASL_NAMESPACE = "sasl";
+  private static final String UNSECURE_NAMESPACE = "unsecure";
+  protected static final String WORKER_PREFIX = "worker-";
+  public static final String UNIQUE_IDENTIFIER = "registry.unique.id";
+  protected static final UUID UNIQUE_ID = UUID.randomUUID();
+  private static final Joiner PATH_JOINER = Joiner.on("/").skipNulls();
+
+  protected final Configuration conf;
+  protected final CuratorFramework zooKeeperClient;
+  // workersPath is the directory path where all the worker znodes are located.
+  protected final String workersPath;
+  private final String workerNodePrefix;
+
+  protected final ServiceRecordMarshal encoder; // to marshal/unmarshal znode data
+
+  private final Set<ServiceInstanceStateChangeListener<InstanceType>> stateChangeListeners;
+
+  protected final boolean doCheckAcls;
+  // Secure ZK is only set up by the registering service; anyone can read the registrations.
+  private final String zkPrincipal, zkKeytab, saslLoginContextName;
+  private String userNameFromPrincipal; // Only set when setting up the secure config for ZK.
+  private final String disableMessage;
+
+  private final Lock instanceCacheLock = new ReentrantLock();
+  // there can be only one instance per path
+  private final Map<String, InstanceType> pathToInstanceCache;
+  // there can be multiple instances per node
+  private final Map<String, Set<InstanceType>> nodeToInstanceCache;
+
+  // The registration znode.
+  private PersistentEphemeralNode znode;
+  private String znodePath; // unique identity for this instance
+
+  private PathChildrenCache instancesCache; // Created on demand.
+
+  /** Local hostname. */
+  protected static final String hostname = RegistryUtilities.getCanonicalHostName();
+
+  /**
+   * @param rootNs A single root namespace override. Not recommended.
+   * @param nsPrefix The namespace prefix to use with default namespaces (appends 'sasl' for secure
+   *     else 'unsecure' to namespace prefix to get effective root namespace).
+   * @param userScopePathPrefix The prefix to use for the user-specific part of the path.
+   * @param workerPrefix The prefix to use for each worker znode.
+   * @param workerGroup group name to use for all workers
+   * @param zkSaslLoginContextName SASL login context name for ZK security; null if not needed.
+   * @param zkPrincipal ZK security principal.
+   * @param zkKeytab ZK security keytab.
+   * @param aclsConfig A config setting to use to determine if ACLs should be verified.
+   */
+  public ZkRegistryBase(
+      String instanceName,
+      Configuration conf,
+      String rootNs,
+      String nsPrefix,
+      String userScopePathPrefix,
+      String workerPrefix,
+      String workerGroup,
+      String zkSaslLoginContextName,
+      String zkPrincipal,
+      String zkKeytab,
+      ConfVars aclsConfig) {
+    this.conf = new Configuration(conf);
+    this.saslLoginContextName = zkSaslLoginContextName;
+    this.zkPrincipal = zkPrincipal;
+    this.zkKeytab = zkKeytab;
+    if (aclsConfig != null) {
+      this.doCheckAcls = HiveConf.getBoolVar(conf, aclsConfig);
+      this.disableMessage = "Set " + aclsConfig.varname + " to false to disable ACL validation";
+    } else {
+      this.doCheckAcls = true;
+      this.disableMessage = "";
+    }
+    this.conf.addResource(YarnConfiguration.YARN_SITE_CONFIGURATION_FILE);
+    this.encoder = new ServiceRecordMarshal();
+
+    // sample path: /llap-sasl/hiveuser/hostname/workers/worker-0000000
+    // worker-0000000 is the sequence number which will be retained until session timeout. If a
+    // worker does not respond due to communication interruptions it will retain the same sequence
+    // number when it returns back. If session timeout expires, the node will be deleted and new
+    // addition of the same node (restart) will get next sequence number
+    final String userPathPrefix =
+        userScopePathPrefix == null ? null : userScopePathPrefix + getZkPathUser(conf);
+    this.workerNodePrefix = workerPrefix == null ? WORKER_PREFIX : workerPrefix;
+    this.workersPath = "/" + PATH_JOINER.join(userPathPrefix, instanceName, workerGroup);
+    this.instancesCache = null;
+    this.stateChangeListeners = new HashSet<>();
+    this.pathToInstanceCache = new ConcurrentHashMap<>();
+    this.nodeToInstanceCache = new ConcurrentHashMap<>();
+    final String namespace = getRootNamespace(rootNs, nsPrefix);
+    ACLProvider aclProvider;
+    // get acl provider for most outer path that is non-null
+    if (userPathPrefix == null) {
+      if (instanceName == null) {
+        if (workerGroup == null) {
+          aclProvider = getACLProviderForZKPath(namespace);
+        } else {
+          aclProvider = getACLProviderForZKPath(workerGroup);
+        }
+      } else {
+        aclProvider = getACLProviderForZKPath(instanceName);
+      }
+    } else {
+      aclProvider = getACLProviderForZKPath(userScopePathPrefix);
+    }
+    this.zooKeeperClient = getZookeeperClient(conf, namespace, aclProvider);
+    this.zooKeeperClient
+        .getConnectionStateListenable()
+        .addListener(new ZkConnectionStateListener());
+  }
+
+  public static String getRootNamespace(
+      String userProvidedNamespace, String defaultNamespacePrefix) {
+    final boolean isSecure = UserGroupInformation.isSecurityEnabled();
+    String rootNs = userProvidedNamespace;
+    if (rootNs == null) {
+      rootNs = defaultNamespacePrefix + (isSecure ? SASL_NAMESPACE : UNSECURE_NAMESPACE);
+    }
+    return rootNs;
+  }
+
+  private ACLProvider getACLProviderForZKPath(String zkPath) {
+    final boolean isSecure = UserGroupInformation.isSecurityEnabled();
+    return new ACLProvider() {
+      @Override
+      public List<ACL> getDefaultAcl() {
+        // We always return something from getAclForPath so this should not happen.
+        LOG.warn("getDefaultAcl was called");
+        return Lists.newArrayList(ZooDefs.Ids.OPEN_ACL_UNSAFE);
+      }
+
+      @Override
+      public List<ACL> getAclForPath(String path) {
+        if (!isSecure || path == null || !path.contains(zkPath)) {
+          // No security or the path is below the user path - full access.
+          return Lists.newArrayList(ZooDefs.Ids.OPEN_ACL_UNSAFE);
+        }
+        return createSecureAcls();
+      }
+    };
+  }
+
+  private CuratorFramework getZookeeperClient(
+      Configuration conf, String namespace, ACLProvider zooKeeperAclProvider) {
+    String zkEnsemble = getQuorumServers(conf);
+    int sessionTimeout =
+        (int)
+            HiveConf.getTimeVar(
+                conf, ConfVars.HIVE_ZOOKEEPER_SESSION_TIMEOUT, TimeUnit.MILLISECONDS);
+    int connectionTimeout =
+        (int)
+            HiveConf.getTimeVar(
+                conf, ConfVars.HIVE_ZOOKEEPER_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
+    int baseSleepTime =
+        (int)
+            HiveConf.getTimeVar(
+                conf, ConfVars.HIVE_ZOOKEEPER_CONNECTION_BASESLEEPTIME, TimeUnit.MILLISECONDS);
+    int maxRetries = HiveConf.getIntVar(conf, ConfVars.HIVE_ZOOKEEPER_CONNECTION_MAX_RETRIES);
+
+    LOG.info(
+        "Creating curator client with connectString: {} sessionTimeoutMs: {} connectionTimeoutMs: {}"
+            + " namespace: {} exponentialBackoff - sleepTime: {} maxRetries: {}",
+        zkEnsemble,
+        sessionTimeout,
+        connectionTimeout,
+        namespace,
+        baseSleepTime,
+        maxRetries);
+    // Create a CuratorFramework instance to be used as the ZooKeeper client
+    // Use the zooKeeperAclProvider to create appropriate ACLs
+    return CuratorFrameworkFactory.builder()
+        .connectString(zkEnsemble)
+        .sessionTimeoutMs(sessionTimeout)
+        .connectionTimeoutMs(connectionTimeout)
+        .aclProvider(zooKeeperAclProvider)
+        .namespace(namespace)
+        .retryPolicy(new ExponentialBackoffRetry(baseSleepTime, maxRetries))
+        .build();
+  }
+
+  private static List<ACL> createSecureAcls() {
+    // Read all to the world
+    List<ACL> nodeAcls = new ArrayList<>(ZooDefs.Ids.READ_ACL_UNSAFE);
+    // Create/Delete/Write/Admin to creator
+    nodeAcls.addAll(ZooDefs.Ids.CREATOR_ALL_ACL);
+    return nodeAcls;
+  }
+
+  /**
+   * Get the ensemble server addresses from the configuration. The format is: host1:port,
+   * host2:port..
+   *
+   * @param conf configuration
+   */
+  private static String getQuorumServers(Configuration conf) {
+    String[] hosts = conf.getTrimmedStrings(ConfVars.HIVE_ZOOKEEPER_QUORUM.varname);
+    String port =
+        conf.get(
+            ConfVars.HIVE_ZOOKEEPER_CLIENT_PORT.varname,
+            ConfVars.HIVE_ZOOKEEPER_CLIENT_PORT.getDefaultValue());
+    StringBuilder quorum = new StringBuilder();
+    for (int i = 0; i < hosts.length; i++) {
+      quorum.append(hosts[i].trim());
+      if (!hosts[i].contains(":")) {
+        // if the hostname doesn't contain a port, add the configured port to hostname
+        quorum.append(":");
+        quorum.append(port);
+      }
+
+      if (i != hosts.length - 1) {
+        quorum.append(",");
+      }
+    }
+
+    return quorum.toString();
+  }
+
+  protected abstract String getZkPathUser(Configuration conf);
+
+  protected final String registerServiceRecord(ServiceRecord srv, final String uniqueId)
+      throws IOException {
+    // restart sensitive instance id
+    srv.set(UNIQUE_IDENTIFIER, uniqueId);
+
+    // Create a znode under the rootNamespace parent for this instance of the server
+    try {
+      // PersistentEphemeralNode will make sure the ephemeral node created on server will be present
+      // even under connection or session interruption (will automatically handle retries)
+      znode =
+          new PersistentEphemeralNode(
+              zooKeeperClient,
+              Mode.EPHEMERAL_SEQUENTIAL,
+              workersPath + "/" + workerNodePrefix,
+              encoder.toBytes(srv));
+
+      // start the creation of znodes
+      znode.start();
+
+      // We'll wait for 120s for node creation
+      long znodeCreationTimeout = 120;
+      if (!znode.waitForInitialCreate(znodeCreationTimeout, TimeUnit.SECONDS)) {
+        throw new Exception(
+            "Max znode creation wait time: " + znodeCreationTimeout + "s exhausted");
+      }
+
+      znodePath = znode.getActualPath();
+
+      if (doCheckAcls) {
+        try {
+          checkAndSetAcls();
+        } catch (Exception ex) {
+          throw new IOException("Error validating or setting ACLs. " + disableMessage, ex);
+        }
+      }
+      if (zooKeeperClient.checkExists().forPath(znodePath) == null) {
+        // No node exists, throw exception
+        throw new Exception("Unable to create znode for this instance on ZooKeeper.");
+      }
+    } catch (Exception e) {
+      LOG.error("Unable to create a znode for this server instance", e);
+      CloseableUtils.closeQuietly(znode);
+      throw (e instanceof IOException) ? (IOException) e : new IOException(e);
+    }
+    return uniqueId;
+  }
+
+  protected final void updateServiceRecord(
+      ServiceRecord srv, boolean doCheckAcls, boolean closeOnFailure) throws IOException {
+    if (srv.get(UNIQUE_IDENTIFIER) == null) {
+      srv.set(UNIQUE_IDENTIFIER, UNIQUE_ID.toString());
+    }
+    // waitForInitialCreate must have already been called in registerServiceRecord.
+    try {
+      znode.setData(encoder.toBytes(srv));
+
+      if (doCheckAcls) {
+        try {
+          checkAndSetAcls();
+        } catch (Exception ex) {
+          throw new IOException("Error validating or setting ACLs. " + disableMessage, ex);
+        }
+      }
+    } catch (Exception e) {
+      LOG.error("Unable to update znode with new service record", e);
+      if (closeOnFailure) {
+        CloseableUtils.closeQuietly(znode);
+      }
+      throw (e instanceof IOException) ? (IOException) e : new IOException(e);
+    }
+  }
+
+  private void checkAndSetAcls() throws Exception {
+    if (!UserGroupInformation.isSecurityEnabled()) return;
+    // We are trying to check ACLs on the "workers" directory, which noone except us should be
+    // able to write to. Higher-level directories shouldn't matter - we don't read them.
+    String pathToCheck = workersPath;
+    List<ACL> acls = zooKeeperClient.getACL().forPath(pathToCheck);
+    if (acls == null || acls.isEmpty()) {
+      // Can there be no ACLs? There's some access (to get ACLs), so assume it means free for all.
+      LOG.warn("No ACLs on " + pathToCheck + "; setting up ACLs. " + disableMessage);
+      setUpAcls(pathToCheck);
+      return;
+    }
+    // This could be brittle.
+    assert userNameFromPrincipal != null;
+    Id currentUser = new Id("sasl", userNameFromPrincipal);
+    for (ACL acl : acls) {
+      if ((acl.getPerms() & ~ZooDefs.Perms.READ) == 0 || currentUser.equals(acl.getId())) {
+        continue; // Read permission/no permissions, or the expected user.
+      }
+      LOG.warn(
+          "The ACL "
+              + acl
+              + " is unnacceptable for "
+              + pathToCheck
+              + "; setting up ACLs. "
+              + disableMessage);
+      setUpAcls(pathToCheck);
+      return;
+    }
+  }
+
+  private void setUpAcls(String path) throws Exception {
+    List<ACL> acls = createSecureAcls();
+    LinkedList<String> paths = new LinkedList<>();
+    paths.add(path);
+    while (!paths.isEmpty()) {
+      String currentPath = paths.poll();
+      List<String> children = zooKeeperClient.getChildren().forPath(currentPath);
+      if (children != null) {
+        for (String child : children) {
+          paths.add(currentPath + "/" + child);
+        }
+      }
+      zooKeeperClient.setACL().withACL(acls).forPath(currentPath);
+    }
+  }
+
+  private void addToCache(String path, String host, InstanceType instance) {
+    instanceCacheLock.lock();
+    try {
+      putInInstanceCache(path, pathToInstanceCache, instance);
+      putInNodeCache(host, nodeToInstanceCache, instance);
+    } finally {
+      instanceCacheLock.unlock();
+    }
+    LOG.debug(
+        "Added path={}, host={} instance={} to cache."
+            + " pathToInstanceCache:size={}, nodeToInstanceCache:size={}",
+        path,
+        host,
+        instance,
+        pathToInstanceCache.size(),
+        nodeToInstanceCache.size());
+  }
+
+  private void removeFromCache(String path, String host) {
+    instanceCacheLock.lock();
+    try {
+      pathToInstanceCache.remove(path);
+      nodeToInstanceCache.remove(host);
+    } finally {
+      instanceCacheLock.unlock();
+    }
+    LOG.debug(
+        "Removed path={}, host={} from cache."
+            + " pathToInstanceCache:size={}, nodeToInstanceCache:size={}",
+        path,
+        host,
+        pathToInstanceCache.size(),
+        nodeToInstanceCache.size());
+  }
+
+  private void putInInstanceCache(
+      String key, Map<String, InstanceType> cache, InstanceType instance) {
+    cache.put(key, instance);
+  }
+
+  private void putInNodeCache(
+      String key, Map<String, Set<InstanceType>> cache, InstanceType instance) {
+    Set<InstanceType> instanceSet = cache.get(key);
+    if (instanceSet == null) {
+      instanceSet = new HashSet<>();
+      instanceSet.add(instance);
+    }
+    cache.put(key, instanceSet);
+  }
+
+  protected final void populateCache(PathChildrenCache instancesCache, boolean doInvokeListeners) {
+    for (ChildData childData : instancesCache.getCurrentData()) {
+      byte[] data = getWorkerData(childData, workerNodePrefix);
+      if (data == null) continue;
+      String nodeName = extractNodeName(childData);
+      if (!nodeName.startsWith(workerNodePrefix)) continue;
+      int ephSeqVersion = extractSeqNum(nodeName);
+      try {
+        ServiceRecord srv = encoder.fromBytes(childData.getPath(), data);
+        InstanceType instance = createServiceInstance(srv);
+        addToCache(childData.getPath(), instance.getHost(), instance);
+        if (doInvokeListeners) {
+          for (ServiceInstanceStateChangeListener<InstanceType> listener : stateChangeListeners) {
+            listener.onCreate(instance, ephSeqVersion);
+          }
+        }
+      } catch (IOException e) {
+        LOG.error(
+            "Unable to decode data for zkpath: {}." + " Ignoring from current instances list..",
+            childData.getPath());
+      }
+    }
+  }
+
+  protected abstract InstanceType createServiceInstance(ServiceRecord srv) throws IOException;
+
+  protected static byte[] getWorkerData(ChildData childData, String workerNodePrefix) {
+    if (childData == null) return null;
+    byte[] data = childData.getData();
+    if (data == null) return null;
+    if (!extractNodeName(childData).startsWith(workerNodePrefix)) return null;
+    return data;
+  }
+
+  private class InstanceStateChangeListener implements PathChildrenCacheListener {
+    private final Logger LOG = LoggerFactory.getLogger(InstanceStateChangeListener.class);
+
+    @Override
+    public void childEvent(final CuratorFramework client, final PathChildrenCacheEvent event) {
+      Preconditions.checkArgument(
+          client != null && client.getState() == CuratorFrameworkState.STARTED,
+          "client is not started");
+
+      synchronized (this) {
+        ChildData childData = event.getData();
+        if (childData == null) return;
+        String nodeName = extractNodeName(childData);
+        if (!nodeName.startsWith(workerNodePrefix)) return;
+        LOG.info("{} for zknode {}", event.getType(), childData.getPath());
+        InstanceType instance = extractServiceInstance(event, childData);
+        if (instance != null) {
+          int ephSeqVersion = extractSeqNum(nodeName);
+          switch (event.getType()) {
+            case CHILD_ADDED:
+              addToCache(childData.getPath(), instance.getHost(), instance);
+              for (ServiceInstanceStateChangeListener<InstanceType> listener :
+                  stateChangeListeners) {
+                listener.onCreate(instance, ephSeqVersion);
+              }
+              break;
+            case CHILD_UPDATED:
+              addToCache(childData.getPath(), instance.getHost(), instance);
+              for (ServiceInstanceStateChangeListener<InstanceType> listener :
+                  stateChangeListeners) {
+                listener.onUpdate(instance, ephSeqVersion);
+              }
+              break;
+            case CHILD_REMOVED:
+              removeFromCache(childData.getPath(), instance.getHost());
+              for (ServiceInstanceStateChangeListener<InstanceType> listener :
+                  stateChangeListeners) {
+                listener.onRemove(instance, ephSeqVersion);
+              }
+              break;
+            default:
+              // Ignore all the other events; logged above.
+          }
+        } else {
+          LOG.info("instance is null for event: {} childData: {}", event.getType(), childData);
+        }
+      }
+    }
+  }
+
+  // The real implementation for the instanceset... instanceset has its own copy of the
+  // ZK cache yet completely depends on the parent in every other aspect and is thus unneeded.
+
+  protected final int sizeInternal() {
+    // not using the path child cache here as there could be more than 1 path per host (worker and
+    // slot znodes)
+    return nodeToInstanceCache.size();
+  }
+
+  protected final Set<InstanceType> getByHostInternal(String host) {
+    Set<InstanceType> byHost = nodeToInstanceCache.get(host);
+    byHost = (byHost == null) ? Sets.newHashSet() : byHost;
+    if (LOG.isDebugEnabled()) {
+      LOG.debug("Returning " + byHost.size() + " hosts for locality allocation on " + host);
+    }
+    return byHost;
+  }
+
+  protected final Collection<InstanceType> getAllInternal() {
+    return new HashSet<>(pathToInstanceCache.values());
+  }
+
+  private static String extractNodeName(ChildData childData) {
+    String nodeName = childData.getPath();
+    int ix = nodeName.lastIndexOf("/");
+    if (ix >= 0) {
+      nodeName = nodeName.substring(ix + 1);
+    }
+    return nodeName;
+  }
+
+  private InstanceType extractServiceInstance(PathChildrenCacheEvent event, ChildData childData) {
+    byte[] data = childData.getData();
+    if (data == null) return null;
+    try {
+      ServiceRecord srv = encoder.fromBytes(event.getData().getPath(), data);
+      return createServiceInstance(srv);
+    } catch (IOException e) {
+      LOG.error(
+          "Unable to decode data for zknode: {}." + " Dropping notification of type: {}",
+          childData.getPath(),
+          event.getType());
+      return null;
+    }
+  }
+
+  public synchronized void registerStateChangeListener(
+      ServiceInstanceStateChangeListener<InstanceType> listener) throws IOException {
+    ensureInstancesCache(0);
+    this.stateChangeListeners.add(listener);
+  }
+
+  @SuppressWarnings("resource") // Bogus warnings despite closeQuietly.
+  protected final synchronized PathChildrenCache ensureInstancesCache(long clusterReadyTimeoutMs)
+      throws IOException {
+    Preconditions.checkArgument(
+        zooKeeperClient != null && zooKeeperClient.getState() == CuratorFrameworkState.STARTED,
+        "client is not started");
+    // lazily create PathChildrenCache
+    PathChildrenCache instancesCache = this.instancesCache;
+    if (instancesCache != null) return instancesCache;
+    ExecutorService tp =
+        Executors.newFixedThreadPool(
+            1,
+            new ThreadFactoryBuilder()
+                .setDaemon(true)
+                .setNameFormat("StateChangeNotificationHandler")
+                .build());
+    long startTimeNs = System.nanoTime(), deltaNs = clusterReadyTimeoutMs * 1000000L;
+    long sleepTimeMs = Math.min(16, clusterReadyTimeoutMs);
+    while (true) {
+      instancesCache = new PathChildrenCache(zooKeeperClient, workersPath, true);
+      instancesCache.getListenable().addListener(new InstanceStateChangeListener(), tp);
+      try {
+        instancesCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
+        this.instancesCache = instancesCache;
+        return instancesCache;
+      } catch (InvalidACLException e) {
+        // PathChildrenCache tried to mkdir when the znode wasn't there, and failed.
+        CloseableUtils.closeQuietly(instancesCache);
+        long elapsedNs = System.nanoTime() - startTimeNs;
+        if (deltaNs == 0 || deltaNs <= elapsedNs) {
+          LOG.error("Unable to start curator PathChildrenCache", e);
+          throw new IOException(e);
+        }
+        LOG.warn("The cluster is not started yet (InvalidACL); will retry");
+        try {
+          Thread.sleep(Math.min(sleepTimeMs, (deltaNs - elapsedNs) / 1000000L));
+        } catch (InterruptedException e1) {
+          LOG.error("Interrupted while retrying the PathChildrenCache startup");
+          throw new IOException(e1);
+        }
+        sleepTimeMs = sleepTimeMs << 1;
+      } catch (Exception e) {
+        CloseableUtils.closeQuietly(instancesCache);
+        LOG.error("Unable to start curator PathChildrenCache", e);
+        throw new IOException(e);
+      }
+    }
+  }
+
+  public void start() throws IOException {
+    if (zooKeeperClient != null) {
+      String principal =
+          ZookeeperUtils.setupZookeeperAuth(conf, saslLoginContextName, zkPrincipal, zkKeytab);
+      if (principal != null) {
+        userNameFromPrincipal = getUserNameFromPrincipal(principal);
+      }
+      zooKeeperClient.start();
+    }
+    // Init closeable utils in case register is not called (see HIVE-13322)
+    CloseableUtils.class.getName();
+  }
+
+  private String getUserNameFromPrincipal(String principal) {
+    if (principal == null) {
+      return null;
+    } else {
+      String[] components = principal.split("[/@]");
+      return components != null && components.length == 3 ? components[0] : principal;
+    }
+  }
+
+  protected void unregisterInternal() {
+    CloseableUtils.closeQuietly(znode);
+  }
+
+  public void stop() {
+    CloseableUtils.closeQuietly(znode);
+    CloseableUtils.closeQuietly(instancesCache);
+    CloseableUtils.closeQuietly(zooKeeperClient);
+  }
+
+  private int extractSeqNum(String nodeName) {
+    // Extract the sequence number of this ephemeral-sequential znode.
+    String ephSeqVersionStr = nodeName.substring(workerNodePrefix.length() + 1);
+    try {
+      return Integer.parseInt(ephSeqVersionStr);
+    } catch (NumberFormatException e) {
+      LOG.error("Cannot parse " + ephSeqVersionStr + " from " + nodeName, e);
+      throw e;
+    }
+  }
+
+  // for debugging
+  private class ZkConnectionStateListener implements ConnectionStateListener {
+    @Override
+    public void stateChanged(
+        final CuratorFramework curatorFramework, final ConnectionState connectionState) {
+      LOG.info("Connection state change notification received. State: {}", connectionState);
+    }
+  }
+}
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/ZookeeperUtils.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/ZookeeperUtils.java
new file mode 100644
index 000000000..86570c775
--- /dev/null
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/server/ZookeeperUtils.java
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kyuubi.jdbc.hive.server;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import javax.security.auth.login.AppConfigurationEntry;
+import javax.security.auth.login.AppConfigurationEntry.LoginModuleControlFlag;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.security.SecurityUtil;
+import org.apache.hadoop.security.UserGroupInformation;
+import org.apache.hadoop.security.authentication.util.KerberosUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ZookeeperUtils {
+  private static final Logger LOG = LoggerFactory.getLogger(ZookeeperUtils.class);
+
+  public ZookeeperUtils() {}
+
+  public static String setupZookeeperAuth(
+      Configuration conf, String saslLoginContextName, String zkPrincipal, String zkKeytab)
+      throws IOException {
+    if (UserGroupInformation.isSecurityEnabled() && saslLoginContextName != null) {
+      LOG.info("UGI security is enabled. Setting up ZK auth.");
+      if (zkPrincipal != null && !zkPrincipal.isEmpty()) {
+        if (zkKeytab != null && !zkKeytab.isEmpty()) {
+          return setZookeeperClientKerberosJaasConfig(saslLoginContextName, zkPrincipal, zkKeytab);
+        } else {
+          throw new IOException("Kerberos keytab is empty");
+        }
+      } else {
+        throw new IOException("Kerberos principal is empty");
+      }
+    } else {
+      LOG.info(
+          "UGI security is not enabled, or no SASL context name. Skipping setting up ZK auth.");
+      return null;
+    }
+  }
+
+  private static String setZookeeperClientKerberosJaasConfig(
+      String saslLoginContextName, String zkPrincipal, String zkKeytab) throws IOException {
+    System.setProperty("zookeeper.sasl.clientconfig", saslLoginContextName);
+    String principal = SecurityUtil.getServerPrincipal(zkPrincipal, "0.0.0.0");
+    ZookeeperUtils.JaasConfiguration jaasConf =
+        new ZookeeperUtils.JaasConfiguration(saslLoginContextName, principal, zkKeytab);
+    javax.security.auth.login.Configuration.setConfiguration(jaasConf);
+    return principal;
+  }
+
+  private static class JaasConfiguration extends javax.security.auth.login.Configuration {
+    private final javax.security.auth.login.Configuration baseConfig =
+        javax.security.auth.login.Configuration.getConfiguration();
+    private final String loginContextName;
+    private final String principal;
+    private final String keyTabFile;
+
+    public JaasConfiguration(String loginContextName, String principal, String keyTabFile) {
+      this.loginContextName = loginContextName;
+      this.principal = principal;
+      this.keyTabFile = keyTabFile;
+    }
+
+    public AppConfigurationEntry[] getAppConfigurationEntry(String appName) {
+      if (this.loginContextName.equals(appName)) {
+        Map<String, String> krbOptions = new HashMap();
+        krbOptions.put("doNotPrompt", "true");
+        krbOptions.put("storeKey", "true");
+        krbOptions.put("useKeyTab", "true");
+        krbOptions.put("principal", this.principal);
+        krbOptions.put("keyTab", this.keyTabFile);
+        krbOptions.put("refreshKrb5Config", "true");
+        AppConfigurationEntry zooKeeperClientEntry =
+            new AppConfigurationEntry(
+                KerberosUtil.getKrb5LoginModuleName(), LoginModuleControlFlag.REQUIRED, krbOptions);
+        return new AppConfigurationEntry[] {zooKeeperClientEntry};
+      } else {
+        return this.baseConfig != null ? this.baseConfig.getAppConfigurationEntry(appName) : null;
+      }
+    }
+  }
+}