无法通过 jdbc 连接到 hive 3

Posted

技术标签:

【中文标题】无法通过 jdbc 连接到 hive 3【英文标题】:unable to connect to hive 3 via jdbc 【发布时间】:2021-05-30 15:18:30 【问题描述】:

我按照https://luckymrwang.github.io/2018/03/14/Install-hive-on-Mac-with-Homebrew/ 中提到的步骤使用 Homebrew 在 Mac 书中安装 Apache Hive。它安装了3.1.2_3的版本。

我使用命令 hive 并创建了一些表。

我去beeline命令行并发出命令!connect jdbc:hive2://(我没有指定任何连接URL,因为文档说不需要指定hive是否安装在同一系统中)并输入密码为hive-site.xml 中配置的 hive 和 hive。它成功连接到蜂巢。我使用了命令show tables 并验证我能够看到表格。

现在我尝试通过 Java JDBC 应用程序连接它。

依赖关系:

compile 'org.apache.hive:hive-jdbc:1.1.0'
compile 'org.apache.hadoop:hadoop-client:3.3.0'

代码:

Class.forName("org.apache.hive.jdbc.HiveDriver");

Connection connection = DriverManager.getConnection("jdbc:hive2://", "", "");
Statement statement = connection.createStatement();

String table = "CUSTOMER";
statement.executeQuery("DROP TABLE " + table);

但是,由于连接被拒绝,我不断收到错误消息。我不确定连接字符串是否正确。你能帮忙解决这个问题吗?

这是异常跟踪

Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.hive.ql.metadata.HiveException

我使用命令hive --hiveconf hive.root.logger=DEBUG,console 在调试模式下运行 hive。我也没有看到任何异常。

您可以在此处找到 hive-site.xml。 https://github.com/Jagannathan6/hive-site.xml/blob/main/hive-site.xml

echo $HADOOP_CLASSPATH in terminal gives the following.
/usr/local/Cellar/hadoop/3.3.0/libexec:/usr/local/Cellar/hive/3.1.2_3/libexec/lib/hive-jdbc-3.1.2.jar:/usr/local/Cellar/hive/3.1.2_3/libexec/lib/hive-exec-3.1.2.jar:/usr/local/Cellar/hive/3.1.2_3/libexec/lib/hive-metastore-3.1.2.jar:/usr/local/Cellar/hive/3.1.2_3/libexec/lib/hive-service-3.1.2.jar:/usr/local/Cellar/hive/3.1.2_3/libexec/lib/libthrift-0.9.3.jar:/usr/local/Cellar/hive/3.1.2_3/libexec/lib/libfb303-0.9.3.jar:/usr/local/Cellar/hive/3.1.2_3/libexec/lib/jdo-api-3.0.1.jar:/usr/local/Cellar/hive/3.1.2_3/libexec/lib/antlr-runtime-3.4.jar:/usr/local/Cellar/hive/3.1.2_3/libexec/lib/datanucleus-api-jdo-4.2.4.jar:/usr/local/Cellar/hive/3.1.2_3/libexec/lib/datanucleus-core-4.1.17.jar:/usr/local/Cellar/hive/3.1.2_3/libexec/lib/datanucleus-rdbms-4.1.19.jar:/usr/local/Cellar/hive/3.1.2_3/libexec/lib/mysql-connector-java-8.0.25.jar:/usr/local/Cellar/hive/3.1.2_3/libexec/conf

类路径中也添加了以下 jar。

【问题讨论】:

您在 Driver.getConnection DriverManager.getConnection("jdbc:hive2://", username, password) 中缺少用户名和密码 默认用户名和密码是什么?我没有设置。所以我猜应该是“”?我的理解错了吗? 您在通过命令行登录时提供了什么? 根据你的配置,用户名是hiveuser10,密码是password,然后把DriverManager.getConnection("jdbc:hive2://", "", "");改成DriverManager.getConnection("jdbc:hive2://", "hiveuser10", "password") 当我使用命令行登录时。我不提供任何用户名和密码。我只需输入 hive 并开始输入命令。我也尝试过使用 hiveuser10 和密码。它没有用。 【参考方案1】:

您的原始错误 org.apache.hadoop.hive.ql.metadata.HiveException 未找到,表示您的类路径中缺少 hive-exec jar,其中包含 HiveException 类。

hive-exec jar 添加到您的依赖项列表中。

    compile 'org.apache.hive:hive-jdbc:1.1.0'
    compile 'org.apache.hadoop:hadoop-client:3.3.0'

    // add this dependency
    compile 'org.apache.hive:hive-exec:1.1.0' 

【讨论】:

你得到什么错误?仍然没有在 hive 异常中找到该类?

以上是关于无法通过 jdbc 连接到 hive 3的主要内容,如果未能解决你的问题,请参考以下文章