Support Questions

Find answers, ask questions, and share your expertise

How to connect to Hive through jdbc

avatar
Expert Contributor

 

Hi everyone, I am trying to get jdbc connection to Hive working (I have to do some hive queries from java program).

I've succesfully established connection to Impala with the following driver and connection string:

 

 

Class.forName("com.cloudera.impala.jdbc4.Driver");

String url = "jdbc:impala://worker01:21050;AuthMech=0"

 

 

But I can't open connection with Hive. I've tried the followings:

 

 

Class.forName("com.cloudera.hive.jdbc4.HS2Driver");

String url = "jdbc:hive2://master:10000;UID=cloudera;PWD=cloudera";


// EXCEPTION ---->

java.sql.SQLException: [Simba][HiveJDBCDriver](500310) Invalid operation: Peer indicated failure: Error validating the login;
	at com.cloudera.hive.hivecommon.api.HiveServer2ClientFactory.createTransport(HiveServer2ClientFactory.java:224)
	at com.cloudera.hive.hive.api.ExtendedHS2Factory.createClient(ExtendedHS2Factory.java:38)
	at com.cloudera.hive.hivecommon.core.HiveJDBCConnection.connect(HiveJDBCConnection.java:597)
	at com.cloudera.hive.jdbc.common.BaseConnectionFactory.doConnect(BaseConnectionFactory.java:219)
	at com.cloudera.hive.jdbc.common.AbstractDriver.connect(AbstractDriver.java:216)
	at java.sql.DriverManager.getConnection(DriverManager.java:664)
	at java.sql.DriverManager.getConnection(DriverManager.java:247)

 

 

 

Class.forName("com.cloudera.hive.jdbc4.HS2Driver");

String url = "jdbc:hive2://master:10000;AuthMech=0";


// EXCEPTION ---->

[Simba][HiveJDBCDriver](500150) Error setting/closing connection: Open Session Error.
at com.cloudera.hive.hive.api.ExtendedHS2Client.openSession(ExtendedHS2Client.java:1107)
at com.cloudera.hive.hivecommon.api.HS2Client.<init>(HS2Client.java:139)
at com.cloudera.hive.hive.api.ExtendedHS2Client.<init>(ExtendedHS2Client.java:474)
at com.cloudera.hive.hive.api.ExtendedHS2Factory.createClient(ExtendedHS2Factory.java:39)
at com.cloudera.hive.hivecommon.core.HiveJDBCConnection.connect(HiveJDBCConnection.java:597)
at com.cloudera.hive.jdbc.common.BaseConnectionFactory.doConnect(BaseConnectionFactory.java:219)
at com.cloudera.hive.jdbc.common.AbstractDriver.connect(AbstractDriver.java:216)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)

 

 

Class.forName("com.cloudera.hive.jdbc4.HS1Driver");

String url = "jdbc:hive://master:10000;AuthMech=0";


// EXCEPTION ---->

[Simba][HiveJDBCDriver](500151) Error setting/closing session: Server version error.
at com.cloudera.hive.hive.api.HS1Client.openSession(HS1Client.java:1090)
at com.cloudera.hive.hive.api.HS1Client.<init>(HS1Client.java:166)
at com.cloudera.hive.hive.api.HiveServer1ClientFactory.createClient(HiveServer1ClientFactory.java:61)
at com.cloudera.hive.hivecommon.core.HiveJDBCConnection.connect(HiveJDBCConnection.java:597)
at com.cloudera.hive.jdbc.common.BaseConnectionFactory.doConnect(BaseConnectionFactory.java:219)
at com.cloudera.hive.jdbc.common.AbstractDriver.connect(AbstractDriver.java:216)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)

 

 

Port 10000 is open. I can connect to hive2 server using beeline with the following bash commands:

 

beeline

!connect jdbc:hive2://localhost:10000 cloudera cloudera org.apache.hive.jdbc.HiveDriver

I have no Kerberos or LDAP enabled. Which is the correct way to establish connection to Hive with jdbc?

 

1 REPLY 1

avatar
Super Collaborator

The second error is normal. You are using the HiveServer1 driver to connect to HiveServer2, that's why it is failing.

According to your beeline command, you have HS2.

 

You need the following URL

jdbc:hive2://localhost:10000;AuthMech=0;transportMode=binary

with

Class.forName("com.cloudera.hive.jdbc4.HS2Driver");

 

UID is not required, but you can provide it. Since you don't have LDAP configured, you definitelly don't have to set password.