通过远程jdbc方式连接到hive数据仓库
1.启动hiveserver2服务器,监听端口10000
$>hive --service hiveserver2
2.通过beeline命令行连接到hiveserver2
$>beeline //进入beeline命令行(于hive --service beeline)
$beeline>!help //查看帮助
$beeline>!quit //退出
$beeline>!connect jdbc:hive2://localhost:10000/mydb2 //连接到hibve数据
$beeline>show databases ;
$beeline>use mydb2 ;
$beeline>show tables;
使用Hive-jdbc驱动程序采用jdbc方式访问远程数据仓库
1.创建java模块
2.引入maven
3.添加hive-jdbc依赖
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>2.1.0</version>
</dependency>
4.连接访问
/**
* 使用jdbc方式连接到hive数据仓库,数据仓库需要开启hiveserver2服务。
*/
public class HiveJdbc {
public static void main(String[] args) throws Exception {
Class.forName("org.apache.hive.jdbc.HiveDriver");
Connection conn = DriverManager.getConnection("jdbc:hive2://192.168.159.128:10000/mydb");
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select id , name ,age from t2");
while(rs.next()){
System.out.println(rs.getInt(1) + "," + rs.getString(2)) ;
}
rs.close();
st.close();
conn.close();
}