首页 > 解决方案 > Spark 程序从一个未指定的位置获取 hadoop 配置

问题描述

我有几个测试用例,例如在 HDFS 上读取/写入文件,我想使用 Scala 自动化并使用 maven 运行。我已经把测试环境的Hadoop配置文件放到了我的maven项目的资源目录中。该项目在我用来运行项目的任何集群的所需集群上也运行良好。

我没有得到的一件事是 Spark 如何从资源目录中获取 Hadoop 配置,即使我没有在项目的任何地方指定它。下面是来自项目的代码片段。

def getSparkContext(hadoopConfiguration: Configuration): SparkContext ={
    val conf = new SparkConf().setAppName("SparkTest").setMaster("local")     
    val hdfsCoreSitePath = new Path("/etc/hadoop/conf/core-site.xml","core-site.xml")
    val hdfsHDFSSitePath = new Path("/etc/hadoop/conf/hdfs-site.xml","hdfs-site.xml")
    val hdfsYarnSitePath = new Path("/etc/hadoop/conf/yarn-site.xml","yarn-site.xml")
    val hdfsMapredSitePath = new Path("/etc/hadoop/conf/mapred-site.xml","mapred-site.xml")
    hadoopConfiguration.addResource(hdfsCoreSitePath)
    hadoopConfiguration.addResource(hdfsHDFSSitePath)
    hadoopConfiguration.addResource(hdfsYarnSitePath)
    hadoopConfiguration.addResource(hdfsMapredSitePath)
    hadoopConfiguration.set("hadoop.security.authentication", "Kerberos")
    UserGroupInformation.setConfiguration(hadoopConfiguration)
    UserGroupInformation.loginUserFromKeytab("alice", "/etc/security/keytab/alice.keytab")
    println("-----------------Logged-in via keytab---------------------")
    FileSystem.get(hadoopConfiguration)
    val sc=new SparkContext(conf)
    return sc
  }
@Test
def testCase(): Unit = {
    var hadoopConfiguration: Configuration = new Configuration()
    val sc=getSparkContext(hadoopConfiguration)
    //rest of the code
    //...
    //...
  }

在这里,我使用了hadoopconfiguration对象,但我没有在任何地方指定它,sparkContext因为这将在我用于运行项目的集群上运行测试,而不是在某些远程测试环境上。

如果这不是正确的方法?谁能解释一下我应该如何在某个远程集群的测试环境上运行火花测试用例的动机?

标签: scalaapache-sparkhadoop

解决方案


推荐阅读