① new MAVEN project, load dependent packages
Add in Pom.xml
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systempath>${java_home}/Lib/tools.jar</systempath>
</dependency>
<dependency>
<groupId>JUnit</groupId>
<artifactId>JUnit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactid>hive-exec</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactid>hive-jdbc</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>Hadoop-common</artifactid>
<version>2.6.1</version>
</dependency>
② Start the Hive service, start the cluster
(hive1.2.1 version needs to be started with Hiveserver2)
Hive–-service hiveserver2–-hiveconf hive.server2.thrift.port=11111 (Turn on service and set port number)
③ Configuration Core-xite.xml
<property>
<name>hadoop.proxyuser.neworigin.groups</name>
<value>*</value>
<description>allow the Superuser Oozie to impersonate all members of the group Group1 and group2</description>
</property>
<property>
<name>hadoop.proxyuser.neworigin.hosts</name>
<value>*</value>
<description>the Superuser can connect only from Host1 and Host2 to impersonate a user</description>
</property>
④ writing Java code
PackageCom.neworigin.HiveTest1;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.SQLException;Importjava.sql.Statement; Public classJdbcutil {StaticString drivername= "Org.apache.hive.jdbc.HiveDriver";StaticString url= "Jdbc:hive2://s100:11111/myhive";StaticString user= "Neworigin";StaticString pass= "123";//Create a connection Public StaticConnection Getconn ()throwsexception{Class.forName (drivername); Connection Conn=drivermanager.getconnection (Url,user,pass); returnConn;}//Create command Public StaticStatement getstmt (Connection conn)throwssqlexception{returnconn.createstatement ();} Public voidClosefunc (Connection conn,statement stmt)throwssqlexception{stmt.close (); Conn.close (); }} PackageCom.neworigin.HiveTest1;Importjava.sql.Connection;ImportJava.sql.ResultSet;ImportJava.sql.ResultSetMetaData;Importjava.sql.Statement; Public classJdbctest { Public Static voidMain (string[] args)throwsException {Connection conn= Jdbcutil.getconn ();//Create a connectionStatement STMT=JDBCUTIL.GETSTMT (conn);//To create an execution objectString sql= "SELECT * from Myhive.employee";//Execute SQL statementString sql2= "CREATE TABLE Jdbctest (ID int,name string)"; ResultSet Set= Stmt.executequery (SQL);//returns the result set of the executionResultSetMetaData meta = set.getmetadata ();//get field while(Set.next ()) { for(intI=1;i<=meta.getcolumncount (); i++) {System.out.print (set.getstring (i)+" "); } System.out.println (); } System.out.println ("The first SQL statement finishes executing"); Booleanb =Stmt.execute (SQL2); if(b) {System.out.println (Success); } }}
JDBC Usage for hive