无法建立从Eclipse到Hive的JDBC连接 [英] Unable to establish a JDBC connection to Hive from Eclipse

查看:319
本文介绍了无法建立从Eclipse到Hive的JDBC连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立一个到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中运行代码时遇到的错误。

 线程main中的异常java.sql .SQLException:org.apache.thrift.transport.TTransportException:java.net.SocketException:连接重置
在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)
(HiveConnection.java:103)
at org.apache.hadoop.hive.jdbc.HiveDriver.connect(HiveDriver.java:104)$(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)

当我尝试使用直线连接到Hive时,出现同样的错误。但是,当我从!connect命令中删除主机名和端口时,它将与以下错误一起工作:

 直线> !连接jdbc:hive://
在4ms中扫描完成
连接到jdbc:hive://
14/03/21 18:42:03 WARN conf.HiveConf :DEPRECATED:配置属性hive.metastore.local不再有任何作用。如果连接到远程元数据库,请确保为hive.metastore.uris提供有效值。
14/03/21 18:42:03 INFO metastore.HiveMetaStore:0:使用实现类打开原始存储:org.apache.hadoop.hive.metastore.ObjectStore
14/03/21 18: 42:04 INFO metastore.ObjectStore:ObjectStore,初始化称为
14/03/21 18:42:05 INFO DataNucleus.Persistence:属性datanucleus.cache.level2未知 - 将被忽略。

我在这里错过了什么!?

解决方案

您有两个选项可以使用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 drop 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 

>直线>!connect jdbc: hive2:// Abcd-Host1:10000 / default
>
> 0:jdbc:hive2:// Abcd-Host1:10000 / default>显示表格;

+ ------------ +
| tab_name |
+ ------------ +
| sample_07 |
| sample_08 |
| test1 |
+ ------------ +
选中3行(0.334秒)

选项2: Hiveserver1



如果您想使用现有的代码(代码为hiveserver1),您需要 https ://cwiki.apache.org/confluence/display/Hive/HiveClient 。您必须在另一个端口的用户空间中启动一个新的配置单元服务器。使用以下命令在给定端口中启动配置单元服务器



nohup hive --service hiveserver -p 10001&



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


I am trying to establish a JDBC connection to Hive so that I can view and create tables and query Hive tables from Eclipse. I used HiveClient sample code: https://cwiki.apache.org/confluence/display/Hive/HiveClient Then I added all the required jars to the java build path inside eclipse and started Hive Thrift Server. Port 10000 is listening. I am using Cloudera QuickstartVM 4.6.1 and the eclipse that comes with it. Here's the error that I get in the IDE when I try to run the code.

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)

When I try connecting to Hive using beeline, I get the same error. However, when I eliminate the host name and port from the !connect command it works with the following error:

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.

What am I missing here!?

解决方案

You have 2 options to connect hiveserver using jdbc

Option 1 : Hiveserver2

You are trying to connect hiveserver2, hiveserver version in cloudera manager is hivesever2, which is more secure than hiveserver. JDBC code you are using is hiveserver,Use the following code snippet for 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 + "'";

If you look at the connection string, can see the hiveserver version 2(jdbc:hive2://localhost:10000/default", "", ""), second and third arguments are username and password, by default keep it empty string "".

For executing this program add hiveserver2 specific libraries.

Instead of writing your own programs for checking hiveserver2 jdbc connection, beeline hive client can be used as follows

> [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)

Options 2: Hiveserver1

If you want to make use of your existing code(code for hiveserver1), which you are having https://cwiki.apache.org/confluence/display/Hive/HiveClient. You got to start a new hiveserver in your userspace in another port. Use the following command to start a hiveserver in a given port

nohup hive --service hiveserver -p 10001 &

Now change the port number to 10001 in jdbc connection and run it.

这篇关于无法建立从Eclipse到Hive的JDBC连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆