一:Hive的三种启动方式
1, hive 命令行模式
进入hive安装目录,输入bin/hive的执行程序,或者输入 hive –service cli
用于linux平台命令行查询,查询语句基本跟mysql查询语句类似
2, hive web界面的启动方式
bin/hive –service hwi (& 表示后台运行)
用于通过浏览器来访问hive,感觉没多大用途,浏览器访问地址是:127.0.0.1:9999/hwi
3, hive 远程服务 (端口号10000) 启动方式
bin/hive –service hiveserver2 &(&表示后台运行)
用java,python等程序实现通过jdbc等驱动的访问hive就用这种起动方式了,这个是程序员最需要的方式了
二:HiveServer2
1, 启动命令
hive --service hiveserver /
hiveserver2
2, 启动结果验证
telnet localhost 10000
3, 命令行链接hiveserver2
[root@node5 ~]# beeline
Beeline version 1.2.1 by Apache Hive
beeline> !connect jdbc:hive2://localhost:10000 root
Connecting to jdbc:hive2://localhost:10000
Enter password for jdbc:hive2://localhost:10000:
Connected to: Apache Hive (version 1.2.1)
Driver: Hive JDBC (version 1.2.1)
Transaction isolation: TRANSACTION_REPEATABLE_READ
0: jdbc:hive2://localhost:10000>
0: jdbc:hive2://localhost:10000> show tables;
+-------------------+--+
| tab_name |
+-------------------+--+
| page_view |
| partitioned_test |
| people |
| people_new |
+-------------------+--+
4 rows selected (0.066 seconds)
4,JDBC链接HIVE
1、在Intellij下新建项目。
2、在项目中引入需要的hive和hadoop相关jar包
jar包分别位于hadoop-2.5.1\share\hadoop\common和apache-hive-1.2.1-bin\apache-hive-1.2.1-bin\lib中
3、新建测试类
/**
* Created by ZhangJintao on 2017/10/29.
*/
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
public class testHIVESERVER2 {
//驱动 url
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
/**
* @param args
*/
public static void main(String[] args) {
try {
//加载驱动
Class.forName(driverName);
//链接hive数据仓库
Connection con = DriverManager.getConnection("jdbc:hive2://192.168.1.205:10000/default", "root", "");
//创建会话
Statement stmt = con.createStatement();
//执行dql,得到结果集
ResultSet res = stmt.executeQuery("select count(*) from people " );
// 遍历结果集,打印数据
if(res.next()){
System.out.println(res.getInt(1));
}
} catch (Exception e) {
// 打印异常
e.printStackTrace();
System.exit(1);
}
}
}
4、运行项目
控制台显示如下,这表示项目已经启动,hadoop正在执行mapreduce。
此时我们前往http://192.168.1.201:8088下查看Scheduler和Applications,可以看到这个查询的mapreduce正在执行,如下图所示
待其执行完毕,我们就可以看到程序有如下输出