Community Articles

Find and share helpful community-sourced technical articles.
Labels (1)
avatar
Rising Star

Steps to connect from a client machine (MAC in this case) to Hadoop cluster using hive JDBC.

Here are couple of links that i have used to build this out Link that talks about hive drivers and jar files

https://streever.atlassian.net/wiki/pages/viewpage.action?pageId=4390924

Link that talks about how to setup java jdbc setup for hive jar files https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Clients

1.Here are the jar files you need to connect to HS2 For HDP 2.3

You'll only need 2 jar files for the JDBC Client

with HDP 2.3 # From

/usr/hdp/current/hive-client hive-jdbc.jar (should be a symlink to the hive-jdbc-xxx-standalone.jar)

# From /usr/hdp/current/hadoop-client hadoop-common.jar (hadoop-common-....jar)

2. Make sure that java home is set on your machine

This is value that i have on my machine for JAVA_HOME

echo $JAVA_HOME

/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home

3. Move these jar files to java library directory on my machine

/Library/Java/Extensions

4. Set the java classpath for the hive jar file

export CLASSPATH=$CLASSPATH:/Library/Java/Extensions/hive-jdbc.jar

5. Use any java based IDE (I used eclipse) to write a simple java class to connect to hiver server2.

Where the jdbc string is mentioned, you have to specify the hive server that you are using and corresponding userid and password as well

Here is the code for that

package hive_test;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
public class HiveJdbcClientv1 {
  private static String driverName = "org.apache.hive.jdbc.HiveDriver";
  /**
   * @param args
   * @throws SQLException
   */
  public static void main(String[] args) throws SQLException {
      try {
      Class.forName(driverName);
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      System.exit(1);
    }
    //replace "hive" here with the name of the user the queries should run as
    Connection con = DriverManager.getConnection("jdbc:hive2://172.16.149.158: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)");
    // show tables
    // String sql = "show tables '" + tableName + "'";
    String sql = ("show tables");
    ResultSet res = stmt.executeQuery(sql);
    if (res.next()) {
        System.out.println(res.getString(1));
      }
  }
}
78,190 Views
Comments
avatar
Contributor

Hi @Chakra,

After adding all the jars mentioned by you, to build path and executing the program getting error as follows :

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/thrift/transport/TTransportException at org.apache.hive.jdbc.HiveDriver.connect(HiveDriver.java:105) at java.sql.DriverManager.getConnection(DriverManager.java:664) at java.sql.DriverManager.getConnection(DriverManager.java:247) at edu.thenet.pc1.connect.hive.HiveJdbcConnector.main(HiveJdbcConnector.java:27) Caused by: java.lang.ClassNotFoundException: org.apache.thrift.transport.TTransportException at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 4 more

Can you please help me to identify which other jars are also required ?

Thank you,

Subrata

avatar
New Contributor

Hello Chakra!!

What jar files need to be added for windows machine or Linux?

I am on HDP 2.5, Hive, Beeline 1.2

Please see my question here on SOF,

Please let me know. Thanks in advance!!