-2

I am new to scala and wanted to write a udf and method to check whether column in dataframe are integer or not . If its int then corresponding value should be true else false.

5
  • What have you already tried by yourself? Code? Commented Aug 9, 2018 at 11:05
  • def checkint(a:String)={ val i="[0-9]*".r a match{ case i()=>true case _=>false } } Commented Aug 9, 2018 at 11:17
  • 1
    Possible duplicate of Spark Data Frames - Check if column is of type integer Commented Aug 9, 2018 at 11:24
  • its in python .. i want in scala Commented Aug 9, 2018 at 11:25
  • Done:Create a udf val checkint=udf((a:String)=> {val i="[0-9]*".r a match{ case i()=>true case _=>false } } ) and able to get the desired output.:) Commented Aug 9, 2018 at 11:57

1 Answer 1

1

Create one udf which takes String as parameter and returns Boolean and inside the udf directly convert string to integer inside the Try block and later check if it is success or failure like below

import org.apache.spark.sql.functions._ import scala.util.Try import scala.util.Success val checkIntUDF = udf { (x: String) => val y = Try(x.toInt); y match { case Success(x) => true; case _ => false; } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.