java - Hive - 运行 Java 代码时的依赖关系

标签 java hadoop hive

运行以下代码需要哪些设置和 jar 文件。我有 jdk1.8.0_05、Hadoop 2.2.0 和 Hive 0.12.0。请帮忙。
我正在尝试在 Eclipse 上运行此代码,但它没有编译。我添加了大约 15 个外部 jar ,但没有用

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));
    }
  }
}

错误::

java.lang.ClassNotFoundException: org.apache.hadoop.hive.jdbc.HiveDriver at java.net.URLClassLoader$1.run(URLClassLoader.java:372) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.security.AccessController.doPrivileged(Native Method) 在 java.net.URLClassLoader.findClass(URLClassLoader.java:360) 在 java.lang.ClassLoader.loadClass(ClassLoader.java:424) 在 sun.misc.Launcher$AppClassLoader。 loadClass(Launcher.java:308) 在 java.lang.ClassLoader.loadClass(ClassLoader.java:357) 在 java.lang.Class.forName0(Native Method) 在 java.lang.Class.forName(Class.java:259)在 HiveJdbcClient.main(HiveJdbcClient.java:15)

最佳答案

您必须将 hive-jdbc-xxxx.jar 添加到您的类路径中。我不知道你有什么 Hadoop 版本或 Hadoop 发行版,但如果你使用的是最后一个 Cloudera 发行版,请下载 hive-0.12.0-cdh5.0.2.tar.gz 来自 this archive并包括 /hive-0.12.0-cdh.5.0.2/hive-jdbc-0.12.0-cdh5.0.2.jar 进入你的java类路径。

关于java - Hive - 运行 Java 代码时的依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24650012/

相关文章:

java - 如何使用 Apache POI 列出 Microsoft Office 文档中的所有嵌入文件?

JavaMail API 不适用于我,但有异常 com.sun.mail.util.MailConnectException : Couldn't connect to host, 端口

java - 将参数从 servlet 转发到 jsp

java - Hadoop正向调度

java - 使用 Eclipse/Maven 构建 Hadoop - 缺少 Artifact jdk.tools :jdk. 工具 :jar:1. 6

hadoop - 无法通过Map Reduce Java程序访问Hadoop HDFS文件系统

java - 为什么方法不是 toLowerCase();在我的代码中工作?

oracle - 使用 sqoop 从 Oracle 到 hive 的日期导入问题

python - Python 2.6.6和 hive 连接问题?

hadoop - 如何在 Hive Web 界面中编写查询