我正在尝试使用isTimingOut删除状态并将其发送到输出Dstream。这是我的代码
val mappingFunc = (key: String, value: Option[Person1], state: State[Output]) => {
val defaultPerson = Output("", "", 1L, Long.MaxValue, 0, Some("str"))
val defaultValues =
state
.getOption()
.getOrElse(defaultPerson)
//Do some calculations to get the updated value
val maxi = value.get.time max defaultValues.defaultMinValue
val mini = value.get.time min defaultValues.defaultMaxValue
val first = if (!value.get.school.isEmpty && defaultValues.school == "")
{
value.get.school
}
else
{
defaultValues.school
}
val counter = defaultValues.counter + 1
val last = if (value.get.degree != None) //An update for a non null value
{
value.get.degree
}
else
{
Some(defaultValues.degree.get)
}
val output = Output(key, first, maxi, mini, counter, last)
if (state.isTimingOut()) {
println("State has timed out ...")
state.getOption()
}
else {
println("State is updated")
state.update(output)
}
}
工作正常。但是,当我使用isTimingOut时,我没有得到预期的结果。我想要的只是如果密钥超时,我希望将其从状态中删除,否则请保持其状态。
我叫
val stateDstream = pairs.mapWithState(
StateSpec.function(mappingFunc).timeout(Seconds(60)))