SparkSql 中的存储过程/函数



有什么方法可以实现sql功能,如sparkSQL中的存储过程或函数?

我知道hbase中的hpl sql和协处理器。但是想知道火花中是否有类似的东西。

您可以考虑使用用户定义函数和内置函数

一个快速的例子

val dataset = Seq((0, "hello"), (1, "world")).toDF("id", "text")  
val upper: String => String = _.toUpperCase    
import org.apache.spark.sql.functions.udf
val upperUDF = udf(upper)
// Apply the UDF to change the source dataset
scala> dataset.withColumn("upper", upperUDF('text)).show

结果

| id| text|upper|
+---+-----+-----+
|  0|hello|HELLO|
|  1|world|WORLD|

我们无法在 SparkSql 中创建 SP/Functions。但是,最好的方法是像 CTE 一样创建一个临时表,并将这些表用于进一步使用。或者你可以在Spark中创建UDF函数。

最新更新