简体   繁体   中英

Convert map to Case class

I have a Case Class:-

case class Messages (log_component: String)

I'm getting a kafka stream that's being mapped to get the desired result from -

        var a = scala.collection.mutable.Map[String, String]()

        for (x <- split_data) {
                    val split_stream = x.split("")

                    if (split_stream.length == 2) a += (split_stream(0) -> split_stream(1))
                    else a += (split_stream(0) -> "")
                  }

I want to convert the map data I'm getting in a to the case class provided. How to get the values I get after mapping and convert them to case class

Thank You

Example data

val of a is 
Map(log_component -> "HTTP")  (I'm getting this after mapping)

I want to store HTTP in Case class in log_component expression of it

I am not sure if I understand your question correctly.

Are you looking for this:

val res1 = Map("log_component" -> "HTTP").values.map(Messages(_))

This returns List(Messages(HTTP))

If you want to do this directly, your code could look like:

  val res1 = for (x <- split_data) {
    val split_stream = x.split(";") // I added here a character!
    if (split_stream.length == 2) 
      Messages(split_stream(1))
    else Messages("")
  }

This returns of course also List(Messages(HTTP))

I will try to answer the title of your question. The body (explanation) of your question is confused and confusing.

Let's say you have a class where all the constructor parameters are of the same type.

case class Person(fName :String, lName :String, room :String)

And let's say you have a Map that contains all the data needed to create a new instance of our class. (This is why the constructor parameters are the same type. The values in a Map are all the same type.)

val data = Map("fName" -> "Jo", "room" -> "A43", "lName" -> "Hu")

Now you can populate a Person from the Map values.

val nextPrsn = Person(data("fName"), data("lName"), data("room"))

Or you can use a more verbose form. The advantage being that the parameters can be given in any order.

val nextPrsn = Person( room  = data("room")
                     , lName = data("lName")
                     , fName = data("fName") )

But there's a problem here. What if the Map is populated incorrectly? Maybe a key->value pair is missing or not specified as expected.

val data = Map("lName" -> "Hu", "fName" -> "Jo", "Rm#" -> "A43")  //wrong "room" key

Now trying to access data("room") will throw an exception. Not good.

One relatively easy fix is to have a default for any missing values. You can attach a default to the Map , which will be the default for any/all missing values...

val data = Map("lName" -> "Hu", "fName" -> "Jo", "Rm#" -> "A43").withDefaultValue("N/A")

... or a different default can be specified for each constructor parameter.

val nextPrsn = Person( data.getOrElse("fName", "") )
                     , data.getOrElse("lName", "unknown")
                     , data.getOrElse("room",  "-") )

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