无法从Eclipse建立与Hive的JDBC连接

时间:2014-03-16 01:42:01

标签: java hadoop jdbc hive cloudera

我正在尝试与Hive建立JDBC连接,以便我可以查看和创建表并从Eclipse查询Hive表。我使用了HiveClient示例代码:https://cwiki.apache.org/confluence/display/Hive/HiveClient 然后我将所有必需的jar添加到eclipse中的java构建路径并启动了Hive Thrift Server。端口10000正在侦听。我正在使用Cloudera QuickstartVM 4.6.1及其附带的eclipse。这是我尝试运行代码时在IDE中遇到的错误。

Exception in thread "main" java.sql.SQLException: org.apache.thrift.transport.TTransportException: java.net.SocketException: Connection reset
    at org.apache.hadoop.hive.jdbc.HiveStatement.executeQuery(HiveStatement.java:191)
    at org.apache.hadoop.hive.jdbc.HiveStatement.execute(HiveStatement.java:127)
    at org.apache.hadoop.hive.jdbc.HiveConnection.configureConnection(HiveConnection.java:108)
    at org.apache.hadoop.hive.jdbc.HiveConnection.<init>(HiveConnection.java:103)
    at org.apache.hadoop.hive.jdbc.HiveDriver.connect(HiveDriver.java:104)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:185)
    at jdbc.Hive.main(Hive.java:24)

当我尝试使用beeline连接到Hive时,我得到了同样的错误。但是,当我从!connect命令中删除主机名和端口时,它会处理以下错误:

beeline> !connect jdbc:hive:// "" ""                 
scan complete in 4ms
Connecting to jdbc:hive://
14/03/21 18:42:03 WARN conf.HiveConf: DEPRECATED: Configuration property hive.metastore.local no longer has any effect. Make sure to provide a valid value for hive.metastore.uris if you are connecting to a remote metastore.
14/03/21 18:42:03 INFO metastore.HiveMetaStore: 0: Opening raw store with implemenation class:org.apache.hadoop.hive.metastore.ObjectStore
14/03/21 18:42:04 INFO metastore.ObjectStore: ObjectStore, initialize called
14/03/21 18:42:05 INFO DataNucleus.Persistence: Property datanucleus.cache.level2 unknown - will be ignored.

我在这里缺少什么!?

1 个答案:

答案 0 :(得分:12)

您有两个选项可以使用jdbc

连接hiveserver

选项1 :Hiveserver2

您正在尝试连接hiveserver2,cloudera manager中的hiveserver版本是hivesever2,这比hiveserver更安全。您正在使用的JDBC代码是hiveserver,请将以下代码段用于hiveserver2

Class.forName("org.apache.hive.jdbc.HiveDriver");
Connection con = DriverManager.getConnection("jdbc:hive2://localhost: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)");
String sql = "show tables '" + tableName + "'";

如果查看连接字符串,可以看到hiveserver版本2( jdbc:hive2:// localhost:10000 / default “,”“,”“),第二个和第三个参数是用户名和密码,默认情况下保留为空字符串“”。

为执行此程序,添加hiveserver2特定库。

不是编写自己的程序来检查hiveserver2 jdbc连接,而是可以按如下方式使用beeline hive客户端

> [testuser02@Abcd-Host1 ~]$ beeline

> beeline> !connect jdbc:hive2://Abcd-Host1:10000/default "" "" ""
> 
> 0: jdbc:hive2://Abcd-Host1:10000/default> show tables;

+------------+
|  tab_name  |
+------------+
| sample_07  |
| sample_08  |
| test1      |
+------------+
3 rows selected (0.334 seconds)

选项2: Hiveserver1

如果您想使用现有代码(hiveserver1的代码),那么您拥有https://cwiki.apache.org/confluence/display/Hive/HiveClient。您必须在另一个端口的用户空间中启动一个新的hiveserver。使用以下命令在给定端口

中启动配置单元

nohup hive --service hiveserver -p 10001&amp;

现在在jdbc连接中将端口号更改为10001并运行它。