背景
Hive 中集成了 Thrift 服务。Thrift 是 Facebook 开发的一个软件框架,它用来进行可扩展且跨语言的服务的开发。Hive 的 HiveServer2 就是基于 Thrift 的,所以能让不同的语言如 Java、Python 来调用 Hive 的接口。对于 Java,Hive 提供了 jdbc 驱动,用户可以使用 Java 代码来连接 Hive 并进行一系列操作。
本节将演示如何使用 Java 代码来连接 HiveServer2
一、使用maven创建一个工程
添加pom依赖:
代码语言:javascript复制<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.8.4</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.8.4</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.8.4</version>
</dependency>
继续在 pom.xml 中添加打包和编译插件:
代码语言:javascript复制<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
二、查看hiveserver2的端口号
代码语言:javascript复制[root@10 ~]# su Hadoop
[hadoop@10 root]$ cd /usr/local/service/hive/
[hadoop@10 hive]$ cat conf/hive-site.xml
<property>
<name>hive.server2.thrift.port</name>
<value>7001</value>
</property>
即HiveServer2 的端口号是7001
三、添加集群安全组入站规则
在创建EMR集群的时候,可能我们没有开HiveServer2的端口号,所以无法使用Java连接到hive,所以在这里我们必须在安全组添加HiveServer2的入站规则,具体操作则是进入控制台,找到EMR集群,点击右侧的【详情】进入并复制安全组ID,如图:
接下来在控制台,通过左上方的【云产品】->【云服务器】->【安全组】,右侧搜索安全组ID,Ctrl v复制安全组ID搜索,点击进入,添加入站规则,如图添加HiveServer2端口的入站规则:
四 、具体代码如下
说明:hive的超级用户是hadoop。
代码语言:javascript复制import java.sql.*;
public class HiveTest {
private static String driverName =
"org.apache.hive.jdbc.HiveDriver";
public static void main(String[] args)
throws SQLException {
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
Connection con = DriverManager.getConnection(
"jdbc:hive2://*.*.*.*:7001/default", "hadoop", "");
Statement stmt = con.createStatement();
String tableName = "HiveTestByJava";
stmt.execute("drop table if exists " tableName);
stmt.execute("create table " tableName
" (key int, value string)");
System.out.println("Create table success!");
// show tables
String sql = "show tables '" tableName "'";
System.out.println("Running: " sql);
ResultSet res = stmt.executeQuery(sql);
if (res.next()) {
System.out.println(res.getString(1));
}
// describe table
sql = "describe " tableName;
System.out.println("Running: " sql);
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getString(1) "t" res.getString(2));
}
sql = "insert into " tableName " values (42,"hello"),(48,"world")";
stmt.execute(sql);
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(1) from " tableName;
System.out.println("Running: " sql);
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getString(1));
}
}
}
结果: