ディレクトリ
WindowAllDataStream→AllWindowedStream
所望のウィンドウ値がウィンドウ+ ProcessWindowFunctionを低減することができる場合、ウィンドウ減らしますか
ウィンドウつ折りウィンドウまたは所望の値であればウィンドウ+ ProcessWindowFunctionを低減することができます
ウィンドウが+ ProcessWindowFunctionを削減します
ウィンドウが+ ProcessWindowFunctionを折ります
ウィンドウAggreate + ProcessWindowFunction
1つの分類
データストリーム
地図
でDataStream→でDataStream
FlatMap
でDataStream→でDataStream
フィルター
でDataStream→データストリームを
KeyBy
でDataStream→KeyedStream
WindowAll
でDataStream→すべてのウィンドウ表示ストリーム
プロセス・インスタンス化 ProcessFunction、各要素を処理します
でDataStream→でDataStream
keyedStreamは、[すべての状態重合されています]
(ウィンドウが削減)对比削減
KeyedStream→でDataStream
(ウィンドウ倍)对比倍
KeyedStream→でDataStream
(合計/最小/最大を含む)集計の比較(Windowsの集約ON)
KeyedStream→データストリームが
ウィンドウ
KeyedStream→WindowedStream
KeyedProcessFunction
KeyedStream→でDataStream
ウィンドウストリーム[重合ステートレス]
適用ウィンドウ(ウィンドウ型変換処理に時間ウィンドウ)
WindowedStream→でDataStream
AllWindowedStream→でDataStream
ウィンドウ減らすか、所望のウィンドウ値がウィンドウであってもよい場合は+ ProcessWindowFunction削減
WindowedStream→データストリームを
所望の値がウィンドウであってもよい場合、ウィンドウは、ウィンドウを倍または削減+ ProcessWindowFunction
WindowedStream→でDataStream
ウィンドウまたはウィンドウ上の集計が所望の値がウィンドウかもしれ場合の削減+ ProcessWindowFunction
WindowedStream→でDataStream
ウィンドウ ProcessWindowFunction(各要素を処理するために、ウィンドウタイムウィンドウ)
WindowedStream→でDataStream
重要なケース
データストリーム
ProcessFunction
でDataStream→でDataStream
val processStream: DataStream[result] = dataStream
.process(new getAllFunction)
//将UserBehavior类转成result类
class getAllFunction extends ProcessFunction[UserBehavior, result] {
override def processElement(value: UserBehavior,
ctx: ProcessFunction[UserBehavior, result]#Context,
out: Collector[result]): Unit = {
//对每一个元素处理
value match {
case behavior: UserBehavior => {
out.collect(result(behavior.itemId, behavior.count))
}
case _ => print("no way")
}
}
}
WindowAll
でDataStream→すべてのウィンドウ表示ストリーム
val resultDataStream: DataStream[String] = processStream
.windowAll(TumblingProcessingTimeWindows.of(Time.seconds(5)))
.apply((_: TimeWindow, input: Iterable[result], out: Collector[String]) => {
out.collect(input.mkString(","))
})
resultDataStream.print()
//输出结果:result(1715,1),result(1715,1),result(1715,1),result(1716,1),result(1716,1)
keyedStream
参照ウィンドウストリームkeyby
窓ストリーム
(1)、グループ化し、Windowsを非グループ化します。
keybyとWindoWall、パケット・データは、各論理パケットフローは、独立して、他の論理パケットフローマルチタスクの実行されることを、あなたの計算ウィンドウの同時実行をストリーミングします。非パケット・データ・ストリームでは、元のデータ・ストリームとは、複数のストリームに分割されていないと、すべての論理窓のロジックは、タスクで同時実行の度に実行されます。
(2)事前定義されたウィンドウディスペンサー
スクロールウィンドウ
滚动事件时间窗口
input
.keyBy(<key selector>)
.window(TumblingEventTimeWindows.of(Time.seconds(5)))
.<windowed transformation>(<window function>);
滚动处理时间窗口
input
.keyBy(<key selector>)
.window(TumblingProcessingTimeWindows.of(Time.seconds(5)))
.<windowed transformation>(<window function>);
スライディングウィンドウ
滑动事件时间窗口
input
.keyBy(<key selector>)
.window(SlidingEventTimeWindows.of(Time.seconds(10), Time.seconds(5)))
.<windowed transformation>(<window function>);
滑动处理时间窗口
input
.keyBy(<key selector>)
.window(SlidingProcessingTimeWindows.of(Time.seconds(10), Time.seconds(5)))
.<windowed transformation>(<window function>);
ケース:スクロール処理時間窓
//windowStream
val windowStream: WindowedStream[(String, Long, Int), Tuple, TimeWindow] = textKeyStream.
window(TumblingProcessingTimeWindows.of(Time.seconds(10)))
// textKeyStream.print("windowStream:")
//windowStream:> (000002,1461756879000,1)
//windowStream:> (000002,1461756879001,1)
//windowStream:> (000002,1461756879002,1)
適用ウィンドウ(ウィンドウ型変換処理に時間ウィンドウ)
WindowedStream→でDataStream
AllWindowedStream→でDataStream
val resultDataStream: DataStream[String] = processStream
.windowAll(TumblingProcessingTimeWindows.of(Time.seconds(5)))
.apply((_: TimeWindow, input: Iterable[result], out: Collector[String]) => {
out.collect(input.mkString(","))
})
resultDataStream.print()
//输出结果:result(1715,1),result(1715,1),result(1715,1),result(1716,1),result(1716,1)
所望のウィンドウ値がウィンドウであってもよい場合、ウィンドウ低減または削減+ ProcessWindowFunction
WindowedStream→でDataStream
val reduceValue: DataStream[result] = dataStream
.process(new getLastFunction)
.keyBy("itemId")
.window(TumblingProcessingTimeWindows.of(Time.seconds(15)))
.reduce { (v1, v2) => result(v1.itemId, v1.count + v2.count) }
reduceValue.print()
所望の値がウィンドウであってもよい場合、ウィンドウは、ウィンドウを倍または削減+ ProcessWindowFunction
WindowedStream→でDataStream
val foldValue: DataStream[result] = dataStream
.process(new getLastFunction)
.keyBy("itemId")
.window(TumblingProcessingTimeWindows.of(Time.seconds(15)))
.fold(result(111,333)){(original:result,ele:result)=>{
result(ele.itemId,original.count+ele.count)
}}
foldValue.print()
ウィンドウが + ProcessWindowFunctionを削減します
WindowedStream→データストリームが:でDataStream種類をウィンドウのパラメータを増やし、および変換しました
val reduceWindowFunctionData: DataStream[String] = dataStream
.process(new getLastFunction)
.keyBy("itemId")
.window(TumblingProcessingTimeWindows.of(Time.seconds(15)))
.reduce((v1, v2) => result(v1.itemId, v1.count + v2.count)
, (key: Tuple, window: TimeWindow, input: Iterable[result], out: Collector[String]) => {
var ele = input.iterator.next()
out.collect((s"${window.getStart}, $ele"))
}
)
ウィンドウが + ProcessWindowFunctionを折ります
WindowedStream→でDataStream
val foldWindowFunctionData: DataStream[String] = dataStream
.process(new getLastFunction)
.keyBy("itemId")
.window(TumblingProcessingTimeWindows.of(Time.seconds(15)))
.fold(result(111, 333), (original: result, ele: result) => {
result(ele.itemId, original.count + ele.count)
}, (key: Tuple, window: TimeWindow, input: Iterable[result], out: Collector[String]) => {
var ele = input.iterator.next()
out.collect((s"${window.getEnd}, $ele"))
})
ウィンドウ Aggreate + ProcessWindowFunction
main{
val aggregateData: DataStream[String] = dataStream
.process(new getLastFunction)
.keyBy("itemId")
.window(TumblingProcessingTimeWindows.of(Time.seconds(15)))
.aggregate(new CountAggregate,new MyProcessWindowFunction)
aggregateData.print()
}
case class result(itemId: Long, count: Long)
//ACC createAccumulator(); 迭代状态的初始值
//ACC add(IN value, ACC accumulator); 每一条输入数据,和迭代数据如何迭代
//ACC merge(ACC a, ACC b); 多个分区的迭代数据如何合并
//OUT getResult(ACC accumulator); 返回数据,对最终的迭代数据如何处理,并返回结果。
class CountAggregate extends AggregateFunction[result, Long, String] {
override def createAccumulator() = 6L
override def add(value: result, accumulator:Long) =
value.count+accumulator
override def getResult(accumulator: Long) = "windows count is:"+accumulator.toString
override def merge(a: Long, b: Long) =
a+b
}
class MyProcessWindowFunction extends ProcessWindowFunction[String, String, Tuple, TimeWindow] {
def process(key: Tuple, context: Context, input: Iterable[String], out: Collector[String]) = {
val count = input.iterator.next()
out.collect("window end is :"+context.window.getEnd+"key is :"+key+count)
}
}
出力:
ウィンドウ終了は次のとおりです。1575213345000keyは次のとおりです。(1715)のウィンドウがカウントされる:7
ウィンドウ終了です:1575213345000keyは次のとおりです。(1713)のウィンドウがカウント:7で
ウィンドウ終了です:1575213345000keyは次のとおりです。(1716)のウィンドウが数:8
図3に示すように、窓関数演算:オリジナル(79000)の値である、折るなど、2枚目の原稿はa79000で、反復が継続します。
//foldStream
//输入:
// 000002 79000
//000002 79001
//000002 79002
//000003 79003
//000004 79004
val groupDstream: DataStream[String] = windowStream.
fold("a") { case (original, value) =>
original + value._2
}
// groupDstream.print("foldDstream::::").setParallelism(1)
//window::::> a790007900179002
//window::::> a79004
//window::::> a79003
窓関数を折る:あなたは、ウィンドウのプロパティに必要がある場合は、この方法を使用することができます
windowStream.fold(("", 0L, 100), (orignal: (String, Long, Int), element: (String, Long, Int)) => {
(element._1, orignal._2 + element._2, orignal._3 + element._3)
}, new MyWindowFunction).print()
class MyWindowFunction extends WindowFunction[(String, Long, Int), String, Tuple, TimeWindow] {
def apply(key: Tuple, window: TimeWindow, input: Iterable[(String, Long, Int)], out: Collector[String]): Unit = {
var allnumber = input.iterator.next()
out.collect(s"Window ${window.getEnd} count: $allnumber")
// input.foreach{
// case (a, b, c) => {
// out.collect(s"${window.getEnd} $a,$b,$c")
// }
// }
}
}
4、削減
//reducedDstream
//输入:000002 0
//000002 1
//000002 2
//000003 3
//000004 4
val reducedDstream: DataStream[(String, Long)] = windowStream.reduce((t1, t2) => {
(t1._1, t1._2 + t2._2)
})
// reducedDstream.print("reducedDstream::::").setParallelism(1)
//reducedDstream::::> (000002,3)
//reducedDstream::::> (000004,4)
//reducedDstream::::> (000003,3)
窓関数を減らします
windowStream.reduce((t1, t2) => {
(t1._1, t1._2 + t2._2, t1._3 + t2._3)
}, (key: Tuple, window: TimeWindow, input: Iterable[(String, Long, Int)], out: Collector[String]) => {
var ele = input.iterator.next()
out.collect((s"${window.getStart}, $ele"))
}).print()
5、timeWindowAllが適用和
.timeWindowAll(Time.seconds(5))
//窗口输出的每个元素是一个迭代器
.apply { (_: TimeWindow, input: Iterable[(ClickLog, ClickMetrics)], out: Collector[java.util.List[(ClickLog, ClickMetrics)]]) => out.collect(input.toList.asJava) }