I am trying to connect to hive from java but getting error. I searched in google but not got any helpfull solution. I have added all jars also.
The code is:-
package mypackage;
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 driver = "org.apache.hadoop.hive.jdbc.HiveDriver";
public static void main(String[] args) throws SQLException,
ClassNotFoundException {
Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver");
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
Connection connect = DriverManager.getConnection("jdbc:hive://master:10000 /default", "", "");
Statement state = connect.createStatement();
String tableName = "mytable";
state.executeQuery("drop table " + tableName);
ResultSet res=state.executeQuery("ADD JAR /home/hadoop_home/hive/lib /hive-serdes-1.0-SNAPSHOT.jar");
res = state.executeQuery("create table tweets (id BIGINT,created_at STRING,source STRING,favorited BOOLEAN,retweet_count INT,retweeted_status STRUCT<text:STRING,user:STRUCT<screen_name:STRING,name:STRING>>,entities STRUCT<urls:ARRAY<STRUCT<expanded_url:STRING>>,user_mentions:ARRAY<STRUCT<screen_name:STRING,name:STRING>>,hashtags:ARRAY<STRUCT<text:STRING>>>,text STRING,user STRUCT<screen_name:STRING,name:STRING,friends_count:INT,followers_count:INT,statuses_count:INT,verified:BOOLEAN,utc_offset:INT,time_zone:STRING>,in_reply_to_screen_name STRING) ROW FORMAT SERDE 'com.cloudera.hive.serde.JSONSerDe' LOCATION '/user/flume/tweets'");
String show = "show tables";
System.out.println("Running show");
res = state.executeQuery(show);
if (res.next()) {
System.out.println(res.getString(1));
}
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));
}
}
}
I am getting these errors:-
run: SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/home/hadoop/hive/lib/slf4j-log4j12-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/home/hadoop/lib/slf4j-log4j12-1.4.3.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/home/GlassFish_Server/glassfish/modules/weld-osgi-bundle.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/io/Writable at org.apache.hadoop.hive.jdbc.HiveStatement.executeQuery(HiveStatement.java:198) at org.apache.hadoop.hive.jdbc.HiveStatement.execute(HiveStatement.java:132) at org.apache.hadoop.hive.jdbc.HiveConnection.configureConnection(HiveConnection.java:133) at org.apache.hadoop.hive.jdbc.HiveConnection.(HiveConnection.java:122) at org.apache.hadoop.hive.jdbc.HiveDriver.connect(HiveDriver.java:106) at java.sql.DriverManager.getConnection(DriverManager.java:571) at java.sql.DriverManager.getConnection(DriverManager.java:215) at dp.HiveJdbcClient.main(HiveJdbcClient.java:35) Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.io.Writable 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) ... 8 more Java Result: 1