简体   繁体   English

如何在Scala中访问嵌套或分层Map结构

[英]How to access nested or hierarchical Map structure in Scala

I have huge data in nested / hierarchical Map format. 我有嵌套/分层Map格式的大量数据。 I am using Scala and spark streaming, to which I am very new. 我正在使用Scala和Spark Streaming,这是我的新手。 Lets say sample streamed data instance/row will look like - Map(nd -> 1, du -> 870, dg -> Map(), did -> GO37, host -> 11.1.1.22, sg -> Map(), nfw -> Map( dst_ip -> 11.1.1.23, v -> 1, src_ip -> 11.1.1.11, pkts -> 1), dnname -> RG, app_name -> read data, bpp -> 40) 假设样本流数据实例/行看起来像-Map(nd-> 1,du-> 870,dg-> Map(),did-> GO37,host-> 11.1.1.22,sg-> Map(), nfw-> Map(dst_ip-> 11.1.1.23,v-> 1,src_ip-> 11.1.1.11,pkts-> 1),dnname-> RG,app_name->读取数据,bpp-> 40)

How do I read 'dst_ip' values? 如何读取“ dst_ip”值? Because I want to read all instances of 'dst_ip' and compute count of it. 因为我想读取'dst_ip'的所有实例并计算它的计数。 I tried various methods like get, option but I am not getting desired output. 我尝试了诸如get,option之类的各种方法,但没有得到想要的输出。 Please advise how can I retrieve the required information. 请告知我如何检索所需的信息。

Given 特定

val myMap: Map[String, Any] = Map(
  "nd" -> 1,
  "du" -> 870,
  "dg" -> Map(),
  "did" -> "GO37",
  "host" -> "11.1.1.22",
  "sg" -> Map(),
  "nfw" -> Map(
    "dst_ip" -> "11.1.1.23",
    "v" -> 1,
    "src_ip" -> "11.1.1.11",
    "pkts" -> 1),
  "dnname" -> "RG",
  "app_name" -> "read data",
  "bpp" -> 40)

You can use pattern matching to give to specifically work on values which are Maps. 您可以使用模式匹配来专门处理Maps值。 Other types of values, you'll return None, which will filter them due to the flatMap. 其他类型的值,您将返回None,由于flatMap会对其进行过滤。 For values which are of type Map, you can then get values of key "dst_ip" (value.get returns an option of the value, this way Maps which don't have this key will return None and be filtered out): 对于Map类型的值,则可以获取键“ dst_ip”的值(value.get返回该值的选项,这样不具有此键的Map将返回None并被过滤掉):

myMap.flatMap{
  case (_, value: Map[String, Any]) =>  value.get("dst_ip")
  case _ => None
}

In your example, you only have one occurrence of a value which contains a Map with a value of interest, but you suggested there could be more of these. 在您的示例中,您只出现了一个值,该值包含具有感兴趣值的Map,但是您建议可以有更多的值。 Thus the flatMap which returns a list. 因此,flatMap返回一个列表。

To get the count of these instances, just call .size on the returned List. 要获取这些实例的数量,只需在返回的List上调用.size即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM