1) 原因
Spark standlone 模式下面driver 提交到cluster,application 用户变成了spark 部署环境用户,这样会带来两个问题:
a) spark master web无法知道那个用户提交的任务;
b) spark权限问题无法管理,比如读取Hive、HBase、HDFS数据的权限问题;
本文提出一种简单的修改方法,让Spark standlone driver on cluster 模型下面读取数据权限问题可以解决。
2) 代码流程
3) 修改方法
原理上面需要从client.main agrs获取driver提交端的user name;然后经过driverdesciption传送给master和worker,需要注意的是driverdesciption里面只能给command envi添加字段;经过worker传送给driver ,但是其中经过了数组转换,转换过程比较复杂,需要修改的代码较多。
本文直接在sparkconf里面设置driverproxyuser属性:
conf.set("driverproxyuser",user.name) |
需要修改的代码:
a) sparkcontext 的sparkuser变量; web显示的用户;
b) SparkDeploySchedulerBackend 的ApplicationDescription的system user name,该app user name可以用于获取hive、hbase等表的权限;
4) 修改代码
sparkcontext.scala
diff /SparkCode/spark-1.4.0/core/src/main/scala/org/apache/spark/SparkContext.scala core/src/main/scala/org/apache/spark/SparkContext.scala 301,315c301 < // val sparkUser =Utils.getCurrentUserName() < //val sparkUser= config.get("driverproxyuser",Utils.getCurrentUserName()) < val sparkUser = { < val conf=config.clone < < if(conf.contains("driverproxyuser")) { < logInfo("conf contains driverproxyuser configered by user ?" + conf.contains("driverproxyuser")) < val proxyuser=conf.get("driverproxyuser") < System.setProperty("HADOOP_USER_NAME", proxyuser ) < // System.setProperty("user.name", proxyuser) < proxyuser < }else{ < Utils.getCurrentUserName() < } < } --- > val sparkUser = Utils.getCurrentUserName() 495c481 < executorEnvs("SPARK_USER") = sparkUser //sp Added JARarkUser --- > executorEnvs("SPARK_USER") = sparkUser 521d506 < |
SparkDeploySchedulerBackend.scala
diff /SparkCode/spark-1.4.0/./core/src/main/scala/org/apache/spark/scheduler/cluster/SparkDeploySchedulerBackend.scala ./core/src/main/scala/org/apache/spark/scheduler/cluster/SparkDeploySchedulerBackend.scala 86,94d85 < < //add by Ricky < val username=System.getProperty("user.name") < if(conf.contains("driverproxyuser")){ < val proxyuser=conf.get("driverproxyuser") < logInfo("set ApplicationDescription user is : "+proxyuser) < System.setProperty("HADOOP_USER_NAME",proxyuser) < System.setProperty("user.name",proxyuser) < } 97,101d87 < < < < logInfo("ApplicationDescription user is :"+appDesc.user) < System.setProperty("user.name",username) // added by Ricky |