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

查看:236
本文介绍了需要与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天全站免登陆