如何使用jdbc从java代码连接hiveserver2?

lmyy7pcs  于 2021-06-26  发布在  Hive
关注(0)|答案(0)|浏览(150)

在下面添加我的代码。当我试图运行代码时,它显示“连接超时”错误。我正在从一个单独的系统中尝试这个代码。基本上,我正在尝试从本地笔记本电脑远程连接到hiveserver2。

package hive_test;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
public class HiveJdbcClientv1 {
  private static String driverName = "org.apache.hive.jdbc.HiveDriver";
  /**
   * @param args
   * @throws SQLException
   */
  public static void main(String[] args) throws SQLException {
      try {
      Class.forName(driverName);
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      System.exit(1);
    }
    //replace "hive" here with the name of the user the queries should run as
    Connection con = DriverManager.getConnection("jdbc:hive2://xxx.xxx.xx.xxx:10000/default", "hive", "");
    Statement stmt = con.createStatement();
    String tableName = "testHiveDriverTable";
    stmt.execute("drop table if exists " + tableName);
    stmt.execute("create table " + tableName + " (key int, value string)");
    // show tables
    // String sql = "show tables '" + tableName + "'";
    String sql = ("show tables");
    ResultSet res = stmt.executeQuery(sql);
    if (res.next()) {
        System.out.println(res.getString(1));
      }
  }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题