1.启动hiveserver2
nohup /home/hadoop/hive-1.1.0-cdh5.5.2/bin/hiveserver2 >> /home/hadoop/gtq_dir/logs/hiveserver.log 2>&1 &
2.代码如下:
package cn.hive; import java.sql.*; /** * Created by jieyue on 2017/12/18. */ public class HiveJdbcTest { private static String driveName = "org.apache.hive.jdbc.HiveDriver"; private static Connection con = null; private static Statement stmt = null; private static ResultSet res = null; public static void main(String[] args) throws SQLException { 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(); } } }
3.pom文件如下
<?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> <version>1.0-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <hadoop.version>2.6.0</hadoop.version> <hive.version>1.1.0</hive.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.hive</groupId> <artifactId>hive-jdbc</artifactId> <version>${hive.version}</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-auth</artifactId> <version>${hadoop.version}</version> <exclusions> <exclusion> <artifactId>jdk.tools</artifactId> <groupId>jdk.tools</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>${hadoop.version}</version> <exclusions> <exclusion> <artifactId>jdk.tools</artifactId> <groupId>jdk.tools</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>${hadoop.version}</version> <exclusions> <exclusion> <artifactId>jdk.tools</artifactId> <groupId>jdk.tools</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>${hadoop.version}</version> <exclusions> <exclusion> <artifactId>jdk.tools</artifactId> <groupId>jdk.tools</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-mapreduce-client-core</artifactId> <version>${hadoop.version}</version> </dependency> </dependencies> <build> <resources> <resource> <directory>${basedir}/src/main/java</directory> <excludes> <exclude>**/*.java</exclude> </excludes> <filtering>true</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.3</version> <configuration> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>