如何解决如何获取DynamicRecord字段值,将其用于匹配,然后更新DynamicRecord
使用AWS Glue的Scala的新功能。我想做的是获取DynamicRecord上一个字段的值,以便我可以使用该值与之匹配,然后更新记录,然后返回它。
在下面的代码中,我想首先使用以下内容获取字段值: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
进行比赛,所以我可以将其用于进一步的更新。
但是我需要先获取值,然后进行匹配,最后返回更改后的记录。类似于以下内容。但是因为我使用字段值进行匹配,所以不再需要引用案例中的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的引用,以便可以对其进行更改并返回它?
我收到的错误是缺少参数类型,类型不匹配。
任何帮助表示赞赏。
谢谢!
解决方法
您的描述有些混乱。看起来您想要的是这样的东西。
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
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。