Scala 使用Akka streams时,如何按包含不同值的字符串进行分组?
让我澄清一下我的问题: 这是我的密码Scala 使用Akka streams时,如何按包含不同值的字符串进行分组?,scala,csv,akka,akka-stream,Scala,Csv,Akka,Akka Stream,让我澄清一下我的问题: 这是我的密码 def averagePerAttributeFlow[T] = Flow[T] .groupBy(10000, { case User(id, location, age) => location match { case Location(city,state,country) => println("MATCHING BY CITY") city+state+country
def averagePerAttributeFlow[T] = Flow[T]
.groupBy(10000, {
case User(id, location, age) =>
location match {
case Location(city,state,country) =>
println("MATCHING BY CITY")
city+state+country
case Location("n/a",state,country) =>
println("MATCHING BY STATE")
state+country
}
case UserBookRating(userId, bookISBN, rating) => userId
})
我有来自csv文件的数据流。某些行的格式如下所示:
美国伊利诺伊州芝加哥
但是,有些行是这样的:
美国伊利诺伊州
有些线路甚至有城市垃圾数据:
美国伊利诺伊州
我如何根据包含美国伊利诺伊州的所有字符串进行分组,而不考虑是否提供了该城市
当用户键入“美国伊利诺伊州芝加哥”时,分组有效,但当用户键入“美国伊利诺伊州n/a”时,分组无效。
不适用表示用户不关心城市
编辑:
好的,所以我发现可能是我的过滤流没有像我预期的那样工作。代码如下:
def filterByAttributeFlow[T](attr: String) = Flow[T].filter( {
case user: User =>
val attrSplit = attr.split(",").map(_.trim())
attrSplit match {
case Array(city,state,country) =>
user.location.isCity(city, state, country) // check if location matches
case Array(_,state,country) =>
println("Filtering by State")
user.location.isState(state,country)
}
case bookRating: UserBookRating =>
bookRating.userId.contentEquals(attr) // check if userId matches
}).withAttributes(ActorAttributes.supervisionStrategy {
e: Throwable =>
system.log.error(s"Error filtering ratings by '$attr': {}", e)
Supervision.Resume // skips the erroneous data and resumes the stream
})
即使该城市没有提供或充满垃圾数据,我如何过滤和保留包含美国伊利诺伊州的所有数据条目?我想出来了。我不得不切换过滤器流程中的两个案例,并将其更改为“n/a”。。。我想我只需要和一只“橡皮鸭”谈谈:]我想出来了。我不得不切换过滤器流程中的两个案例,并将其更改为“n/a”。。。我想我只需要和一只“橡皮鸭”谈谈:]