首页 > 解决方案 > 如何在 Spark Scala 应用程序的配置中的案例类字段上放置布尔条件并在运行时评估它们?

问题描述

我有一个用 Scala 编写的 Spark 应用程序,其中有一个 Dataset[Event],其中 Event 是用户定义的类型,如下所示:

case class Event(timestamp: Long, state: String, source: String)

我正在转变为:

case class TransformedEvent(timestamp: Long, state: String, source: String, is_finished: Boolean)

基本上,我在其他字段的基础上添加一个字段“is_finished”。

示例:is_finished = true if

state = "state1"
AND
source = "source1"

等等

为了更好地解释,这里是代码:

val events: Dataset[Event] = getEvents()

// Here is the transformation

val transformedEvents: Dataset[TransformedEvent] = events.map(e => convert(e))

// where the convert function is something like this

def convert(event: Event): TransformedEvent = {

    val isFinished = if(event.state == "state1" && event.source = "source1")

    TransformedEvent(timestamp = event.timestamp,
                     state = event.state,
                     source = event.source,
                     is_finished = isFinished)

}

我正在尝试找出一种方法来制作这种 event.state == "state1" && event.source = "source1" 配置驱动的条件,因为我将来可能必须添加/删除/更新这些条件,因此不想在每次发生这种情况时都更改代码和部署。

谁能指出我正确的方向?

提前致谢。

标签: scalaapache-sparkconfigurationconfig

解决方案


推荐阅读