2017-10-17 2 views
0

Flink에서 Markov Model을 구현하고 싶습니다. 먼저 카프카에서 데이터를 읽습니다. 나는 어떻게 플라이크로 트라이 그램 마르코프 모델을 구현할 수 있습니까?Flink Markov Model 구현

답변

0

마크로프 모델을 마지막으로 구현합니다. 이 코드는 전이 행렬만을 계산합니다.

private static class MarkovModel implements AllWindowFunction<Tuple2<String,String>, Tuple3<Long, Long,  HashMap<String,Integer>>, TimeWindow>{ 
    @Override 
    public void apply(TimeWindow window, Iterable<Tuple2<String, String>> requests, Collector<Tuple3<Long, Long, HashMap<String, Integer>>> out) throws Exception { 

     HashMap<String,Integer> map = new HashMap<>(); 

     String first = ""; 
     String second = ""; 
     String third = ""; 

     for (Tuple2<String, String> request : requests) { 
      if(first == ""){ 
       third = second; 
       second = first; 
       first = request.f1; 
      }else if(second == ""){ 
       third = second; 
       second = request.f1; 
      }else if(third == ""){ 
       third = request.f1; 
      }else{ 
       third = second; 
       second = first; 
       first = request.f1; 
      } 

      if(third != ""){ 
       int count = map.getOrDefault(first + second + third,0); 
       map.put(first + second + third,count + 1); 
      } 
     } 


     System.out.println(map); 
     System.out.println(map.values().stream().mapToDouble(x->x).sum()); 
     out.collect(new Tuple3(window.getStart(), window.getEnd(), map)); 
    } 
} 
관련 문제