如何理解这种语法?case streamRelation @ StreamingRelation(dataSourceV1,sourceName,输出)

jay*_*ong 3 scala

在阅读结构化流的源代码时,我对此语法感到困惑。

在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)
    })
……
 }
Run Code Online (Sandbox Code Playgroud)

我的问题:

  1. 如何理解案例streamingRelation @ StreamingRelation(dataSourceV1,sourceName,输出)?

  2. “ @”的作用是什么?

Krz*_*sik 5

您可以通过多种方法进行模式匹配:

您可以按类型将整个对象捕获为变量:

 case streamingRelation: StreamingRelation => 
     //do something with object of type StreamingRelation bound to variable streamingRelation
Run Code Online (Sandbox Code Playgroud)

或者您可以解构它:

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

的语法@结合了以下两者:

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