scala - 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
因为这将在我用于运行项目的集群上运行测试,而不是在某些远程测试环境上。
如果这不是正确的方法?谁能解释一下我应该如何在某个远程集群的测试环境上运行火花测试用例的动机?
解决方案
推荐阅读
- git - 如何在不影响子模块的情况下 git reset --hard HEAD^ 回购根目录
- docker - Docker 容器未压缩大小
- ios - 如何让 iMessage 在 iOS 14+ 模拟器上显示 Xcode 测试的链接气泡?
- flutter - 使用带有 GetX/Obx() 的 Switch() 小部件
- android - 在 Google Play 控制台中更新
- javascript - 将格式为 Sat Dec 30 1899 HH:MM:SS 的 JavaScript 时间戳转换为今天?
- windows-10 - AutoHotKey - 三指拖动脚本导致小问题,如何解决?
- javascript - 如何在包含对象的反应js中映射数组?
- java - 尝试从响应正文上的关闭流中读取
- python - 我怎么知道列表中有哪些元素?