简体   繁体   中英

The initial data metrics is Map[String, Any], and one of the data types in Any is WrappedArray(map(),map()). How do I get a value from map

I have some metrics data like below, it's Map[String, Any], I want to get the data from Map, eg I want to get non_unique -> 1 from metrics data.

Map(applicationId -> local-1673262860096, job_name -> dist_batch, tmst -> 1673262884352, measure_name -> duplication_measure, metrics -> WrappedArray( Map(metric_name -> total, metric_value -> 50), Map(metric_name -> duplicate, metric_value -> 1), Map(metric_name -> unique, metric_value -> 48), Map(metric_name -> non_unique, metric_value -> 1), Map(metric_name -> distinct, metric_value -> 49) ), measure_type -> Duplication, data_source -> source)

I try to use val metricToInvestigate= metrics.get("metrics").get , but find that metricToInvestigate becomes to Any type, but I don't know how to get the output like non_unique -> 1

You gotta cast it:

val metricsMap: Map[String, Int] = 
  metrics("metrics")
  .asInstanceOf[Seq[Map[String,Int]]]
  .map { m => m("metric_name") -> m("metric_value") }
  .toMap

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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