1

I wrote the following code to take some action based on a field value for each row:

import spark.implicits._ 
table.map(line =>
          column_names.map(column =>
                      if (line.getAs[Int](column)==0)
                          println("yes")
                      )
          )

However I encounter the following error:

java.lang.UnsupportedOperationException: No Encoder found for Unit
- array element class: "scala.runtime.BoxedUnit"
- root class: "scala.collection.immutable.List"

I understand that an encoder is required to convert objects and primitives in the Spark InternalRow, but the field that I'm trying to access contains an int, and based on my understanding the corresponding encoder should be available thanks to spark.implicits._.

Could you please tell me what am I missing and what should I do to fix it?

1 Answer 1

1

The problem is that your map-statement returns Unit (i.e. nothing), for which there is no encoder. If you just want to print, then use foreach instead:

table.foreach(line =>
  column_names.foreach(column =>
    if (line.getAs[Int](column)==0)
      println("yes")
  )
)

otherwise, return something (ensure that you also return the same type in the else-clause, e.g.

table.map(line =>
  column_names.map(column =>
    if (line.getAs[Int](column)==0) {
      println("yes")
      "yes"
     } else {
       "no"
     }
  )
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.