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

时间:2019-05-21 10:03:06

标签: 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)
    })
 ....
 }

我的问题: 如何理解case streamingRelation@StreamingRelation(dataSourceV1, sourceName, output)
@在这里的功能是什么?

1 个答案:

答案 0 :(得分:5)

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

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

 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