当我放弃修改Configurationonline 的要求并且仅基于自定义的一组 Hadoop 配置 *.xml 文件时,我已经解决了这个难题。
起初,我编写了 Java 类,它将附加层的配置添加到org.apache.hadoop.conf.Configuration 的默认资源中。它是静态初始化附加配置默认资源:
public class Configurator {
static {
// We initialize needed Hadoop configuration layers default configuration
// by loading appropriate classes.
try {
Class.forName("org.apache.hadoop.hdfs.DistributedFileSystem");
} catch (ClassNotFoundException e) {
LOG.error("Failed to initialize HDFS configuartion layer.");
}
try {
Class.forName("org.apache.hadoop.mapreduce.Cluster");
} catch (ClassNotFoundException e) {
LOG.error("Failed to initialize YARN/MapReduce configuartion layer.");
}
// We do what actually HBase should: default HBase configuration
// is added to default Hadoop resources.
Configuration.addDefaultResource("hbase-default.xml");
Configuration.addDefaultResource("hbase-site.xml");
}
// Just 'callable' handle.
public void init() {
}
}
所以现在如果有人只是加载我的Configurator,他或她会通过类路径搜索以下 inftastructure 配置:core、hdfs、MapReduce、YARN、HBase。相应的文件是core-default.xml、core-site.xml、hdfs-default.xml、hdfs-site.xml、mapred-default.xml、mapred-site.xml、yarn-default.xml、yarn-site.xml、hbase-default.xml、hbase-site.xml。如果我需要额外的层,扩展没有问题。
Configurator.init() 只是为了有更简单的类加载句柄。
现在我需要在 Spark 上下文启动期间扩展 Python Spark 脚本以访问配置器:
# Create minimal Spark context.
sc = SparkContext(appName="ScriptWithIntegratedConfig")
# It's critical to initialize configurator so any
# new org.apach.hadoop.Configuration object loads our resources.
sc._jvm.com.wellcentive.nosql.Configurator.init()
所以现在普通的 Hadoop new Configuration() 构造(在基于 Hadoop 的数据集的 PythonRDD 基础架构中很常见)导致从类路径加载所有层配置,我可以在其中放置所需集群的配置。
至少对我有用。