2013-06-24 2 views
0
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)); 

    } 
    } 
} 

클래스 메서드를 바깥으로 선언하고 IOException과 같은 컴파일 오류를 발생시키는 경우 클래스 화기 변수를 한 번만 초기화하고 메소드가 IOEception을 던지려고합니다. 한 번만 초기화 할 수 있습니까?정적 클래스 내부에서 IO 예외 -

+0

예외의 스택 추적을 제공하십시오. –

+0

컴파일시 오류가 발생했습니다.보고되지 않습니다. IOException을 캐치하거나 던져야합니다. –

답변

1

메이크업의 StandardNaiveBayesClassifier - 싱글

public class StandardNaiveBayesClassifier { 
private static StandardNaiveBayesClassifier instance; 

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

private StandardNaiveBayesClassifier() { 
} 

}

1

당신은 한 번만 classifier 변수를 초기화하는 정적 블록을 사용할 수 있습니다. 정적 블록의 classifier 객체를 생성 할 때 I는 modelPath 변수를 가정하고있어

public class classifyTweet { 

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

    static final Configuration conf = new Configuration(); 

    static final StandardNaiveBayesClassifier classifier; 

    static { 
     try { 
     classifier = new StandardNaiveBayesClassifier(NaiveBayesModel.materialize(new Path(modelPath), conf)); 
     } 
     catch(IOException e) { 
      e.printStackTrace(); 
      System.out.println("Initialization failed."); 
     } 
    } 

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

    //do some work... 

    } 
    } 
} 

이 범위에있다. 당신은 그것에 대해 아무 말도하지 않습니다.

+0

'NaiveBayesModel.materialize (새 경로 (modelPath), conf)'가 IOException을 발생시키기 때문에 동일한 IOException이 발생하지 않았습니까? throws, 그래서이 문장은 정적이 아닌 메소드 안에 있어야합니까? –

+0

@MahenderSingh 예. 과연. 나는 정적 블록에'try/catch'를 놓는 것을 잊어 버렸습니다. 답변을 업데이트했습니다. – dcernahoschi