Support Questions

Find answers, ask questions, and share your expertise

how to execute hive program or hive jar file in hortonworks

avatar
Expert Contributor

i want execute the below program in hortonworks some one help me

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

public class HiveJdbcClient {
  private staticString driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
  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);
    }
    Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000/default", "", "");
    Statement stmt = con.createStatement();
    String tableName = "testHiveDriverTable";
    stmt.executeQuery("drop table " + tableName);
    ResultSet res = stmt.executeQuery("create table "+ tableName + " (key int, value string)");
    // show tables
    String sql = "show tables '"+ tableName + "'";
    System.out.println("Running: " + sql);
    res = stmt.executeQuery(sql);
    if(res.next()) {
      System.out.println(res.getString(1));
    }
    // describe table
    sql = "describe " + tableName;
    System.out.println("Running: " + sql);
    res = stmt.executeQuery(sql);
    while (res.next()) {
      System.out.println(res.getString(1) + "\t" + res.getString(2));
    }
    // load data into table
    // NOTE: filepath has to be local to the hive server
    // NOTE: /tmp/a.txt is a ctrl-A separated file with two fields per line
    String filepath = "/tmp/a.txt";sql = "load data local inpath '" + filepath + "' into table " + tableName;
    System.out.println("Running: "+ sql);
    res = stmt.executeQuery(sql);
    // select * query sql = "select * from " + tableName;
    System.out.println("Running: " + sql);
    res = stmt.executeQuery(sql);
    while (res.next()) {
      System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2));
    }
    // regular hive query
    sql = "select count(1) from " + tableName;
    System.out.println("Running: " + sql);
    res = stmt.executeQuery(sql);
    while (res.next()) {
      System.out.println(res.getString(1));
    }
  }
}
1 ACCEPTED SOLUTION

avatar

You would need the following jars in your classpath:

/usr/hdp/current/hive-client/lib/hive-jdbc.jar

/usr/hdp/current/hadoop-client/hadoop-common.jar

/usr/hdp/current/hadoop-client/hadoop-auth.jar

With these in the classpath you can compile and execute your java client.

View solution in original post

5 REPLIES 5

avatar

You would need the following jars in your classpath:

/usr/hdp/current/hive-client/lib/hive-jdbc.jar

/usr/hdp/current/hadoop-client/hadoop-common.jar

/usr/hdp/current/hadoop-client/hadoop-auth.jar

With these in the classpath you can compile and execute your java client.

avatar
Expert Contributor

i already have that jar file in my hortonworks,i want know how to compile and execute the file on that path

avatar

To compile:

$JAVA_HOME/bin/java HiveJdbcClient.java

To execute:

$JAVA_HOME/bin/java -cp .:/usr/hdp/current/hive-client/lib/hive-jdbc.jar:/usr/hdp/current/hadoop-client/hadoop-common.jar:/usr/hdp/current/hadoop-client/hadoop-auth.jar HiveJdbcClient

avatar
Master Mentor

@sivasaravanakumar k has this been resolved? Can you post your solution or accept the best answer?

avatar
Super Collaborator

@sivasaravanakumar k If you are using any IDE to develop program like Eclipse then you can Add all required JAR file to program path and then build JAR using Eclipse. (http://tutoringcenter.cs.usfca.edu/resources/adding-user-libraries-in-eclipse.html)

If you want to run your program on remote job where these jar might not be on classpath then build FAT/Executable jar using Eclipse. FAT JAR will ensure that all the required dependency is packaged with the program. (http://stackoverflow.com/questions/502960/eclipse-how-to-build-an-executable-jar-with-external-jar)

You can also use build-tool like Maven to handle dependency for you.