`
zhangxiong0301
  • 浏览: 351106 次
社区版块
存档分类
最新评论

HIVESERVER2问题解决

    博客分类:
  • HIVE
 
阅读更多

 

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;
		
	}

	
	
}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics