Created 12-28-2015 03:33 PM
i want execute the below program in hortonworks some one help me
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
public class HiveJdbcClient {
private staticString 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));
}
}
}
Created 12-28-2015 05:01 PM
You would need the following jars in your classpath:
/usr/hdp/current/hive-client/lib/hive-jdbc.jar
/usr/hdp/current/hadoop-client/hadoop-common.jar
/usr/hdp/current/hadoop-client/hadoop-auth.jar
With these in the classpath you can compile and execute your java client.
Created 12-28-2015 05:01 PM
You would need the following jars in your classpath:
/usr/hdp/current/hive-client/lib/hive-jdbc.jar
/usr/hdp/current/hadoop-client/hadoop-common.jar
/usr/hdp/current/hadoop-client/hadoop-auth.jar
With these in the classpath you can compile and execute your java client.
Created 12-29-2015 04:26 AM
i already have that jar file in my hortonworks,i want know how to compile and execute the file on that path
Created 12-29-2015 06:09 AM
To compile:
$JAVA_HOME/bin/java HiveJdbcClient.java
To execute:
$JAVA_HOME/bin/java -cp .:/usr/hdp/current/hive-client/lib/hive-jdbc.jar:/usr/hdp/current/hadoop-client/hadoop-common.jar:/usr/hdp/current/hadoop-client/hadoop-auth.jar HiveJdbcClient
Created 02-03-2016 02:42 AM
@sivasaravanakumar k has this been resolved? Can you post your solution or accept the best answer?
Created 05-18-2016 12:35 PM
@sivasaravanakumar k If you are using any IDE to develop program like Eclipse then you can Add all required JAR file to program path and then build JAR using Eclipse. (http://tutoringcenter.cs.usfca.edu/resources/adding-user-libraries-in-eclipse.html)
If you want to run your program on remote job where these jar might not be on classpath then build FAT/Executable jar using Eclipse. FAT JAR will ensure that all the required dependency is packaged with the program. (http://stackoverflow.com/questions/502960/eclipse-how-to-build-an-executable-jar-with-external-jar)
You can also use build-tool like Maven to handle dependency for you.