如何在没有数据框的情况下在Spark中执行列表达式 [英] How to execute Column expression in spark without dataframe

查看:81
本文介绍了如何在没有数据框的情况下在Spark中执行列表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我仅使用Literal(没有数据框列),有什么方法可以评估我的Column表达式.

Is there any way that I can evaluate my Column expression if I am only using Literal (no dataframe columns).

例如,类似:

val result: Int = someFunction(lit(3) * lit(5))
//result: Int = 15

import org.apache.spark.sql.function.sha1
val result: String = someFunction(sha1(lit("5")))
//result: String = ac3478d69a3c81fa62e60f5c3696165a4e5e6ac4

我能够使用数据框进行评估

I am able to evaluate using a dataframes

val result = Seq(1).toDF.select(sha1(lit("5"))).as[String].first
//result: String = ac3478d69a3c81fa62e60f5c3696165a4e5e6ac4

但是有什么方法可以在不使用数据框的情况下获得相同的结果吗?

But is there any way to get the same results without using dataframe?

推荐答案

要评估文字列,您可以

To evaluate a literal column you can convert it to an Expression and eval without providing input row:

scala> sha1(lit("1").cast("binary")).expr.eval()
res1: Any = 356a192b7913b04c54574d18c28d46e6395428ab

只要函数是UserDefinedFunction,它将以相同的方式工作:

As long as the function is an UserDefinedFunction it will work the same way:

scala> val f = udf((x: Int) => x)
f: org.apache.spark.sql.expressions.UserDefinedFunction = UserDefinedFunction(<function1>,IntegerType,Some(List(IntegerType)))

scala> f(lit(3) * lit(5)).expr.eval()
res3: Any = 15

这篇关于如何在没有数据框的情况下在Spark中执行列表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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