gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:09:20 25 4
gpt4 key购买 nike

运行以下代码需要哪些设置和 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com