java - 静态类中的 IO 异常 - java

标签 java exception hadoop ioexception

public class classifyTweet {

  public static class MapClass 
          extends Mapper<LongWritable, Text, Text, Text> {

  static final Configuration conf = new Configuration();

  protected void map(LongWritable key, Text value, Context context) 
             throws IOException, InterruptedException {

  StandardNaiveBayesClassifier classifier = new StandardNaiveBayesClassifier(NaiveBayesModel.materialize(new Path(modelPath), conf));

    }
  }
 }

我只想初始化分类器变量一次,materialize 方法抛出 IOEception,如果我在 map 方法之外声明它并且喜欢配置对象初始化,它会给出 IOException 的编译错误。如何只初始化一次?

最佳答案

制作 StandardNaiveBayesClassifier - 单例

public class StandardNaiveBayesClassifier {
private static StandardNaiveBayesClassifier instance;

public static StandardNaiveBayesClassifier getInstance(... you params) {
    if (instance == null)
        instance = new StandardNaiveBayesClassifier();
    return instance;
}

private StandardNaiveBayesClassifier() {
}

关于java - 静态类中的 IO 异常 - java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17270894/

相关文章:

hadoop - yarn hadoop 2.4.0 : info message: ipc. 客户端重试连接到服务器

java - 按下按键和播放器动画发生之间的延迟(KeyListener,Java)

java - InternetAddress 允许在本地部分使用方括号 - 错误?

java - with try resource when opening the resource throws exception怎么办?

mongodb - 在 Hadoop MapReduce(使用 Mongo Hadoop 连接器)之后,重复记录被写入 MongoDB

hadoop - AWS EMR 上的 YARN 日志聚合 - UnsupportedFileSystemException

Java 游戏 - 潜艇 killer - 只要按下向下键就发射炸弹

java - JTextArea txt; txt.getText() 跳过 "\n"

c++ - 如果抛出异常,alloca() 是否返回内存?

symfony - 如何添加自定义错误页面Symfony 3