My requirement is to create categories for age.I am trying to write multiple if conditions in UDF but its taking else condition. my code is below.
My data
1,Ashok,23,asd
2,Joi,27,dfs
3,Sam,30,dft
4,Bob,37,dat
my code
val sqlContext = new org.apache.spark.sql.SQLContext(sc)
import org.apache.spark.sql.functions._
import org.apache.spark.sql.types._
import org.apache.spark.sql._
import org.apache.spark.sql.SaveMode
import sqlContext.implicits._
val a = sc.textFile("file2.txt")
a.foreach(println)
val coder: (Int=>String)=(arg:Int)=>{if(arg>20&&arg<27) "20-27";if(arg>30&&arg<37) "30-37"; else "38+"}
val co = udf(coder)
val a2 = a1.select(col("Id"),col("Name"),col("Age"),col("Dpt"))
a2.withColumn("range",co(col("Age"))).show()
Output i am getting
1,Ashok,23,asd,38+
2,Joi,27,dfs,38+
3,Sam,30,dft,38+
4,Bob,37,dat,38+
For each row it is showing 38+, please suggest the syntax.