0

I have a hive database named handson and I am trying to show the records from handson.EMP_DATA table using java code. I am using the hive-1.2.1 version. When I try to complete and run my code from the home directory it throws java.lang.ClassNotFoundException: org.apache.hive.jdbc.HiveDriver. I have included my java code, hive-site.xml and the error occurred below:

This is my java code

import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;

public class readEmpHive {
    
    private static String driverName = "org.apache.hive.jdbc.HiveDriver";
    //private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver"; <--- I have also tried running this

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
    
    // Register driver and create driver instance
        Class.forName(driverName);
    // get connection
        System.out.println("working?");
        Connection con = DriverManager.getConnection("jdbc:hive2://localhost:10000/handson", "root", "root");

   /*
        I have also tried this
        Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000/handson", "root", "root");
   */

        System.out.println("working!!!");
    // create statement
        Statement stmt = con.createStatement();
    // execute statement
        String query = "SELECT * FROM EMP_DATA";
        ResultSet res = stmt.executeQuery(query);
        System.out.println(" Empno \t EName \t Job \t Salary \t Deptno ");
        while(res.next()){
            int empno = res.getInt(1);
            String ename = res.getString(2);
            String job = res.getString(3);
            int sal = res.getInt(4);
            int deptno = res.getInt(5);
            System.out.println(empno+", "+ename+", "+job+", "+sal+", "+deptno);
        }
        con.close();
    }
}

This is my hive-site.xml file

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>

<configuration>

    <property>
        <name>hive.metastore.uris</name>
        <value>thrift://localhost:9083</value>
        <description>IP address (or fully-qualified domain name) and port of the metastore host</description>
    </property>
    
    <property>
        <name>javax.jdo.option.ConnectionURL</name>
        <value>jdbc:mysql://localhost/hivemetadb?createDatabaseIfNotExist=true</value>
        <description>the URL of the MySQL database</description>
    </property>

    <property>
        <name>javax.jdo.option.ConnectionDriverName</name>
        <value>com.mysql.jdbc.Driver</value>
    </property>

    <property>
        <name>javax.jdo.option.ConnectionUserName</name>
        <value>root</value>
    </property>

    <property>
        <name>javax.jdo.option.ConnectionPassword</name>
        <value>root</value>
    </property>
    
    <property>
      <name>hive.hwi.listen.host</name>
      <value>0.0.0.0</value>
    </property>

    <property>
      <name>hive.hwi.listen.port</name>
      <value>9999</value>
    </property>

    <property>
      <name>hive.hwi.war.file</name>
      <value>lib/hive-hwi-1.2.1.war</value>
    </property>


    <property>
      <name>hive.metastore.local</name>
      <value>true</value>
    </property>

    <property>
      <name>hive.metastore.uris</name>
      <value>thrift://localhost:9083</value>
      <description>IP address (or fully-qualified domain name) and port of the metastore host</description>
    </property>

</configuration>

When I compile and run my code, I got an error

hduser@hadoop:~$ javac readEmpHive.java
hduser@hadoop:~$ java readEmpHive
Exception in thread "main" java.lang.ClassNotFoundException: org.apache.hive.jdbc.HiveDriver
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(Clas
Arud
  • 131
  • 8
  • This post should help https://stackoverflow.com/questions/28760141/connect-hive-through-java-jdbc – maruti060385 Jun 03 '21 at 15:34
  • This is not helpful, because these result were need to be implemented on pom.xml. In my case I have plain java code in a file, which I need to compile and run in my terminal. – Arud Jun 03 '21 at 16:00
  • I am using local machine for this. – Arud Jun 03 '21 at 16:10

0 Answers0