运行方式
Hive脚本运行方式
Hive在CLI模式中
hive打通了 hdfs 和 linux 本地文件系统
与hdfs交互
执行执行dfs命令
例:dfs –ls /
与Linux交互
!开头
例: !pwd
sql运行脚本
hive -e “”
//重定向
hive -e “”>aaa
//静默输出,去掉无关字内容
hive -S -e “”>aaa
//运行脚本
hive -f file
//运行完脚本后进入hive客户端
hive -i /home/my/hive-init.sql
hive> source file (在hive cli中运行)
Hive Beeline
首先server端启动hiveserver2,默认端口号10000
注意:生成环境中最好用hiveServer2
Beeline 要与HiveServer2配合使用
服务端启动hiveserver2
客户的通过beeline两种方式连接到hive
1、beeline -u jdbc:hive2:// localhost :10000/default -n root
2、beeline
beeline> !connect jdbc:hive2://<host>:<port>/<db>;auth=noSasl root 123
示例:!connect jdbc :hive2://node2:10000/default; root 123
默认 用户名、密码不验证
退出:beeline> !quit
Hive JDBC
服务端启动hiveserver2后,在java代码中通过调用hive的jdbc访问默认端口10000进行连接、访问
代码
public class Demo { public static void main(String[] args) { Connection connection=null; Statement statement=null; ResultSet rs=null; try { Class.forName("org.apache.hive.jdbc.HiveDriver"); connection=DriverManager.getConnection("jdbc:hive2://node2:10000/default","root",""); String sql="select * from t_person"; statement=connection.createStatement(); rs=statement.executeQuery(sql); while(rs.next()){ System.out.println(rs.getInt(1)); } } catch (Exception e) { e.printStackTrace(); }finally{ // rs statement connection依次关闭 } } }