4

I followed the steps mentioned in https://luckymrwang.github.io/2018/03/14/Install-hive-on-Mac-with-Homebrew/ to install Apache Hive in Mac book using Homebrew. It installed the version of 3.1.2_3.

I used the command hive and created some tables.

I went to the beeline command line and issued the command !connect jdbc:hive2:// (I did not specify any connection URL as the documentation said there is no need to specify if the hive is installed in the same system) and entered the password as hive and hive as configured in hive-site.xml. It successfully connected to the hive. I used the command show tables and verified that I am able to see the tables.

Now I try to connect the same via Java JDBC application.

Dependencies:

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

Code:

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

However, I keep getting the error as the connection refused. I am not sure whether the connection string is correct or not. Can you please help in resolving the issue?

This is the exception trace

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

I ran hive in debug mode using the command hive --hiveconf hive.root.logger=DEBUG,console. I didn't see any exceptions there as well.

You can find the hive-site.xml here. 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

The following jars are added in the classpath as well.

enter image description here

Chandan
  • 11,465
  • 1
  • 6
  • 25
  • you are missing username and password in Driver.getConnection DriverManager.getConnection("jdbc:hive2://", username, password) – Chandan Jun 02 '21 at 10:32
  • what is the default username and password? I haven't set any. So my guess it should be "" ?Is my understanding wrong? – jagannathan rajagopalan Jun 03 '21 at 06:35
  • what you are providing during login through command line? – Chandan Jun 03 '21 at 07:17
  • according to your configuration username is `hiveuser10` and password is `password` then change `DriverManager.getConnection("jdbc:hive2://", "", "");` to `DriverManager.getConnection("jdbc:hive2://", "hiveuser10", "password")`. – Chandan Jun 03 '21 at 09:34
  • when i login with the command line. I do not provide any username and password. I just type hive and start typing the commands. I have tried with hiveuser10 and password as well. It didn't work. – jagannathan rajagopalan Jun 03 '21 at 12:10
  • check this [post](https://sparkbyexamples.com/apache-hive/hive-hiveexception-java-lang-runtimeexception-unable-to-instantiate-org-apache-hadoop-hive-ql-metadata-sessionhivemetastoreclient/) maybe it would help. – Chandan Jun 03 '21 at 13:43
  • I had done this earlier after which the operations from the command line worked. The problem I am facing is unable from jdbc program – jagannathan rajagopalan Jun 04 '21 at 05:55
  • try this https://stackoverflow.com/questions/25157273/connect-from-java-to-hive-using-jdbc – Chandan Jun 04 '21 at 14:46
  • this is the driver I am using it already – jagannathan rajagopalan Jun 04 '21 at 14:54
  • change `Class.forName("org.apache.hive.jdbc.HiveDriver");` to `Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver");` and also double check you have driver jar in the project. The compiler is not able to find `org.apache.hadoop.hive.ql.metadata.HiveException` class. replace your driver jar file – Ghayel Jun 05 '21 at 10:38
  • you are giving reference of `"org.apache.hive.jdbc.HiveDriver")` whereas it should be `org.apache.hadoop.hive.jdbc.HiveDriver` replace it and this will be fixed – Ghayel Jun 05 '21 at 10:43
  • Also change this ` DriverManager.getConnection("jdbc:hive2://", "", "");` to `jdbc:hive2://localhost:/default", "", "");` – Ghayel Jun 05 '21 at 10:47
  • I did both the changes you suggested. I have added the dependency. compile 'org.apache.hive:hive-jdbc:1.1.0' and compileOnly 'org.apache.hadoop:hadoop-core:1.2.1'. Now I get this error java.lang.ClassNotFoundException: org.apache.hadoop.hive.jdbc.HiveDriver – jagannathan rajagopalan Jun 06 '21 at 04:13

1 Answers1

-1

Your original error of org.apache.hadoop.hive.ql.metadata.HiveException class not being found, indicates that you are missing the hive-exec jar in your class path, which houses the HiveException class.

Add the hive-exec jar to your dependency list.

    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' 
Raja Nadar
  • 9,409
  • 2
  • 32
  • 41