JavaPairDStream<String,Integer> counts=pairword.updateStateByKey(new Function2<List<Integer>, Optional<Integer>, Optional<Integer>>() { @Override public Optional<Integer> call(List<Integer> values, Optional<Integer> state) throws Exception { // TODO Auto-generated method stub // TODO Auto-generated method stub /* * values:经过分组最后这个key所对应的value【1,1,1,1,1】 * state:这个key在本次之前的状态 */ Integer update=0; if(state.isPresent()) { //判断state上一次是否有值 update=state.get(); } for(Integer value : values) { //累加 update+=value; } return Optional.of(update); } });

解决办法:可能是Optional导错包了,应该导
import com.google.common.base.Optional;

评论 抢沙发