"Hive" JDBC operation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.