1. Start Hiveserver2
Nohup/home/hadoop/hive-1.1.0-cdh5.5.2/bin/hiveserver2 >>/home/hadoop/gtq_dir/logs/hiveserver.log 2> &1 &
2. The code is as follows:
Packagecn.hive;ImportJava.sql.*;/*** Created by Jieyue on 2017/12/18.*/ Public classHivejdbctest {Private StaticString drivename = "Org.apache.hive.jdbc.HiveDriver"; Private StaticConnection con =NULL; Private StaticStatement stmt =NULL; Private StaticResultSet res =NULL; Public Static voidMain (string[] args)throwsSQLException {Try{class.forname (drivename); Con= Drivermanager.getconnection ("Jdbc:hive2://172.17.101.12:10000/demo", "Root", "" "); stmt=con.createstatement (); String TableName= "T_h_jdbc"; Stmt.execute ("DROP TABLE IF EXISTS" +tableName); Stmt.execute ("CREATE TABLE" + TableName + "(key int, value string)"); System.out.println ("CREATE TABLE success!!"); String SQL= "Show CREATE TABLE" +TableName; System.out.println ("Running:" +SQL); Res=stmt.executequery (SQL); if(Res.next ()) System.out.println (res.getstring (1)); SQL= "desc" +TableName; System.out.println ("Running:" +SQL); Res=stmt.executequery (SQL); while(Res.next ()) System.out.println (res.getstring (1) + "T" + res.getstring (2)); SQL= "SELECT * from" +TableName; System.out.println ("Running:" +SQL); Res=stmt.executequery (SQL); while(Res.next ()) System.out.println (String.valueof (Res.getint (1)) + "\ T" + res.getstring (2)); SQL= "SELECT count (*) from" +TableName; Res=stmt.executequery (SQL); System.out.println ("Running:" +SQL); while(Res.next ()) System.out.println (res.getstring (1)); } Catch(Exception e) {e.printstacktrace (); System.exit (1); } finally { if(Res! =NULL) Res.close (); if(stmt! =NULL) Stmt.close (); if(Con! =NULL) Con.close (); } }}
The 3.pom file is as follows
<?xml version= "1.0" encoding= "UTF-8"? ><project xmlns= "http://maven.apache.org/POM/4.0.0"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion>4.0.0 </modelVersion> <groupId>mm</groupId> <artifactId>MyDemoApp</artifactId> <versio N>1.0-snapshot</version> <properties> <project.build.sourceencoding>utf-8</project.build .sourceencoding> /* .java</exclude> </excludes> <filtering>true</filtering> & lt;/resource> </resources> <plugins> <plugin> <groupId> Org.apache.maven.plugins</groupid> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</s ource> <target>1.7</target> </configuration> </plugin > <plugin> <groupId>org.apache.maven.plugins</groupId> <a Rtifactid>maven-shade-plugin</artifactid> <version>2.3</version> <co nfiguration> </configuration> <executions> <EXECUTION&G T <phase>package</phase> <goals> <goal>shade< /goal> </goals> </execution> </executions> </plugin> </plugins> </build></project>
Hive JDBC Connection Hiveserver2