如何获取 DynamicRecord 字段值,使用它进行匹配,然后更新 DynamicRecord

How to get DynamicRecord field value, use that for matching, then update DynamicRecord

使用 AWS Glue 的 Scala 新手。我想要做的是为传入的 DynamicRecord 上的字段获取一个值,以便我可以使用它来匹配,然后更新记录,然后 return 它。

在下面的代码中,我想先用这个获取字段值:rec => rec.getField(col)

所以我可以在继续匹配之前测试该值。

这是我目前拥有的,它按原样工作。

    def doSomething(col: String): DynamicRecord => DynamicRecord = 
    { 
     
      rec => rec.getField(col) match 
      {
        case Some(s: String) => 
        {
           //do something with s to the record (rec)
        }
        case default => do something with default case to the record (rec)
      }
      rec
    }

请注意,因为我直接与 rec 进行匹配,所以我可以使用它来进一步更新它。

但我需要先获取值,然后匹配,最后 return 更改的记录。类似于下面的内容。但是因为我正在使用字段值进行匹配,所以我不再引用案例中的 rec

    def doSomething(col: String): DynamicRecord => DynamicRecord = 
    { 
      var fldVal: String = ""
      rec => fldVal = rec.getField(col)

      fldVal match 
      {
        case Some(s: String) => 
        {
           //do something to the record with s
        }
        case default => //do something to the record with default case
      }
      rec //need to return record here
    }

我怎样才能先从 rec 获取字段值然后引用 rec 以便我可以更改它并 return 它?

我收到的错误是,缺少参数类型,类型不匹配。

感谢任何帮助。

谢谢!

您的描述有点混乱。看来你要找的是这样的。

def doSomething(col: String): DynamicRecord => DynamicRecord =
  rec => {
    rec.getField(col) match {
      case Some(s: String) =>
        //do something to the record with s
      case default =>
        //do something to the record with default case
    }
    rec //need to return record here
  }

如果是这样,我可能会推荐以下简化。

def doSomething(col: String): DynamicRecord => DynamicRecord =
  rec => rec.getField(col).fold {
    //return rec modified with default case
  } { s =>
    //return rec modified with s
  }