Scala 当processElement依赖于广播数据时,如何在flink中单元测试BroadcastProcessFunction
我使用BroadcastProcessFunction实现了一个flink流。我从processBroadcastElement获取模型,并将其应用于processElement中的事件 我没有找到对流进行单元测试的方法,因为我没有找到确保在第一个事件之前调度模型的解决方案。 我想说有两种方法可以实现这一点:Scala 当processElement依赖于广播数据时,如何在flink中单元测试BroadcastProcessFunction,scala,apache-flink,Scala,Apache Flink,我使用BroadcastProcessFunction实现了一个flink流。我从processBroadcastElement获取模型,并将其应用于processElement中的事件 我没有找到对流进行单元测试的方法,因为我没有找到确保在第一个事件之前调度模型的解决方案。 我想说有两种方法可以实现这一点: 1.找到一个解决方案,首先在流中推送模型 2.让广播状态在流执行之前填充模型,以便恢复 我可能错过了一些东西,但我没有找到一个简单的方法来做到这一点 下面是我的问题的一个简单单元测试: i
1.找到一个解决方案,首先在流中推送模型
2.让广播状态在流执行之前填充模型,以便恢复 我可能错过了一些东西,但我没有找到一个简单的方法来做到这一点 下面是我的问题的一个简单单元测试:
import org.apache.flink.api.common.state.MapStateDescriptor
import org.apache.flink.streaming.api.functions.co.BroadcastProcessFunction
import org.apache.flink.streaming.api.functions.sink.SinkFunction
import org.apache.flink.streaming.api.scala._
import org.apache.flink.util.Collector
import org.scalatest.Matchers._
import org.scalatest.{BeforeAndAfter, FunSuite}
import scala.collection.mutable
class BroadCastProcessor extends BroadcastProcessFunction[Int, (Int, String), String] {
import BroadCastProcessor._
override def processElement(value: Int,
ctx: BroadcastProcessFunction[Int, (Int, String), String]#ReadOnlyContext,
out: Collector[String]): Unit = {
val broadcastState = ctx.getBroadcastState(broadcastStateDescriptor)
if (broadcastState.contains(value)) {
out.collect(broadcastState.get(value))
}
}
override def processBroadcastElement(value: (Int, String),
ctx: BroadcastProcessFunction[Int, (Int, String), String]#Context,
out: Collector[String]): Unit = {
ctx.getBroadcastState(broadcastStateDescriptor).put(value._1, value._2)
}
}
object BroadCastProcessor {
val broadcastStateDescriptor: MapStateDescriptor[Int, String] = new MapStateDescriptor[Int, String]("int_to_string", classOf[Int], classOf[String])
}
class CollectSink extends SinkFunction[String] {
import CollectSink._
override def invoke(value: String): Unit = {
values += value
}
}
object CollectSink { // must be static
val values: mutable.MutableList[String] = mutable.MutableList[String]()
}
class BroadCastProcessTest extends FunSuite with BeforeAndAfter {
before {
CollectSink.values.clear()
}
test("add_elem_to_broadcast_and_process_should_apply_broadcast_rule") {
val env = StreamExecutionEnvironment.getExecutionEnvironment
env.setParallelism(1)
val dataToProcessStream = env.fromElements(1)
val ruleToBroadcastStream = env.fromElements(1 -> "1", 2 -> "2", 3 -> "3")
val broadcastStream = ruleToBroadcastStream.broadcast(BroadCastProcessor.broadcastStateDescriptor)
dataToProcessStream
.connect(broadcastStream)
.process(new BroadCastProcessor)
.addSink(new CollectSink())
// execute
env.execute()
CollectSink.values should contain("1")
}
}
感谢David Anderson的更新我选择了缓冲溶液。我为同步定义了一个进程函数:
class SynchronizeModelAndEvent(modelNumberToWaitFor: Int) extends CoProcessFunction[Int, (Int, String), Int] {
val eventBuffer: mutable.MutableList[Int] = mutable.MutableList[Int]()
var modelEventsNumber = 0
override def processElement1(value: Int, ctx: CoProcessFunction[Int, (Int, String), Int]#Context, out: Collector[Int]): Unit = {
if (modelEventsNumber < modelNumberToWaitFor) {
eventBuffer += value
return
}
out.collect(value)
}
override def processElement2(value: (Int, String), ctx: CoProcessFunction[Int, (Int, String), Int]#Context, out: Collector[Int]): Unit = {
modelEventsNumber += 1
if (modelEventsNumber >= modelNumberToWaitFor) {
eventBuffer.foreach(event => out.collect(event))
}
}
}
谢谢这不是一个简单的方法。您可以让processElement缓冲其所有输入,直到processBroadcastElement接收到模型。或者在没有事件流量的情况下运行作业一次,并在模型广播后获取保存点。然后将该保存点恢复到同一作业中,但其事件输入已连接 顺便说一下,在Flink社区中,您所寻找的功能通常被称为“辅助输入”
dataToProcessStream
.connect(ruleToBroadcastStream)
.process(new SynchronizeModelAndEvent(3))
.connect(broadcastStream)
.process(new BroadCastProcessor)
.addSink(new CollectSink())