StandardNaiveBayesClassifier

StandardNaiveBayesClassifier

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/

10-10 02:02