Here I am explaining a sample hive jdbc client. With this we can fire hive queries from java programs. The only thing is that we need to start the hive server. By default, hive server listens at port 10000. The sample program is given below. The program is self explanatory and you can rewrite it to execute any type of hive queries. For this program you need the mysql-connector jar in the classpath.
import java.sql.SQLException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.sql.DriverManager; /* * @author * Amal G Jose * */ public class HiveJdbc private static String driver = "org.apache.hadoop.hive.jdbc.HiveDriver"; /** * @param args * @throws SQLException */ public static void main(String[] args) throws SQLException { try { Class.forName(driver); } catch (ClassNotFoundException e) { e.printStackTrace(); System.exit(1); } Connection connect = DriverManager.getConnection("jdbc:hive://:10000/default", "", ""); Statement state = connect.createStatement(); String tableName = "test"; state.executeQuery("drop table " + tableName); ResultSet res = state.executeQuery("create table " + tableName + " (key int, value string)"); // Query to show tables String show = "show tables"; System.out.println("Running: " + show); res = state.executeQuery(show); if (res.next()) { System.out.println(res.getString(1)); } // Query to describe table String describe = "describe " + tableName; System.out.println("Running: " + describe); res = state.executeQuery(describe); while (res.next()) { System.out.println(res.getString(1) + "\t" + res.getString(2)); } } }
Hi,
M getting below error. could you please help me to resolve it-
Exception in thread “main” java.lang.NoClassDefFoundError: org/apache/hadoop/hive/service/HiveInterface
at org.apache.hadoop.hive.jdbc.HiveDriver.connect(HiveDriver.java:104)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at Hive_JDBC.main(Hive_JDBC.java:21)
Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.hive.service.HiveInterface
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
… 4 more
Thanks in advance
Hi,
This is because of a missing jar. Check whether you added hive-service-*-.jar in the projects class path.
Hi,
M getting below error. could you please help me to resolve it-
java.lang.ClassNotFoundException: org.apache.hadoop.hive.jdbc.HiveDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:191)
at Jdbc.main(Jdbc.java:20)
The reason is very clear from the logs. It is because of a missing jar file. Add hive-jdbc-*.jar to the classpath.
Regards,
Amal G Jose
Hi Amal,
Same prblm here, I have added all the hive jars, but issue not.solved.
the error i am getting here is java.lang.ClassNotFoundException: org.apache.hive.jdbc.HiveDriver and i have added the hive service and also hive jdbc jars but the error is not getting resolved.please help me
Please ensure that the jars are added to the classpath properly.
When i am executing same code and jars with simple program it’s connecting properly…but when i am adding servlets to it and started debugging,it is showing me that error and after class.forname() it is directly throwing me this error
hi,
i am facing this error.
ava.sql.SQLException: Invalid URL: jdbc:hive2://server:10000/default
at org.apache.hadoop.hive.jdbc.HiveConnection.(HiveConnection.java:85)
at org.apache.hadoop.hive.jdbc.HiveDriver.connect(HiveDriver.java:104)
at org.eclipse.datatools.connectivity.drivers.jdbc.JDBCConnection.createConnection(JDBCConnection.java:328)
at org.eclipse.datatools.connectivity.DriverConnectionBase.internalCreateConnection(DriverConnectionBase.java:105)
at org.eclipse.datatools.connectivity.DriverConnectionBase.open(DriverConnectionBase.java:54)
at org.eclipse.datatools.connectivity.drivers.jdbc.JDBCConnection.open(JDBCConnection.java:96)
at org.eclipse.datatools.connectivity.drivers.jdbc.JDBCConnectionFactory.createConnection(JDBCConnectionFactory.java:53)
at org.eclipse.datatools.connectivity.internal.ConnectionFactoryProvider.createConnection(ConnectionFactoryProvider.java:83)
at org.eclipse.datatools.connectivity.internal.ConnectionProfile.createConnection(ConnectionProfile.java:359)
at org.eclipse.datatools.connectivity.ui.PingJob.createTestConnection(PingJob.java:76)
at org.eclipse.datatools.connectivity.ui.PingJob.run(PingJob.java:59)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)
Ensure that the hiveserver is running fine in your cluster.
Did anyone get this to work. i tried the same and code throws the same error. i ensured all the jar files are in the classpath