How to get the Kyuubi JDBC driver

Kyuubi Thrift API is fully compatible with HiveServer2, so technically, it allows to use any Hive JDBC driver to connect Kyuubi Server. But it’s recommended to use Kyuubi Hive JDBC driver, which is forked from Hive 3.1.x JDBC driver, aims to support some missing functionalities of the original Hive JDBC driver.

The driver is available from Maven Central:

  1. <dependency>
  2. <groupId>org.apache.kyuubi</groupId>
  3. <artifactId>kyuubi-hive-jdbc-shaded</artifactId>
  4. <version>1.7.0</version>
  5. </dependency>

Connect to non-kerberized Kyuubi Server

The following java code connects directly to the Kyuubi Server by JDBC without using kerberos authentication.

  1. package org.apache.kyuubi.examples;
  2. import java.sql.*;
  3. public class KyuubiJDBC {
  4. private static String driverName = "org.apache.kyuubi.jdbc.KyuubiHiveDriver";
  5. private static String kyuubiJdbcUrl = "jdbc:kyuubi://localhost:10009/default;";
  6. public static void main(String[] args) throws SQLException {
  7. try (Connection conn = DriverManager.getConnection(kyuubiJdbcUrl)) {
  8. try (Statement stmt = conn.createStatement()) {
  9. try (ResultSet rs = stmt.executeQuery("show databases")) {
  10. while (rs.next()) {
  11. System.out.println(rs.getString(1));
  12. }
  13. }
  14. }
  15. }
  16. }
  17. }

Connect to Kerberized Kyuubi Server

The following Java code uses a keytab file to login and connect to Kyuubi Server by JDBC.

  1. package org.apache.kyuubi.examples;
  2. import java.sql.*;
  3. public class KyuubiJDBCDemo {
  4. private static String driverName = "org.apache.kyuubi.jdbc.KyuubiHiveDriver";
  5. private static String kyuubiJdbcUrlTemplate = "jdbc:kyuubi://localhost:10009/default;" +
  6. "kyuubiClientPrincipal=%s;kyuubiClientKeytab=%s;kyuubiServerPrincipal=%s";
  7. public static void main(String[] args) throws SQLException {
  8. String clientPrincipal = args[0]; // Kerberos principal
  9. String clientKeytab = args[1]; // Keytab file location
  10. String serverPrincipal = args[2]; // Kerberos principal used by Kyuubi Server
  11. String kyuubiJdbcUrl = String.format(kyuubiJdbcUrlTemplate, clientPrincipal, clientKeytab, serverPrincipal);
  12. try (Connection conn = DriverManager.getConnection(kyuubiJdbcUrl)) {
  13. try (Statement stmt = conn.createStatement()) {
  14. try (ResultSet rs = stmt.executeQuery("show databases")) {
  15. while (rs.next()) {
  16. System.out.println(rs.getString(1));
  17. }
  18. }
  19. }
  20. }
  21. }
  22. }