gpt4 book ai didi

scala - 如何理解这个语法? case streamingRelation@StreamingRelation(dataSourceV1, sourceName, 输出)

转载 作者:行者123 更新时间:2023-12-01 05:08:39 25 4
gpt4 key购买 nike

在阅读structured Streaming的源代码时,我对这个语法感到困惑。

在 microBatchExecution.scala 中

val _logicalPlan = analyzedPlan.transform {
case streamingRelation@StreamingRelation(dataSourceV1, sourceName,
output) =>
toExecutionRelationMap.getOrElseUpdate(streamingRelation, {
// Materialize source to avoid creating it in every batch
val metadataPath = s"$resolvedCheckpointRoot/sources/$nextSourceId"
val source = dataSourceV1.createSource(metadataPath)
nextSourceId += 1
logInfo(s"Using Source [$source] from DataSourceV1 named
'$sourceName' [$dataSourceV1]")
StreamingExecutionRelation(source, output)(sparkSession)
})
……
}

My Question:

  1. how to understand case streamingRelation@StreamingRelation(dataSourceV1, sourceName, output)?

  2. what's the function of "@" here?

最佳答案

有许多不同的方法可以进行模式匹配:

您可以通过类型捕获整个对象到变量:

 case streamingRelation: StreamingRelation => 
//do something with object of type StreamingRelation bound to variable streamingRelation

或者你可以解构它:

case StreamingRelation(dataSourceV1, sourceName, output) => 
//do something with members of an object like dataSourceV1, sourceName etc.

@ 的语法结合了两者:

case streamingRelation@StreamingRelation(dataSourceV1, sourceName, output) =>
//both whole object is available as streamingRelation and all matched members like dataSourceV1, sourceName

关于scala - 如何理解这个语法? case streamingRelation@StreamingRelation(dataSourceV1, sourceName, 输出),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56235852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com