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.
- What have you already tried by yourself? Code?cchantep– cchantep2018-08-09 11:05:13 +00:00Commented Aug 9, 2018 at 11:05
- def checkint(a:String)={ val i="[0-9]*".r a match{ case i()=>true case _=>false } }user8613814– user86138142018-08-09 11:17:46 +00:00Commented Aug 9, 2018 at 11:17
- 1Possible duplicate of Spark Data Frames - Check if column is of type integerrennerj2– rennerj22018-08-09 11:24:20 +00:00Commented Aug 9, 2018 at 11:24
- its in python .. i want in scalauser8613814– user86138142018-08-09 11:25:17 +00:00Commented 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.:)user8613814– user86138142018-08-09 11:57:36 +00:00Commented Aug 9, 2018 at 11:57
Add a comment |
1 Answer
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; } }