简体   繁体   English

在 Scala 中将 IndexedSequence[Map[String, Map[String, Int]]] 转换为 Map[String, Map[String, Int]]

[英]Converting an IndexedSequence[Map[String, Map[String, Int]]] to Map[String, Map[String, Int]] in Scala

I am a beginner in Scala and i have a case where i am trying to fetch a particular column in a dataframe and pass it to another method for further processing.我是 Scala 的初学者,我有一个案例,我试图获取数据帧中的特定列并将其传递给另一种方法进行进一步处理。

In this case, i am able to get the column values as String.在这种情况下,我能够将列值作为字符串获取。 I can do either of the two:我可以做以下两者之一:

  1. Convert String to Map[String, Map[String, Int]]将字符串转换为 Map[String, Map[String, Int]]

  2. Convert IndexedSequence[Map[String, Map[String, Int]]] to Map[String, Map[String, Int]]将 IndexedSequence[Map[String, Map[String, Int]]] 转换为 Map[String, Map[String, Int]]

The value in my dataframe is as follows:我的数据框中的值如下:

[endGoal -> [a -> 10, b -> 10], max -> [a -> 30, b -> 30]]

The code snippet am trying to use to convert the string to Map[String, Map[String, Int]] is as follows:我试图用来将字符串转换为 Map[String, Map[String, Int]] 的代码片段如下:

val map = recordArray(33) //Got the String
//Converting it to IndexedSequence[Map[String, Map[String, Int]]]
val result = for (line <- map; array = map.split(",").map(_.trim))
          yield Map(array.head -> array.tail.map(x => {val y = x.split(":"); (y(0).toString, y(1).toInt)}).toMap)

But the above code helps me get it converted only to IndexedSequence[Map[String, Map[String, Int]]]但是上面的代码帮助我将它转换为 IndexedSequence[Map[String, Map[String, Int]]]

Am stuck in getting it converted to Map[String, Map[String, Int]].我坚持将其转换为 Map[String, Map[String, Int]]。 Could you please explain me with an example on how to achieve the value in the required type?您能否举例说明如何实现所需类型的值?

Thank you.谢谢你。

Basically, i was taking a wrong approach to solve the problem.. My bad.. I was trying to do something like this:基本上,我采取了错误的方法来解决问题..我的不好..我试图做这样的事情:

data.foreachPartition { rddpartition =>
    rddpartition.foreach { record =>
        var recordString = record.mkString(“,”)
      var recordArray = recordString.split(“,”)

Which is where i ended up getting a string and then i was stuck trying to convert the string to Map[String, Map[String, Int]] and ended up getting IndexedSequence[Map[String, Map[String, Int]]].这是我最终得到一个字符串的地方,然后我试图将字符串转换为 Map[String, Map[String, Int]] 并最终得到 IndexedSequence[Map[String, Map[String, Int]]]。

Found 2 solutions:找到2个解决方案:

  1. From what i have tried above, adding:根据我上面的尝试,添加:

     result.toList.flatten.toMap

converts the IndexedSequence[Map[String, Map[String, Int]]] to Map[String, Map[String, Int]]将 IndexedSequence[Map[String, Map[String, Int]]] 转换为 Map[String, Map[String, Int]]

  1. I should directly get the Map[String, Map[String, Int]] from the dataframe by doing this:我应该通过这样做直接从数据框中获取 Map[String, Map[String, Int]] :

     data.foreachPartition { rddpartition => rddpartition.foreach { record => val uid = record.getAs[String]("uid") val advertiserId = record.getAs[String]("advertiserId") val time = record.getAs[Long]("time") val map = record.getAs[Map[String, Map[String, Int]]]("map") val crossDeviceUsers = record.getAs[mutable.WrappedArray[String]]("crossDeviceUsers")

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

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