0

I am trying to convert string to scala type by using asInstanceof method, while executing this am getting below exception java.lang.ClassCastException: java.lang.String cannot be cast to scala.Tuple2

my code as below

import org.apache.spark.sql.Column
import org.apache.spark.sql.functions.col
val cond : String = "(null, col(field).isNotNull)"  // Will get this condition from properties file.
type mutliColumnType = (Column, Column)

def condition( value : String , field : String = "somefield") : mutliColumnType = {
value match {
   case "a" => (null, col(field).isNull) 
   case _ => convertStringToMutliColumnType(cond) //cond.asInstanceOf[mutliColumnType]  
  }
}

condition("a") // returns data
condition("ab") // Exception 

How can we convert string to multiColumnType here ?

UPDATE:

Currently I written below code snippet to parse string to mutliColumnType :

def convertStringToMutliColumnType(cond : String) : mutliColumnType = {
    val colArray=cond.trim.substring(1, cond.length-1).split(",")
    (col(colArray(0)), col(colArray(1)))
}

1 Answer 1

4

You seem to be wanting asInstanceOf to evaluate a string as Scala code. This isn't really the same thing as "casting", it's not what asInstanceOf does, and in fact evaluating strings as code is not something Scala supports at all (apart from some internal APIs and ill-advised libraries like the now-defunct twitter-util-eval).

It's quite unclear what you're trying to do here, but your options are basically either to write a parser that takes strings and returns mutliColumnType values (which is a lot of work and almost certainly a bad idea), or just not to do this—i.e. to use Scala code where you need Scala code and strings where you need strings.

As a footnote: asInstanceOf is only really useful for downcasting (when you've lost type information and have something typed as Any that you as the programmer "know" is actually a String or whatever), and even then it should be considered an advanced technique that's unsafe and not generally idiomatic. Any time you write asInstanceOf you're telling the compiler to get out of the way because you know better, and in my experience you'll generally be wrong.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I realised that asInstanceOf is not to use here. I am not getting any idea how can I parse cond to mutliColumnType. Could you please give me an approach to parse this?
@Giri In general I don't think "just don't do that" is a helpful SO answer, but in this case I don't think there's a better one. I'd suggest opening another question that describes what you're trying to accomplish at a higher level.

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.