Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 参数数目可变的Spark Sql udf_Scala_Apache Spark_Apache Spark Sql - Fatal编程技术网

Scala 参数数目可变的Spark Sql udf

Scala 参数数目可变的Spark Sql udf,scala,apache-spark,apache-spark-sql,Scala,Apache Spark,Apache Spark Sql,我想要一个用于Spark Sql的concat函数。 我已经写了一个udf作为 sqlContext.udf.register("CONCAT",(args:String*)=>{ String out="" for(arg<-args) { out+=arg } out }) sqlContext.sql("select col1,col2,CONCAT(col1,col2) from testtable") sqlContext.udf.register

我想要一个用于Spark Sql的concat函数。 我已经写了一个udf作为

sqlContext.udf.register("CONCAT",(args:String*)=>{
 String out=""
 for(arg<-args)
  {
    out+=arg
  }
 out
})

sqlContext.sql("select col1,col2,CONCAT(col1,col2) from testtable")
sqlContext.udf.register(“CONCAT”,(args:String*)=>{
String out=“”

对于(arg如果您只想使用原始SQL连接列,则根本不需要自定义UDF。
CONCAT
函数已经存在:

val df = sc.parallelize(List(("a", "b", "c"))).toDF("x", "y", "z")
df.registerTempTable("df")
sqlContext.sql("SELECT CONCAT(x, y, z) AS xyz FROM df").show

// +---+
// |xyz|
// +---+
// |abc|
// +---+
从1.5.0开始,您可以直接使用
concat
/
concat\u ws
功能:

import org.apache.spark.sql.functions.{concat, concat_ws}

df.select(concat_ws("-", $"x", $"y", $"z").alias("x-y-z")).show
// +-----+
// |x-y-z|
// +-----+
// |a-b-c|
// +-----+

df.select(concat($"x", $"y", $"z").alias("xyz")).show

// +---+
// |xyz|
// +---+
// |abc|
// +---+

另请参见

您可以使用
struct
函数执行此操作,如下所示:

val myUDF = udf {
  (r: Row) => r.toSeq.map(...) // the "r" row contains your arguments
}
val df = ....
df.select(col("col1"), myUDF(struct(col("col2"), col("col3"), col("col4"), ...)))

你能提供问题中的例外情况吗?