【发布时间】:2018-04-06 11:45:02
【问题描述】:
我想使用 JDBC 在 Hive 中创建表。这是我试过的代码,
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
public class HiveClient {
private static String driverName = "com.mysql.jdbc.Driver";
public static void main(String[] args) {
Connection con=null;
// Register driver and create driver instance
try {
Class.forName(driverName);
// get connection
con = DriverManager.getConnection("jdbc:mysql://sandbox-hdp.hortonworks.com/hive?createDatabaseIfNotExist=true/userdb", "root", "dc123");
// create statement
Statement stmt = con.createStatement();
// execute statement
stmt.executeQuery("CREATE TABLE IF NOT EXISTS "
+" employee ( eid int, name String, "
+" salary String, destignation String)"
+" COMMENT ‘Employee details’"
+" ROW FORMAT DELIMITED"
+" FIELDS TERMINATED BY ‘\t’"
+" LINES TERMINATED BY ‘\n’"
+" STORED AS TEXTFILE;");
System.out.println(" Table employee created.");
con.close();
}catch(Exception e){
System.out.println(e);
}
}
}
此程序显示异常:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver.
请帮我解决这个问题。
【问题讨论】:
-
我认为你必须使用:
private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver"; -
@hisi 但是我应该添加任何外部罐子吗?它仍然显示类未找到异常。
-
您必须在代码中将
com.mysql.jdbc.Driver替换为org.apache.hadoop.hive.jdbc.HiveDriver
标签: java jdbc hive ambari hortonworks-sandbox