java - Hadoop 单例模式的使用

标签 java hadoop design-patterns singleton

我正在尝试实现单例,它将在 hadoop 中缓存和验证 map reduce 作业的配置。我们将其命名为 ConfigurationManager

这是我目前拥有的:

public class ConfigurationManager {
    private static volatile ConfigurationManager instance;
    private static final String CONF_NAME = "isSomethingEnabled";
    private boolean isSomethingEnabled;

    private ConfigurationManager(Configuration configuration) {
        this.isSomethingEnabled= configuration.getBoolean(CONF_NAME, false);
    }

    public static void init(Configuration configuration) {
        if (instance == null) {
            synchronized (ConfigurationManager.class) {
                if (instance == null) {
                    this.instance = new ConfigurationManager(configuration);
                }
            }
        }
    }

    public static ConfigurationManager get() {
        return instance;
    }

    public boolean isSomethingEnabled() {
        return isSomethingEnabled;
    }
}  

如您所见,它被设计为线程安全的。此外,它不是标准的单例:我将初始化和访问器方法分开,不强制在 get 调用时出现 hadoop 的 Configuration 实例。因此,为了使用它,我过早地在 Tool 的祖先中调用了 init,然后尝试在 reducer 中使用 get 访问我的单例(像这样 ConfigurationManager.get().isSomethingEnabled()),但由于某些原因 get 返回 null。有人可以解释这种行为吗?也许 maps/reducer 是作为单独的进程启动的?

最佳答案

每个 reduce 任务运行在不同的 jvm 上。这可以解释 null。

您可以在以下位置执行每个 reduce 任务:Reducer - configure

关于java - Hadoop 单例模式的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41673176/

相关文章:

java - 场景的最佳设计模式

java - GWT 2.0 Chrome 开发者插件的问题

java - 包括 TestNG @Before* 步骤以吸引报告

java - 组合器在HBase扫描mapreduce中为每个区域创建mapoutput文件

mysql - 在 sqoop 导出中,对于长文本,Sqoop 将列设为空

ruby - 将类方法委托(delegate)给ruby中的对象

java - 什么模式与策略模式一起使用可以避免具体策略中的重复代码?

java - RSA key 对不起作用

java - Jersey 无法使用 MOXy 解码对象(序言中不允许内容)

hadoop - 从哪里获取大数据管道的示例数据和查询?