在spark sql中需要等效的SQL IsNumeric函数 [英] Need the equivalent of SQL IsNumeric function in spark sql

查看:52
本文介绍了在spark sql中需要等效的SQL IsNumeric函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像我们有验证表达式是否为数字的 SQL ISNUMERIC 函数一样,我需要 Spark SQL 中是否有任何等效函数,我试图找到它但无法找到它.请问有人可以提供帮助或建议吗?

Like we have SQL ISNUMERIC Function which validates whether the expression is numeric or not , I need if there is any equivalent function in Spark SQL, I have tried to find it but couldn't get it. Please if someone can help or suggest for the same ?

推荐答案

尝试使用 spark udf,这种方法将帮助您克隆任何函数 -

Try using spark udf, this approach will help you clone any function -

scala> spark.udf.register("IsNumeric", (inpColumn: Int) => BigInt(inpColumn).isInstanceOf[BigInt])
res46: org.apache.spark.sql.expressions.UserDefinedFunction = UserDefinedFunction(<function1>,BooleanType,Some(List(IntegerType)))

scala> spark.sql(s""" select "ABC", IsNumeric(123) as IsNumeric_1  """).show(false)
+---+-----------+
|ABC|IsNumeric_1|
+---+-----------+
|ABC|true       |
+---+-----------+


scala> spark.sql(s""" select "ABC", IsNumeric("ABC") as IsNumeric_1  """).show(false)
+---+-----------+
|ABC|IsNumeric_1|
+---+-----------+
|ABC|null       |
+---+-----------+

这里,如果列值不是整数,上面的函数将返回 null.

Here, above function will return null if column value is not integer.

希望这会有所帮助.

这篇关于在spark sql中需要等效的SQL IsNumeric函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆