hive端口只能开启一个,开启多个会有阻塞,不利于并发访问。在通过jdbc远程访问hive数据仓库时,需要启动 hiveserver2服务器,并通过 beeline命令行 连接到 hiveserver2服务器
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//连接到hive数据
$beeline>show databases ;
$beeline>use mydb2 ;
$beeline>show tables; //显式表
3. 创建数据表 t ,插入一条数据测试
4. 在windows端编程测试连接
/**
* 使用jdbc方式连接到hive数据仓库,数据仓库需要开启hiveserver2服务。
*
*/
public class App {
public static void main(String[] args) throws Exception{
Class.forName("org.apache.hive.jdbc.HiveDriver");
Connection conn = DriverManager.getConnection("jdbc:hive2://192.168.32.201:10000/mydb2");
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("select id, name, age from t");
while(rs.next()) {
System.out.println(rs.getInt(1) + " , " + rs.getString(2));
}
rs.close();
statement.close();
conn.close();
}
}
//输出
1 , tom