Spark 中 Double/Int 值的 Null 检查

Dar*_*han 3 hadoop hive scala apache-spark

我是 Spark 新手,如何检查 scala 或 Spark 中 Double 中的 Null 值和 Int 值。

就像字符串一样,我们可以这样做:

val value = (FirstString.isEmpty()) match {
          case true => SecondString
          case _    => FirstString
        }
Run Code Online (Sandbox Code Playgroud)

我搜索了很多,但只找到了字符串值。您也可以建议我使用其他数据类型吗?

提前致谢。

Sam*_*mar 6

null 仅适用于 Scala 中的 AnyRef(即非基本类型)类型。AnyVal 类型不能设置为 null。

例如:

// the below are AnyVal(s) and wont compile
val c: Char = null
val i: Int = null
val d: Double = null  
Run Code Online (Sandbox Code Playgroud)

String 是 AnyRef,因此可以为 null:

// this is ok!
val c: String = null 
Run Code Online (Sandbox Code Playgroud)

这就是为什么将空值与 Int/Double 类型进行模式匹配是不可能的:

// wont compile!
null match {
        case a:Int => "is a null Int"
        case _ => "something else"
        }
Run Code Online (Sandbox Code Playgroud)