Реализация марковской модели Флинка

Я хочу реализовать марковскую модель во Flink. Сначала я прочитал данные из Кафки. Как я могу реализовать модель триграммы Маркова с помощью flink?


person mustfkeskin    schedule 17.10.2017    source источник


Ответы (1)


Наконец-то я реализую марковскую модель. Этот код вычисляет только матрицу перехода.

    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));
    }
}
person mustfkeskin    schedule 17.10.2017