Using the Java API to manipulate hive is the third external interface to test hive
Test Hive Service Startup
1 Packageorg.admln.hive;2 3 Importjava.sql.SQLException;4 Importjava.sql.Connection;5 ImportJava.sql.ResultSet;6 Importjava.sql.Statement;7 ImportJava.sql.DriverManager;8 9 Public classtesthive {Ten One Private StaticString drivername = A"Org.apache.hadoop.hive.jdbc.HiveDriver"; - - Public Static voidMain (string[] args) the throwsSQLException { - Try { - Class.forName (drivername); -}Catch(ClassNotFoundException e) { + e.printstacktrace (); -System.exit (1); + } A atConnection con =Drivermanager.getconnection ( -"Jdbc:hive://192.168.126.133:10000/default", "Hive", "Hadoop"); -Statement stmt =con.createstatement (); -String tableName = "Hellohive"; -Stmt.execute ("drop table if exists" +tableName); -Stmt.execute ("CREATE TABLE" + TableName + in"(Key int, value string)"); -SYSTEM.OUT.PRINTLN ("Create table success!"); to //Show Tables +String sql = "Show tables '" + tableName + "'"; -System.out.println ("Running:" +sql); theResultSet res =stmt.executequery (SQL); * if(Res.next ()) { $System.out.println (res.getstring (1));Panax Notoginseng } - the //Describe table +sql = "Describe" +TableName; ASystem.out.println ("Running:" +sql); theres =stmt.executequery (SQL); + while(Res.next ()) { -System.out.println (res.getstring (1) + "T" + res.getstring (2)); $ } $ - -sql = "SELECT * from" +TableName; theres =stmt.executequery (SQL); - while(Res.next ()) {WuyiSystem.out.println (String.valueof (Res.getint (1)) + "\ T" the+ res.getstring (2)); - } Wu -sql = "SELECT COUNT (1) from" +TableName; AboutSystem.out.println ("Running:" +sql); $res =stmt.executequery (SQL); - while(Res.next ()) { -System.out.println (res.getstring (1)); - } A } +}
Results:
The following two parameters in the JDBC link should not be user name and password, I am silly in the sky, but test no matter what to fill in or do not fill in can link success
Why is it that hive can be linked by default?
Jar Package Used
1 hadoop-2.2.0/share/hadoop/common/hadoop-common-2.2.0. Jar2 $HIVE _home/lib/ hive-exec-0.11.03 $HIVE _home/lib/hive-jdbc-0.11.04 $HIVE _home/lib/ hive-metastore-0.11.0. Jar 5 $HIVE _home/lib/hive-service-0.11.0. Jar 6 $HIVE _home/lib/libfb303-0.9.0. Jar 7 $HIVE _home/lib/commons-logging-1.0.4. Jar 8 $HIVE _home/lib/slf4j-api-1.6.1.jar
The code is excerpt.
http://www.iteblog.com/archives/846
Hive-with Java API operation