1 Packagecom.java.hadoop.hive;2 3 Importjava.sql.Connection;4 ImportJava.sql.DriverManager;5 Importjava.sql.PreparedStatement;6 ImportJava.sql.ResultSet;7 Importjava.sql.SQLException;8 Importjava.sql.Statement;9 Ten Importorg.apache.hadoop.hive.metastore.api.ThriftHiveMetastore.Processor.drop_database; One ImportOrg.junit.Before; A Importorg.junit.Test; - - Public classtesthive { the PrivateConnection Connection; - PrivatePreparedStatement PS; - PrivateResultSet rs; - //Create a connection + @Before - Public voidgetconnection () { + Try { A atClass.forName ("Org.apache.hive.jdbc.HiveDriver"); -Connection = Drivermanager.getconnection ("Jdbc:hive2://192.168.18.130:10000/", "root", "root"); - System.out.println (connection); -}Catch(ClassNotFoundException e) { - e.printstacktrace (); -}Catch(SQLException e) { in e.printstacktrace (); - } to } + //Close Connection - Public voidClose () { the Try { * if(rs! =NULL) { $ rs.close ();Panax Notoginseng } - if(PS! =NULL) { the ps.close (); + } A if(Connection! =NULL) { the connection.close (); + } -}Catch(SQLException e) { $ e.printstacktrace (); $ } - } - the //Create a table - @TestWuyi Public voidcreatetable () { theString sql = "CREATE TABLE Goods2 (ID int,name string) row format delimited fields terminated by ' \ t '"; - Try { WuPS =connection.preparestatement (SQL); - ps.execute (SQL); About close (); $}Catch(SQLException e) { - e.printstacktrace (); - } - } A //Delete a table + @Test the Public voiddroptable () { -String sql = "DROP table goods"; $ Try { thePS =connection.preparestatement (SQL); the Ps.execute (); the close (); the}Catch(SQLException e) { - e.printstacktrace (); in } the } the //Add Data About @Test the Public voidInsert ()throwssqlexception{ theString sql = "Load data inpath '/goods.txt ' into table goods"; the //Remember to upload goods.txt in the file system first +PS =connection.preparestatement (SQL); - Ps.execute (); the close ();Bayi } the //Enquiry the @Test - Public voidFind ()throwsSQLException { -String sql = "SELECT * FROM Goods"; thePS =connection.preparestatement (SQL); thers =ps.executequery (); the while(Rs.next ()) { theSystem.out.println (Rs.getobject (1) + "---" + rs.getobject (2)); - } the close (); the } the 94 the}
1String sql= "Show tables; SELECT * FROM TEST_TB limit 10 ";2list<string> Command =NewArraylist<string>();3 4Command.add ("Hive");5Command.add ("-E");6 command.add (SQL);7 8list<string> results =NewArraylist<string>();9Processbuilder Hiveprocessbuilder =Newprocessbuilder (command);TenHiveprocess =Hiveprocessbuilder.start (); One ABufferedReader br =NewBufferedReader (NewInputStreamReader ( - Hiveprocess.getinputstream ())); -String data =NULL; the while(data = Br.readline ())! =NULL) { - results.add (data); -}
"Hive" JDBC operation