1

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

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

public class HiveJdbcClient {
  private static String driverName = "org.apache.hadoop.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);
    }
    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));
    }
  }
}

Then I added all the required jars to the java build path inside eclipse. 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.

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.sql.SQLException: Could not establish connection to localhost:10000/default: java.net.ConnectException: Connection refused
    at org.apache.hadoop.hive.jdbc.HiveConnection.<init>(HiveConnection.java:116)
    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 HiveJdbcClient.main(HiveJdbcClient.java:22)

Does anyone know what I am missing here?

Ramtin
  • 135
  • 1
  • 2
  • 9
  • You need to start the Hive Thrift server. Can you check if it is started? – Venkat Mar 14 '14 at 20:58
  • How can I check if it is started? – Ramtin Mar 14 '14 at 21:29
  • If you did not start it then its not started. Check documentation : http://my.safaribooksonline.com/book/databases/hadoop/9781449326944/16dot-hive-thrift-service/_starting_the_thrift_server_html – Venkat Mar 15 '14 at 00:16
  • Thanks. I started the thrift server and port 10000 is now listening. Now I have another error message in Eclipse saying: Exception in thread "main" java.sql.SQLException: org.apache.thrift.transport.TTransportException: java.net.SocketException: Connection reset – Ramtin Mar 15 '14 at 17:50
  • check this http://stackoverflow.com/questions/10828562/java-sql-sqlexception-org-apache-thrift-transport-ttransportexception-in-hive/10845065#10845065 – user3782579 Jul 10 '14 at 11:21

1 Answers1

0

I added log4j-1.2.15.jar to libraries and the problem was resolved.

Ramtin
  • 135
  • 1
  • 2
  • 9