Search code examples
scalabigdata

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, e.g. 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


Solution

  • 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