---
title: "security cdh mapreduce access hbase"
author: "lookqlp"
date: "2014-12-02T15:09:17Z"
tags: ["HBase"]
categories: ["开源软件"]
slug: "security-cdh-mapreduce-access-hbase"
draft: false
lastmod: "2015-03-17T11:13:02Z"
id: 2162513
url: "https://lookqlp.iteye.com/blog/2162513"
---

执行mapreduce的用户必须是可以访问hdfs相应目录和执行mapreduce的账户，例如hive。 指定hive的节点kinit获取执行权限 在mapreduce main代码中加入访问hbase的权限，例如： import java.io.IOException; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.HTableInterface; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.security.UserGroupInformation; public class Connection { private static final String ZK\_PATH = "10.1.33.21,10.1.33.22,10.1.33.23"; public static void main(String\[] args) { org.apache.hadoop.conf.Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", ZK\_PATH); conf.set("hbase.rpc.protection", "privacy"); conf.set("hadoop.security.authentication", "Kerberos"); UserGroupInformation.setConfiguration(conf); try { UserGroupInformation.loginUserFromKeytab(conf.get("hbase.master.kerberos.principal"), conf.get("hbase.keytab.path")); } catch (IOException e1) { e1.printStackTrace(); } // HTablePool tablePool = new HTablePool(conf, 5); // @SuppressWarnings("deprecation") // HTableInterface usersTable = (HTable)tablePool.getTable("dsp\_data"); HTableInterface dsp\_data; try { dsp\_data = new HTable(conf, "dsp\_data"); Put p = new Put(Bytes.toBytes("rowkey2")); p.add(Bytes.toBytes("index"), Bytes.toBytes("click\_count"), Bytes.toBytes("111")); dsp\_data.put(p); dsp\_data.close(); } catch (IOException e) { e.printStackTrace(); } } } 同时，hdfs hive yarn xml加入到jar包中，若不行，采用ToolRunner形式执行jar，此种方式可以加载到正确jar包，若还不行，可将xml配置文件，加入/etc/hbase 配置文件夹下。 官方文章中（http://hbase.apache.org/book/security.html）提示需要加入配置： hbase.rpc.protection privacy 经过测试，就因为有了该配置导致如上问题，client hbase site xml不能加入此配置。 说明： 在cdh5.2.0中， hbase.rpc.engine org.apache.hadoop.hbase.ipc.SecureRpcEngine 此配置也不需要加入xml中。 xml例如： hbase.rootdir hdfs://ip-10-1-33-20.ec2.internal:8020/hbase hbase.client.write.buffer 2097152 hbase.client.pause 100 hbase.client.retries.number 35 hbase.client.scanner.caching 100 hbase.client.keyvalue.maxsize 10485760 hbase.rpc.timeout 60000 hbase.snapshot.enabled true hbase.security.authentication kerberos zookeeper.session.timeout 60000 zookeeper.znode.parent /hbase zookeeper.znode.rootserver root-region-server hbase.zookeeper.quorum ip-10-1-33-23.ec2.internal,ip-10-1-33-22.ec2.internal,ip-10-1-33-21.ec2.internal,ip-10-1-33-25.ec2.internal,ip-10-1-33-24.ec2.internal hbase.zookeeper.property.clientPort 2181 hbase.master.kerberos.principal hbase/\_HOST@YEAHMOBI.COM hbase.regionserver.kerberos.principal hbase/\_HOST@YEAHMOBI.COM mr中使用http://www.cloudera.com/content/cloudera/en/documentation/cdh5/v5-0-0/CDH5-Installation-Guide/cdh5ig\_mapreduce\_hbase.html TableMapReduceUtil.addDependencyJars(job);方式加载。 并且使用user api加入例如： hbase.master.kerberos.principal=hbase/ip-10-1-10-15.ec2.internal@YEAHMOBI.COM hbase.keytab.path=/home/dev/1015q.keytab 补充：2015-03-17 如上mapreduce访问安全的hbase是不可行的，解决办法参见《mapreduce mapper access security hbase》