hive server

来源:互联网 发布:淘宝优惠券广告 编辑:程序博客网 时间:2024/04/29 05:24

hive server

hive 1

启动:

hive –service hiveserver

jdbc连接

org.apache.hadoop.hive.jdbc.HiveDriver
dbc:hive://172.22.1.100:10000/test

hive2

启动:

hive –service hiveserver2

jdbc连接

org.apache.hive.jdbc.HiveDriver
dbc:hive2://172.22.1.100:10000/test

开启远程写的权限

hdfs-site.xml

<property><name>dfs.permissions</name><value>false</value></property>

配置用户自定义安全策略

首先hive支持多种安全认证方式:NONE,NOSASL, KERBEROS, LDAP, PAM ,CUSTOM等,一般默认配置为none就是没有任何验证,所以需要其他的安全策略,我选择的是CUSTOM(自定义安全策略)。
如果将hive.server2.authentication设置成CUSTOM,则需要设置
hive.server2.custom.authentication.class来指定用于权限认证的类,这个类需要实现
org.apache.hive.service.auth.PasswdAuthenticationProvider接口。
首先实现一个自定义类:

package com.imis.project;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import javax.security.sasl.AuthenticationException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hive.conf.HiveConf;import org.apache.hive.service.auth.PasswdAuthenticationProvider;public class CustomHiveServer2Auth implements PasswdAuthenticationProvider  {    @Override    public void Authenticate(String username, String password)            throws AuthenticationException {        boolean ok = false;        String passMd5 = new MD5().md5(password);        HiveConf hiveConf = new HiveConf();        Configuration conf = new Configuration(hiveConf);        String filePath=null;        File file=null;        try {            filePath=conf.get("hive.server2.custom.authentication.file");            System.out.println("hive.server2.custom.authentication.file ["                    + filePath + "] ..");            file = new File(filePath);        } catch (Exception e) {            System.out.println("not found!");        }        BufferedReader reader = null;        try {            reader = new BufferedReader(new FileReader(file));            String tempString = null;            while ((tempString = reader.readLine()) != null) {                String[] datas = tempString.split(",", -1);                if(datas.length != 2) continue;                //ok                if(datas[0].equals(username) && datas[1].equals(passMd5)) {                    ok = true;                    break;                }            }            reader.close();        } catch (Exception e) {            e.printStackTrace();            throw new AuthenticationException("read auth config file error, [" + filePath + "] ..", e);        } finally {            if (reader != null) {                try {                    reader.close();                } catch (IOException e1) {}            }        }        if(ok) {            System.out.println("user [" + username + "] auth check ok .. ");        } else {            System.out.println("user [" + username + "] auth check fail .. ");            throw new AuthenticationException("user [" + username + "] auth check fail .. ");        }    }    //MD5加密    class MD5 {        private MessageDigest digest;        private char hexDigits[] = {'0','1','2','3','4','5','6','7',        '8','9','a','b','c','d','e','f'};        public MD5() {            try {              digest = MessageDigest.getInstance("MD5");            } catch (NoSuchAlgorithmException e) {              throw new RuntimeException(e);            }        }         public String md5(String str) {            byte[] btInput = str.getBytes();            digest.reset();            digest.update(btInput);            byte[] md = digest.digest();            // 把密文转换成十六进制的字符串形式            int j = md.length;            char strChar[] = new char[j * 2];            int k = 0;            for (int i = 0; i < j; i++) {                byte byte0 = md[i];                strChar[k++] = hexDigits[byte0 >>> 4 & 0xf];                strChar[k++] = hexDigits[byte0 & 0xf];            }            return new String(strChar);        }    }}

然后整个项目打包,包括包的结构命名为HiveServer2Auth.jar,放到$HIVE_HOME/lib下
hive-site.xml

<property><name>hive.server2.authentication</name><value>CUSTOM</value></property><property><name>hive.server2.custom.authentication.class</name><value>com.imis.project.CustomHiveServer2Auth</value></property><property><name>hive.server2.custom.authentication.file</name><value>/usr/local/apache-hive-0.13.1-bin/conf/hive.server2.users.conf</value></property>

在$HIVE_HOME/conf下新建文件hive.server2.users.conf,里面写入内容:
[root@dev conf]# cat hive.server2.users.conf
user,5f4dcc3b5aa765d61d8327deb882cf99

//下面这个是password的MD5码,你可以自己用上面的函数定制你的密码
5f4dcc3b5aa765d61d8327deb882cf99

然后重要的一件事,配置hive的日志
在hive的conf目录下执行

cp hive-log4j.properties.template hive-log4j.properties
修改
hive.log.dir=/home/hadoop/hive/conf # 默认的存储位置
hive.log.file=hive.log # 默认的文件名

最后在相应目录下新建hive.log文件就可以看到hive系统日志了

当然你也可以配置job的日志

启动hiveserver2,已经做到了权限控制。

远程调用mapreduce的问题

返回错误代码1,表示的是用户认证的错误
返回错误代码2,表示的是参数的问题

此处查看hive.log发现是

user=imis, access=EXECUTE, inode=”/tmp/hadoop-yarn”:hadoop:supergroup:rwx—-)

此处解决方案:首先发现是权限的问题然后通过命令

hadoop fs -ls /tmp

发现有两个目录hadoop-yarn和hive,在hive里面就是刚刚在自定义安全策略里面的用户名的目录比如user
然后就会发现user是没有办法使用hadoop-yarn目录的,因为user没有权限,所以首先把hive目录下的用户授予权限,然后改变hadoop-yarn的权限。

hdfs dfs -chmod -R 777 /tmp/hadoop-yarn
hdfs dfs -chomd -R 777 /tmp/hive

注:此处hadoop-tarn和hive的用户是hadoop组是supergroup,有一个现象就是root用户没有授予权限的权限,只能由hadoop用户授予777权限。

0 0
原创粉丝点击