Unable to establish a JDBC connection to Hive from Eclipse

27,517

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.

Share:
27,517
Ramtin
Author by

Ramtin

I am a Big Data Lab Administrator.

Updated on July 20, 2022

Comments

  • Ramtin
    Ramtin almost 2 years

    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!?

  • Ramtin
    Ramtin about 10 years
    Thanks for the answer. But it shouldn't be it. I tried the first option and I get Exception in thread "main" java.sql.SQLException: Invalid URL: jdbc:hive2://localhost:10000/default With option 2 the error still persists!
  • SachinJose
    SachinJose about 10 years
    Make sure you added only the following jars in your classpath during option 1. Don't add any other jars from hive library. hive-jdbc-0.10.0-cdh4.4.0.jar commons-logging-1.0.4.jar hive-service-0.10.0-cdh4.4.0.jar libfb303-0.9.0.jar libthrift-0.9.0-cdh4-1.jar slf4j-api-1.6.4.jar slf4j-log4j12-1.6.1.jar log4j-1.2.16.jar
  • Ramtin
    Ramtin about 10 years
    I removed other jars. In addition to the jars you mentioned, I had to keep hive-metastore-0.10.0.jar. I still get the initial error message.
  • SachinJose
    SachinJose about 10 years
    Can you try the second option ?
  • SachinJose
    SachinJose about 10 years
    Have you enabled security (kerberos) in hiveserver2 ?
  • Ramtin
    Ramtin about 10 years
    kerberos are not enabled. I also tried the second option. But I get the same error!
  • SachinJose
    SachinJose about 10 years
    Try connecting through beeline as mentioned in the modified, If that is not working, there is some problem with your server
  • Ramtin
    Ramtin about 10 years
    I tried connecting through beeline. I updated my question above with the error.
  • Ramtin
    Ramtin about 10 years
    Thanks Sach. I realized that my hiveserver2 was not started.
  • Shankar
    Shankar almost 9 years
    @h4ck3r : if my hive server2 is security enabled, how do i connect to hive server in standalone mode or secured hadoop-yarn cluster mode? do i have to pass user, pass in the connection details?