【问题标题】:Extracting Map values from a List of Maps List[Map[String,Any]]?从 Maps List[Map[String,Any]] 列表中提取 Map 值?
【发布时间】:2017-10-16 21:53:20
【问题描述】:

您将如何进行模式匹配以从列表中包含的地图中提取值?

所以我的数据是List[Map[String,Any]] 类型,看起来像:

List(Map(sequence -> 192, id -> 8697413670252052, type -> List(AimLowEvent, DiscreteEvent), time -> 527638582195))
List(Map(duration -> 143858743, id -> 8702168014834892, sequence -> 195, sessionId -> 8697344444103393, time -> 527780267698, type -> List(SessionCanceled, SessionEnded)), Map(trackingInfo -> Map(trackId -> 14170286, location -> Browse, listId -> cd7c2c7a-00f6-4035-867f-d1dd7d89972d_6625365X3XX1505943605585, videoId -> 80000778, rank -> 0, row -> 0, requestId -> ac12f4e1-5644-46af-87d1-ec3b92ce4896-4071171), id -> 8697344444103393, sequence -> 89, time -> 527636408955, type -> List(Play, Action, Session)), 1)
List(Map(duration -> 142862569, id -> 8702168403395215, sequence -> 201, sessionId -> 8697374208897843, time -> 527780267698, type -> List(SessionCanceled, SessionEnded)), Map(trackingInfo -> Map(trackId -> 14170286, location -> Browse, listId -> cd7c2c7a-00f6-4035-867f-d1dd7d89972d_6625365X3XX1505943605585, videoId -> 80000778, rank -> 0, row -> 0, requestId -> ac12f4e1-5644-46af-87d1-ec3b92ce4896-4071171), id -> 8697374208897843, sequence -> 136, time -> 527637405129, type -> List(Play, Action, Session)), 1)

首先,我只想保留包含Map(trackingInfo -> .. 的记录,因为对我很重要的字段在这些记录中,例如trackId。但是在这些相同的记录中,我还需要外部字段,例如 sequence

我已经厌倦了将列表扁平化为 Map by,所以我可以在地图上进行匹配:

myList.flatten.toMap

但是它返回 java.lang.ClassCastException: scala.collection.immutable.Map$Map4 cannot be cast to scala.Tuple2 错误。

感谢任何帮助

【问题讨论】:

  • 您不应该从您显示的代码中获得此异常(或对 Stefano 的回答发表评论的异常)。真正的原因可能是在其他地方错误地使用了asInstanceOfisInstanceOf
  • @AlexeyRomanov 是的,我确实使用asInstanceOf[List[Map[String,Any]] 来获得上述结构。原始数据是嵌套的json。
  • @AlexeyRomanov 我认为你是对的,所以我开始以不同的方式处理这个问题。如果您对这里感兴趣是我关于解析原始 json stackoverflow.com/questions/46787888/… 的问题

标签: scala scala-collections


【解决方案1】:

对于您想要实现的目标,我相信filter 会做到:

// taken from your example and simplified
val list =
  List(
    Map("duration" -> 142862569L, "id" -> 8702168403395215L, "sequence" -> 201, "sessionId" -> 8697374208897843L, "time" -> 527780267698L, "type" -> List("SessionCanceled", "SessionEnded")),
    Map("trackingInfo" -> Map("trackId" -> 14170286L, "location" -> "Browse", "listId" -> "cd7c2c7a-00f6-4035-867f-d1dd7d89972d_6625365X3XX1505943605585", "videoId" -> "80000778", "rank" -> 0, "row" -> 0, "requestId" -> "ac12f4e1-5644-46af-87d1-ec3b92ce4896-4071171"), "id" -> 8697374208897843L, "sequence" -> 136, "time" -> 527637405129L, "type" -> List("Play", "Action", "Session")))

val onlyWithTrackingInfo =
  list.filter(_.contains("trackingInfo"))

打印onlyWithTrackingInfo 将输出以下(美化):

List(
  Map(
    trackingInfo -> Map(trackId -> 14170286, location -> Browse, listId -> cd7c2c7a-00f6-4035-867f-d1dd7d89972d_6625365X3XX1505943605585, videoId -> 80000778, rank -> 0, row -> 0, requestId -> ac12f4e1-5644-46af-87d1-ec3b92ce4896-4071171), 
    id -> 8697374208897843,
    sequence -> 136,
    time -> 527637405129,
    type -> List(Play, Action, Session)
  )
)

【讨论】:

  • 感谢您的回复,但我一直收到此错误java.lang.ClassCastException: scala.collection.immutable.$colon$colon cannot be cast to scala.collection.immutable.Map
  • 可能是因为在我的数据中,trackingInfo 仅出现在第二张地图中,而从来没有出现在您的数据中?
【解决方案2】:

一种方法是collect 任何包含所需密钥的Map

def extract(list: List[Map[String, Any]], key: String) = list.collect{
  case m if m.get(key) != None => m
}

val list1: List[Map[String, Any]] = List(
  Map("sequence" -> 192, "id" -> 8697413670252052L, "type" -> List("AimLowEvent", "DiscreteEvent"), "time" -> 527638582195L)
)

val list2: List[Map[String, Any]] = List(
  Map("duration" -> 143858743, "id" -> 8702168014834892L, "sequence" -> 195, "sessionId" -> 8697344444103393L, "time" -> 527780267698L, "type" -> List("SessionCanceled", "SessionEnded")),
  Map("trackingInfo" -> Map("trackId" -> 14170286, "location" -> "Browse", "listId" -> "cd7c2c7a-00f6-4035-867f-d1dd7d89972d_6625365X3XX1505943605585", "videoId" -> 80000778, "rank" -> 0, "row" -> 0, "requestId" -> "ac12f4e1-5644-46af-87d1-ec3b92ce4896-4071171"), "id" -> 8697344444103393L, "sequence" -> 89, "time" -> 527636408955L, "type" -> List("Play", "Action", "Session"))
)

extract(list1, "trackingInfo")
// res1: List[Map[String,Any]] = List()

extract(list2, "trackingInfo")
// res2: List[Map[String,Any]] = List(
//  Map(trackingInfo -> Map(trackId -> 14170286, location -> Browse, listId -> cd7c2c7a-00f6-4035-867f-d1dd7d89972d_6625365X3XX1505943605585, videoId -> 80000778, rank -> 0, row -> 0, requestId -> ac12f4e1-5644-46af-87d1-ec3b92ce4896-4071171), id -> 8697344444103393, sequence -> 89, time -> 527636408955, type -> List(Play, Action, Session))
// )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-02
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    • 1970-01-01
    相关资源
    最近更新 更多