状態 - のFLINK動作例

flatMap(... RichFlatMapFunction)

ヴァルkeyedSensorData = sensorData.keyBy(。_ ID)

valを警告= keyedSensorData 
  .flatMap(新しいTemperatureAlert(1.7))

alerts.print()

... 

クラスTemperatureAlert(ヴァルしきい値:ダブル)は、RichFlatMapFunction [SensorReading、(文字列、ダブルを拡張しますダブル)] { 
    プライベートVAR lastTempState:ValueState [ダブル] = _ 

    オーバーライドデフオープン(パラメータ:構成):単位= { 
        valのlastTempDesc =新しいValueStateDescriptor [ダブル]( "lastTemp"、Types.of [ダブル])
        lastTempState = getRuntimeContext。 getStateを[ダブル](lastTempDesc)
    } 

    flatMap DEFオーバーライド(読み出し:SensorReading、アウト:コレクタ[(文字列、ダブル、ダブル)]):単位= { 
        ヴァルlastTemp = lastTempState.value()
        ヴァルtempDiff =(lastTemp - reading.temperature).ABS 
        IF(tempDiff>閾値){ 
            out.collect(reading.id、reading.temperature、tempDiff)
        } 
        this.lastTempState.update(reading.temperature)
    } 
}

 

flatMapWithState

ヴァルアラート= keyedSensorData.flatMapWithState [(文字列、ダブル、ダブル)、ダブル] { 
  場合(で:SensorReading、なし)=> 
    (List.empty、一部(in.temperature))
  場合(R:SensorReading、lastTemp:一部の[ダブル])=> 
    ヴァルtempDiff =(r.temperature - lastTemp.get).ABS 
    (tempDiff> 1.7){場合
      (リスト((r.id、r.temperature、tempDiff)、一部(r.temperature)))
    }他{ 
      (List.empty、一部(r.temperature))
    } 
}

 

 

233

おすすめ

転載: www.cnblogs.com/lemos/p/12649826.html