java核心代码
JavaPairDStream<String, Integer> wordCounts = pair.updateStateByKey(new Function2<List<Integer>, Optional<Integer>, Optional<Integer>>() {
@Override
public Optional<Integer> call(List<Integer> values,
Optional<Integer> state) throws Exception {
Integer newValue = 0;
if(state.isPresent())
{
newValue = state.get();
}
for(Integer value : values)
{
newValue += value;
}
return Optional.of(newValue);
}
});
scala:
val wordCounts = pairs.updateStateByKey((values:Seq[Int],state:Option[Int]) => {
var newValue = state.getOrElse(0);
for(value <- values)
{
newValue += value;
}
Option(newValue)
})