HiveServer2是经常与beeline一起使用,可以用jdbc客户端远程连接,一般用于生产环境。
在提供传统客服端的功能之外,还提供其他功能。
Beeline连接
1.先在hadoop集群启动HiveServer2
启动命令:hiveserver2
启动日志在hive.log中查看
2.再启动Beeline
命令: beeline
3.连接
!connect jdbc:hive2://localhost:10000
Beeline命令
传统命令与Beeline命令示例比较:
1.传统: quit;
2.Beeline: !quit
SQL语句操作还是和传统一样
HiveServer2 web界面管理
除了在HiveServer2服务端可以看到日志,还可以在hive -site.xml中配置 ip端口后,可提供ui界面
<property>
<name>hive.server2.webui.host</name>
<value>localhost</value>
</property>
<property>
<name>hive.server2.webui.port</name>
<value>10002</value>
</property>
JDBC连接
导入hive依赖包,hadoop-common及其相关依赖
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class HiveServerBeeline { // 驱动全名 private static String driverName = "org.apache.hive.jdbc.HiveDriver"; public static void main(String[] args) throws SQLException { // TODO Auto-generated method stub try { // JVM查找并加载指定的类,也就是说JVM会执行该类的静态代码段 和 new 示例化对象类似 Class.forName(driverName); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); System.exit(1); } // 连接并登陆数据库 Connection con = DriverManager.getConnection("jdbc:hive2://192.192.192.192:10000/default", "user", "passwd"); // 创建Statement对象 Statement stmt = con.createStatement(); String sql = "show databases"; // stmt.execute("create external table if not exists testdb.test(id int,name String)"); ResultSet res = stmt.executeQuery(sql);
if (res.next()) { System.out.println(res.getString(1)); } }
没导入hadoop-common依赖会报异常:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/conf/Configuration at org.apache.hive.jdbc.HiveConnection.createUnderlyingTransport(HiveConnection.java:418) at org.apache.hive.jdbc.HiveConnection.createBinaryTransport(HiveConnection.java:438) at org.apache.hive.jdbc.HiveConnection.openTransport(HiveConnection.java:225) at org.apache.hive.jdbc.HiveConnection.<init>(HiveConnection.java:182) at org.apache.hive.jdbc.HiveDriver.connect(HiveDriver.java:107) at java.sql.DriverManager.getConnection(DriverManager.java:664) at java.sql.DriverManager.getConnection(DriverManager.java:247) at hiveservertwo.HiveServerBeeline.main(HiveServerBeeline.java:32) Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.conf.Configuration at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 8 more