I just want to check a data frame's filed type, so I had to write this function;
import scala.refelect.runtime.universe.TypeTag
import org.apache.spark.sql.DataFrame
def colCheck[T:TypeTag](data:DataFrame,colName:String):Boolean={
data.schema.exists(f->f.name==colName&f.dataType.isInstanceOf[T]
}
eg:data.schema.head is StringType
and name is col1
I do this used spark-shell
import org.apache.spark.sql.types.{IntegerType,StringType}
val data:DataFrame=spark.createDataFrame(List(("",1),("",2))).toDF("col1","col2")
data.schema.head.dataType.isInstanceOf[StringType]
> :Boolean =true # write result
data.schema.head.dataType.isInstanceOf[IntegerType]
> :Boolean =false # write result
colChek[IntegerType](data,"col1")
> :Boolean =true
my expected is
colChek[IntegerType](data,"col1")
> :Boolean =false
colChek[StringType](data,"col1")
> :Boolean =false
but I got this
colChek[IntegerType](data,"col1")
> :Boolean =true
what causes this to happen and how to fix it, thanks very much
version spark2.4.5 scala's version is 2.11.12