hive(7):maven整合hive简单实例-爱代码爱编程
1 前置环境
确保集群上hivesever2的服务已启动
hive数据库一般在10000端口
2 新建一个maven项目
新建一个maven项目
3 引入POM文件
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-jdbc -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>3.1.2</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
注意:引入的jar包的版本根据自己的hive版本进行选择。
3 编写一个测试程序
package org.example;
import java.sql.*;
public class App
{
public static final String URL="jdbc:hive2://192.168.222.138:10000/handsome";
public static final String USER = "root";
public static String PASS = "123456";
public static void main( String[] args ) throws SQLException {
Connection connection = DriverManager.getConnection(URL,USER,PASS);
String sql = "select * from t_team_ace_player";
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(sql);
System.out.println("编号\t队名\t玩家名");
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("team_name");
String age = rs.getString("ace_player_name");
System.out.println(id+"\t"+name+"\t"+age);
}
}
}
测试结果如下