NoSuchElementException:下一个在 MapPartitionsToPair-Spark 中的空迭代器上

NoSuchElementException: next on empty iterator in MapPartitionsToPair-Spark

我正在尝试在 Spark Streaming 中执行 MapPartitionsToPair,但它总是 returns 错误:NoSuchElementException: next on empty iterator。

这是我的代码:

 JavaPairDStream<String,String> streamGiveKey=   streamData1.mapPartitionsToPair(new PairFlatMapFunction<Iterator<String>, String, String>() {
      @Override
      public Iterable<Tuple2<String, String>> call(Iterator<String> stringIterator) throws Exception {

            ArrayList<Tuple2<String,String>>arrayOfPartitionsWithKeys= new ArrayList<Tuple2<String, String>>();

             while (stringIterator.hasNext()){
                 if(stringIterator.next()==null){
                     return null;
                 }

                 JsonMessage retMap = new Gson().fromJson(stringIterator.next(),JsonMessage.class);
                 String key= retMap.getSid();
                Tuple2<String,String> keyValue= new Tuple2<String,String>(key,stringIterator.next());
                 arrayOfPartitionsWithKeys.add(keyValue);

             }


 return  arrayOfPartitionsWithKeys;
         }
    });

谁能告诉我可能是什么问题? 非常感谢。

每次调用 iterator.next() 时,都会在基础集合中向前移动一个元素。在这种情况下,我建议在你的循环中使用 next 一次并将其分配给一个本地变量并重用该变量...