无法通过eclipse中的java代码从配置单元获取数据

无法通过eclipse中的java代码从配置单元获取数据,java,eclipse,hadoop,jdbc,hive,Java,Eclipse,Hadoop,Jdbc,Hive,我使用的是Ubuntu 14.04、hadoop-1.2.1()、hive-1.1.0()、EclipseIDE 我已经创建了一个java项目,并将所有外部jar文件导入到 /usr/local/hive/lib/ /usr/local/hadoop/hadoop-core-1.2.1.jar 我的java代码如下所示: import java.sql.SQLException; import java.sql.Connection; import java.sql.ResultSet; imp

我使用的是Ubuntu 14.04、hadoop-1.2.1()、hive-1.1.0()、EclipseIDE

我已经创建了一个java项目,并将所有外部jar文件导入到

/usr/local/hive/lib/

/usr/local/hadoop/hadoop-core-1.2.1.jar

我的java代码如下所示:

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";
 
  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));
    }
  }
}
在eclipse中运行此程序时,我遇到以下错误:

java.lang.ClassNotFoundException: org.apache.hadoop.hive.jdbc.HiveDriver位于 java.net.URLClassLoader$1.run(URLClassLoader.java:366)位于 java.net.URLClassLoader$1.run(URLClassLoader.java:355)在 java.security.AccessController.doPrivileged(本机方法)位于 java.net.URLClassLoader.findClass(URLClassLoader.java:354)位于 loadClass(ClassLoader.java:425)位于 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)位于 loadClass(ClassLoader.java:358)位于 java.lang.Class.forName0(本机方法)位于 java.lang.Class.forName(Class.java:191)位于 HiveJdbcClient.main(HiveJdbcClient.java:15)


如何为hive加载jdbc驱动程序?

您确定在
/usr/local/hive/lib
目录中有
hive-jdbc-0.14.0.jar
(或hadoop发行版的正确版本),并且在Eclipse中声明了类路径中的jar吗?类路径可以在“运行配置…”中的“类路径”选项卡中专门设置@AbbéRésina我从下载了hive