简体   繁体   中英

Type Mismatch in scala case match

Trying to create multiple dataframes in a single foreach, using spark, as below

I get values delivery and click out of row.getAs("type") , when I try to print them.

val check = eachrec.foreach(recrd => recrd.map(row => {
  row.getAs("type") match {
    case "delivery" => val delivery_data = delivery(row.get(0).toString,row.get(1).toString)
    case "click" => val click_data = delivery(row.get(0).toString,row.get(1).toString)
    case _ => "not sure if this impacts"
}})
)

but getting below error:

Error:(41, 14) type mismatch; found: String("delivery") required: Nothing case "delivery" => val delivery_data = delivery(row.get(0).toString,row.get(1).toString) ^

My plan is to create dataframe using todf() once I create these individual delivery objects referenced by delivery_data and click_data by:

delivery_data.toDF() and click_data.toDF().
  1. Please provide any clue regarding the error above (in match case).
  2. How can I create two df's using todf() in val check?

val declarations make your first 2 case s return type to be unit , but in the third case you return a String

for instance, here the z type was inferred by the compiler, Unit :

def x = {
    val z: Unit = 3 match {
      case 2 => val a = 2
      case _ => val b = 3
    }
  }

I think you need to cast this match clause to String. row.getAs("type").toString

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