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;
采用jdbc方式访问远程数据仓库
依赖
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>2.1.0</version>
</dependency>
代码:
/**
* 使用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.136.128:10000/mydb2");
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select id , name ,age from t");
while(rs.next()){
System.out.println(rs.getInt(1) + "," + rs.getString(2)) ;
}
rs.close();
st.close();
conn.close();
}
}