There are several ways in which Java connects to hive, and it comes in contact with two types of operations:
First, Hive is going to start the remote service Interface, command:
Hive--service hiveserver-p 50000 &
1. Connect hive through the JDBC driver
Of course, there are other ways of connecting, such as ODBC, which is very common.
Unstable, often by the large amount of data flush hanging, not recommended to use.
Packagecn.ac.iscas.hiveclient;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;Importjava.util.Properties; Public classhivejdbcclient {Private StaticString drivername; Private StaticString URL; Private StaticString User; Private StaticString password; PrivateConnection conn =NULL; PrivateStatement stmt =NULL; Static{Properties prop=NewProperties (); InputStream in= Hivejdbcclient.class. getResourceAsStream ("Hivecli.properties"); Try{prop.load (in); DriverName= Prop.getproperty ("drivername"); URL= Prop.getproperty ("url"); User= Prop.getproperty ("User"); Password= Prop.getproperty ("Password"); }Catch(IOException e) {e.printstacktrace (); } } Public Booleanexecute (String sql) {Booleanrs =false; Try{conn=Getconn (); stmt=conn.createstatement (); RS=stmt.execute (SQL); } Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{ Try{ if(NULL!=conn) {Conn.close (); Conn=NULL; } if(NULL!=stmt) {Stmt.close (); stmt=NULL; } }Catch(SQLException e) {e.printstacktrace (); } } returnrs; } PublicResultSet executeQuery (String sql) {ResultSet rs=NULL; Try{conn=Getconn (); stmt=conn.createstatement (); RS=stmt.executequery (SQL); } Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{ Try{ if(NULL!=conn) {Conn.close (); Conn=NULL; } if(NULL!=stmt) {Stmt.close (); stmt=NULL; } }Catch(SQLException e) {e.printstacktrace (); } } returnrs; } Private StaticConnection Getconn ()throwsclassnotfoundexception, sqlexception{class.forname (drivername); Connection Conn=drivermanager.getconnection (Url,user,password); returnConn; } Public Static voidMain (string[] args) {hivejdbcclient HC=Newhivejdbcclient (); ResultSet RS= Hc.executequery ("desc Carsrecord"); Try { while(Rs.next ()) {System.out.println (rs.getstring (1)); } } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
2. Connect via Hive Thrift
Packagecn.ac.iscas.hiveclient;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.util.ArrayList;Importjava.util.List;Importjava.util.Properties;Importorg.apache.hadoop.hive.service.HiveClient;Importorg.apache.hadoop.hive.service.HiveServerException;Importorg.apache.thrift.TException;ImportOrg.apache.thrift.protocol.TBinaryProtocol;ImportOrg.apache.thrift.protocol.TProtocol;ImportOrg.apache.thrift.transport.TSocket;ImportOrg.apache.thrift.transport.TTransport;Importorg.apache.thrift.transport.TTransportException; Public classhivethreatclient {StaticTtransport Transport; StaticTprotocol Protocol; Statichiveclient Client; StaticString IP; Static intPort; Static{Properties prop=NewProperties (); InputStream in= Hivejdbcclient.class. getResourceAsStream ("Hivecli.properties"); Try{prop.load (in); IP= Prop.getproperty ("IP"); Port= Integer.valueof (Prop.getproperty ("Port")); }Catch(IOException e) {e.printstacktrace (); } } Public StaticList<string> Execute (String query)throwshiveserverexception, texception,ttransportexception{List<String> result =NewArraylist<string>(); Transport=NewTsocket (Ip,port); Protocol=NewTbinaryprotocol (transport); Client=Newhiveclient (protocol); Transport.open (); Client.send_execute (query); Client.recv_execute (); //Client.send_commit_txn (RQST); //client.execute (query);list<string> list = Client.fetchn (10); while(NULL!=list && list.size () >0){ for(String r:list) {System.out.println (R); Result.add (R); } list= Client.fetchn (10); } client.shutdown (); Transport.close (); returnresult; } Public Static voidMain (string[] args) {Try { //hivethreatclient.execute ("desc Carsrecord"); //hivethreatclient.execute ("SELECT distinct addressname from Carsrecord where collectiontime= ' 2015-02-02 '"); //load Data inpath '/2015/02/2015-02-01.dat ' overwrite into table Carsrecord partition (collectiontime= ' 2015-02-01 ') for(inti = 10; I < 29; i++) {String day= i > 9? "" +i: "0" +i; String Stat= "Load Data inpath '/2015/02/2015-02-" +day+ ". Dat ' overwrite into table Carsrecord partition (collectiontime= ' 2015-02-" + day+ "')"; SYSTEM.OUT.PRINTLN (STAT); Hivethreatclient.execute (STAT); } //Hivethreatclient.execute ("select * from Carsrecord where collectiondate>= ' 2014-01-01 ' and collectiondate< ' 2014-01-03 ' ");}Catch(hiveserverexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(ttransportexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(texception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
The jar package that needs to be introduced:
- <classpathentry kind= "Lib" path= "Hive-service-0.13.1.jar"/>
<classpathentry kind= "Lib" path= "Hive-exec-0.13.1.jar"/>
<classpathentry kind= "Lib" path= "Slf4j-api-1.6.6.jar"/>
<classpathentry kind= "Lib" path= "Hive-metastore-0.13.1.jar"/>
<classpathentry kind= "Lib" path= "Libfb303-0.9.0.jar"/>
Hive Java API