HIVESERVER2问题解决

本文介绍HiveServer2的基础参数配置及安全策略设置,包括如何启动服务、通过JDBC进行连接,并针对配置错误给出了解决方案。此外,还详细讲解了自定义安全验证的实现方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

hiveserver2是hiveserver的高级版本,在安全和并发上有所增强。hiveserver2相关的基础参数有:

hive.server2.transport.mode – 默认值为binary(TCP),可选值HTTP,0.13版本后开始支持。  
hive.server2.thrift.http.port– HTTP的监听端口,默认值为10001。
hive.server2.thrift.min.worker.threads– 最小工作线程数,默认为5。
hive.server2.thrift.max.worker.threads – 最大工作线程数,默认为500。  
hive.server2.thrift.port– TCP 的监听端口,默认为10000。  
hive.server2.thrift.bind.host– TCP绑定的主机,默认为localhost。
hive.server2.enable.doAs:设置为false,查询将以运行hiveserver2进程的用户运行,否则以提交查询的用户执行查询
hive.server2.long.polling.timeout:Time in milliseconds that HiveServer2 will wait, before responding to asynchronous calls that use long polling,默认值5000L。
hive.server2.authentication:HIVESERVER2的安全验证机制,有4中种选项:
      NONE: no authentication check
       LDAP: LDAP/AD based authentication
       KERBEROS: Kerberos/GSSAPI authentication
       CUSTOM: Custom authentication provider
               (Use with property hive.server2.custom.authentication.class)
       PAM: Pluggable authentication module.
hive.server2.custom.authentication.class:当采用CUSTOM验证时,指定自定义的验证类。

 

 

配置好参数就可以启动hiveserver2服务:

 

${HIVE_HOME}/bin/hive --service hiveserver2

或者 

${HIVE_HOME}/bin/hiveserver2

 

 

启动后使用JDBC连接,简单java代码如下:

 

  1. public class HiveJdbcClient {  
  2.   
  3.     private static String driverName = "org.apache.hive.jdbc.HiveDriver";  
  4.     
  5.     public boolean run() {  
  6.   
  7.         try {  
  8.             Class.forName(driverName);  
  9.             Connection con = null;  
  10.             con = DriverManager.getConnection(  
  11.                     "jdbc:hive2://192.168.30.42:10000/hivedb""""");  
  12.             Statement stmt = con.createStatement();  
  13.             ResultSet res = null;  
  14.   
  15.             String sql = "select count(*) from test_data";  
  16.   
  17.             System.out.println("Running: " + sql);  
  18.             res = stmt.executeQuery(sql);  
  19.             System.out.println("ok");  
  20.             while (res.next()) {  
  21.                 System.out.println(res.getString(1));  
  22.   
  23.             }  
  24.             return true;  
  25.         } catch (Exception e) {  
  26.             e.printStackTrace();  
  27.             System.out.println("error");  
  28.             return false;  
  29.         }  
  30.   
  31.     }  
  32. }

 

结果报错如下:

 

java.sql.SQLException: Invalid time unit l
        at org.apache.hive.jdbc.Utils.verifySuccess(Utils.java:120)
        at org.apache.hive.jdbc.Utils.verifySuccessWithInfo(Utils.java:108)
        at org.apache.hive.jdbc.HiveStatement.execute(HiveStatement.java:265)
        at com.dazen.HiveJDBC.exeQuery(HiveJDBC.java:21)
        at com.dazen.HiveJDBC.main(HiveJDBC.java:33)

 

 

解决办法:

 


修改配置参数:
hive.server2.long.polling.timeout 的默认值 5000L为5000,通过看hive源代码发现,hive这个参数后面的字母代表的是时间单位,如合法的有d,m,s,ms,us等等。
在hive-common工程里的Hiveconf类中有如下代码:

  public static TimeUnit unitFor(String unit, TimeUnit defaultUnit) {
    unit = unit.trim().toLowerCase();
    if (unit.isEmpty()) {
      if (defaultUnit == null) {
        throw new IllegalArgumentException("Time unit is not specified");
      }
      return defaultUnit;
    } else if (unit.equals("d") || unit.startsWith("day")) {
      return TimeUnit.DAYS;
    } else if (unit.equals("h") || unit.startsWith("hour")) {
      return TimeUnit.HOURS;
    } else if (unit.equals("m") || unit.startsWith("min")) {
      return TimeUnit.MINUTES;
    } else if (unit.equals("s") || unit.startsWith("sec")) {
      return TimeUnit.SECONDS;
    } else if (unit.equals("ms") || unit.startsWith("msec")) {
      return TimeUnit.MILLISECONDS;
    } else if (unit.equals("us") || unit.startsWith("usec")) {
      return TimeUnit.MICROSECONDS;
    } else if (unit.equals("ns") || unit.startsWith("nsec")) {
      return TimeUnit.NANOSECONDS;
    }
    throw new IllegalArgumentException("Invalid time unit " + unit);
  }

 

 

到此为止,就可以免用户账号登陆了(hive.server2.enable.doAs=false,为true的话则需要设置用户名为hadoop管理员账号密码为空),即在上述JDBC代码中用户密码两个参数为"".

 

下面配置安全策略,可以采用自定义方式,步骤为:

1. hive.server2.authentication=CUSTOM,

2. 自己需要实现一个认证的类如:com.qiku.custom.auth.HiveServer2Auth,将               class文件打成jar包放入${HIVE_HOME}/lib下

3.hive.server2.custom.authentication.class=com.qiku.custom.auth.HiveServer2Auth

4.hive.server2.enable.doAs=false。注意:如果这个参数为true的话,那我们只能用hadoop系统账号做用户名,因为此时hive将以自定义的用户名去访问hadoop文件,会遇到无访问权限的问题;设置为false,则我们的自定义的用户只做验证用,不做访问hdfs的账号。

5.按照自己的代码逻辑,在hive-site.xml中添加自定义的配置项。根据我的实现(代码后面贴出)则配置为:hive.server2.auth.hadoop=wyx,其中hadoop为用户名。

6.最后在jdbc代码中指定用户名和密码就可以。

 

具体自定义认证的代码很简单,只需要实现:一个认证接口PasswdAuthenticationProvider以完成认证,以及Configurable接口以让hive能将所有的配置参数(比如hive-site.xml)提供给我们的代码。

代码如下:

package com.qiku.custom.auth;

import javax.security.sasl.AuthenticationException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configurable;
import org.apache.hadoop.conf.Configuration;
import org.apache.hive.service.auth.PasswdAuthenticationProvider;

public class HiveServer2Auth implements PasswdAuthenticationProvider,Configurable{

	private static final Log LOG = LogFactory.getLog(  HiveServer2Auth.class );
	private Configuration conf = null;
	private static final String HIVE_SERVER2_AUTH_PREFIX="hive.server2.auth.%s";
	
	@Override
	public void Authenticate(String user, String passwd)throws AuthenticationException {
		
		String pass = getConf().get( String.format( HIVE_SERVER2_AUTH_PREFIX , user) );
		
		if( pass == null || !pass.equals( passwd ) ){
			throw new AuthenticationException( "用户登录HIVESERVER2验证失败 !" );
		}
	}

	@Override
	public Configuration getConf() {
		
		return conf;
	}

	@Override
	public void setConf(Configuration conf) {
		this.conf = conf;
		
	}

	
	
}

 

 

### 解决 HiveServer2 无法启动及 DataGrip 连接 Hive 报错问题 #### **HiveServer2 启动失败的原因分析与解决办法** 1. **HDFS 处于安全模式** - HDFS 安全模式可能导致 HiveServer2 启动失败。当 NameNode 处于安全模式时,它不允许任何写操作,这会影响 Hive 的元数据存储初始化。 - 检查当前是否处于安全模式: ```bash hadoop dfsadmin -safemode get ``` - 若显示为 `ON`,则退出安全模式: ```bash hadoop dfsadmin -safemode leave ``` 2. **Metastore 数据库不可用** - Hive Metastore 是 Hive 元数据的核心组件。如果数据库服务(如 MySQL 或 PostgreSQL)未正常运行或者连接字符串配置错误,都会导致 HiveServer2 启动失败。 - 确保数据库服务已启动,并测试连接: ```bash mysql -u <username> -p<password> -h <host> ``` - 验证 `hive-site.xml` 中的 JDBC URL 和凭证是否正确[^2]。 3. **日志排查** - 查阅 Hive 日志文件以定位具体错误原因。默认路径位于 `/var/log/hive/` 或 `$HIVE_HOME/logs/`。 - 寻找关键字如 `Error`, `Exception` 来缩小问题范围。 4. **端口冲突或绑定失败** - 如果 HiveServer2 的监听端口已被其他进程占用,则会引发启动异常。 - 检测目标端口状态: ```bash netstat -anop | grep :10000 ``` - 修改 `hive-site.xml` 中的 `hive.server2.thrift.port` 参数指定新的端口号[^1]。 --- #### **DataGrip 连接 Hive 出现 Connection Refused 的处理措施** 1. **确认 HiveServer2 正常工作** - 使用 Beeline 工具验证 HiveServer2 是否能够接受外部请求: ```bash beeline -u jdbc:hive2://node1:10000/default -n username -p password ``` - 成功登录表明基础环境搭建完成;反之需回溯前述步骤逐一排除隐患。 2. **调整 DataGrip 配置** - 在 DataGrip 中创建一个新的 Hive 数据源时,请仔细核对以下参数: - **Host**: 输入实际部署 HiveServer2 的主机名或 IP 地址。 - **Port**: 默认值一般设为 10000。 - **Database name**: 可选填,默认选用 `default` 库作为初始目录。 - 添加兼容版本的 Hive JDBC Driver 至插件列表下拉框选项里[^3]。 3. **网络连通性检验** - Ping 测试确保两台设备间存在可达链路关系: ```bash ping node1 ``` - Telnet 方法进一步判断 TCP 层面交互状况: ```bash telnet node1 10000 ``` --- ### 综合脚本案例展示 下面给出一段用于自动化诊断和修复常见问题的小型 Shell 脚本: ```bash #!/bin/bash # Step A: Check if HiveServer2 is running. ps aux | grep 'org.apache.hive.service.server.HiveServer2' | grep -v grep &>/dev/null if [[ $? != 0 ]]; then echo "[WARN] HiveServer2 service seems down." fi # Step B: Validate metastore connectivity via CLI tool. mysql -u hive_user -phive_password -e "SHOW DATABASES;" &>/dev/null || \ { echo "[ERROR] Failed connecting to the configured RDBMS."; exit 1; } # Optional C: Restart services gracefully after fixing issues detected above. for svc in hdfs namenode datanode resourcemanager nodemanager ; do systemctl restart $svc && sleep 5 done echo "[INFO] All checks passed successfully!" ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值