【发布时间】:2021-01-23 07:06:06
【问题描述】:
我只是想检查一个数据框的字段类型,所以我不得不写这个函数;
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]
}
例如:data.schema.head 是StringType,名称是col1
我用 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
我的预期是
colChek[IntegerType](data,"col1")
> :Boolean =false
colChek[StringType](data,"col1")
> :Boolean =false
但我得到了这个
colChek[IntegerType](data,"col1")
> :Boolean =true
这是什么原因造成的,如何解决,非常感谢
版本 火花2.4.5 scala的版本是2.11.12
【问题讨论】:
-
您能否具体说明问题是什么以及您期望的输出是什么
-
我已经更新了我的问题,非常感谢
标签: scala apache-spark apache-spark-sql