A hive Thriftserver example.(hands on lab)
-----------------------------------------Create java file in eclipse with name
The necessary jar files go to $HIVE_HOME/lib and search for libraries and add to the below program .Slf4j-api-1.6.1.jar
--go to $HADOOP_HOME search for necessary libraries and add them too.
________________________
ThriftHive.java(add all the jar in the build configuration step to the program)
--------------------------
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);
System.out.println("try block");
} 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 = "test";
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));
}
}
}
----------------------------------
Go to $HIVE_HOME/bin directory
To start the thrift server please type the below command:
$HIVE_HOME/bin>hive --service hiveserver
-------------
Go to eclipse :
After adding all the Jars we need to run the Java application and we will get the output
________
For every query hard coded in the java code in the eclipse there will be one Ok displayed on output screen.
No comments:
Post a Comment