【问题标题】:Flink Markov Model ImplementationFlink 马尔可夫模型实现
【发布时间】:2017-10-17 06:33:05
【问题描述】:

我想在 Flink 中实现马尔科夫模型。 首先,我从 Kafka 读取数据。如何用 flink 实现三元马尔可夫模型?

【问题讨论】:

    标签: apache-flink markov-models


    【解决方案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));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-31
      • 2023-04-07
      • 1970-01-01
      相关资源
      最近更新 更多