Reputation:
I am facing this exception while connecting to beeline, hive2 version 1.2.1000.2.5.0.0, I have added hive-jdbc.jar file into classpath on my Windows 10 machine.
Exception:
java.lang.ClassNotFoundException: org.apache.hadoop.hive.jdbc.HiveDriver
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at HiveJdbcClient.main(HiveJdbcClient.java:17)
HiveJdbcClient.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);
}
//replace "hive" here with the name of the user the queries should run as
Connection con = DriverManager.getConnection("jdbc:hive://localhost:10003/default", "", "");
Statement stmt = con.createStatement();
String tableName = "testHiveDriverTable";
stmt.execute("drop table if exists " + tableName);
stmt.execute("create table " + tableName + " (key int, value string)");
// show tables
String sql = "show tables '" + tableName + "'";
System.out.println("Running: " + sql);
ResultSet res = stmt.executeQuery(sql);
if (res.next()) {
System.out.println(res.getString(1));
}
}
}
Thanks for the answer, I have already tried this,
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
Thanks in advance!!
Upvotes: 1
Views: 4098
Reputation:
I tried my best on Windows couldn't run it successfully. So, I switched to centos, this should also help Windows users, do comment for any questions :)
It’s hard to find all the jars to 1.2.1000.2.5.0.0, so I tried 1.2.1 version and it worked on our nn2 server. I have wasted lot of time on Windows, Linux has a very good documentation.
HiveJdbcClient.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.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);
}
//replace "hive" here with the name of the user the queries should run as
Connection con = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "", "");
Statement stmt = con.createStatement();
String tableName = "testHiveDriverTable";
stmt.execute("drop table if exists " + tableName);
stmt.execute("create table " + tableName + " (key int, value string)");
// show tables
String sql = "show tables '" + tableName + "'";
System.out.println("Running: " + sql);
ResultSet res = stmt.executeQuery(sql);
if (res.next()) {
System.out.println(res.getString(1));
}
}
}
Above is the simple program which I tried on our nn2, before you run any of those files, please add all these jars into the classpath of any machine which you are working on.
I hope I am not missing some jar files, this should work for you with no errors:
If you just want to test the connection without adding into the classpath you can do it using this command on Linux:
Compile:
[root@centosserver ~]# javac -cp .:hive-jdbc-1.2.1.jar:hive-service-1.2.1.jar:hive-exec-1.2.1.jar:hive-metastore-1.2.1.jar:hive-shims-1.2.1.jar:hive-beeline-1.2.1:hive-serde-1.2.1:hive-common-1.2.1:httpclient-4.2.5.jar:httpcore-4.2.1.jar:httpcore-4.3-alpha1.jar:httpclient-4.0-alpha4.jar:httpclient-4.3.4.jar:commons-logging-1.2.jar:hadoop-common-2.2.0.jar:slf4j-api-1.7.21.jar HiveJdbcClient.java
Execute:
[root@centosserver ~]# java -cp .:hive-jdbc-1.2.1.jar:hive-service-1.2.1.jar:hive-exec-1.2.1.jar:hive-metastore-1.2.1.jar:hive-shims-1.2.1.jar:hive-beeline-1.2.1:hive-serde-1.2.1:hive-common-1.2.1:httpclient-4.2.5.jar:httpcore-4.2.1.jar:httpcore-4.3-alpha1.jar:httpclient-4.0-alpha4.jar:httpclient-4.3.4.jar:commons-logging-1.2.jar:hadoop-common-2.2.0.jar:slf4j-api-1.7.21.jar HiveJdbcClient
Basic command is
java -cp .:your-Jar-File1:your-Jar-File2:your-Jar-File3 yourMainClass.Java
Upvotes: 0
Reputation: 2319
if you use the most recent jar (e.g version 2.1x) you should try this
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
instead of
private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
for in recent JDBC jar there is no org.apache.hadoop.hive.jdbc.HiveDriver
Upvotes: 1