Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.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
在spark scala函数中将列表作为参数传递会导致错误_Scala_Apache Spark - Fatal编程技术网

在spark scala函数中将列表作为参数传递会导致错误

在spark scala函数中将列表作为参数传递会导致错误,scala,apache-spark,Scala,Apache Spark,我有一个spark scala udf,它将一个参数作为dataframe的列,另一个参数作为List,但当我运行该函数时,它会抛出指向List参数的错误 类型不匹配,找到spark.sql.row,需要spark.sql.column 我运行udf的参数如下: 自定义项名称($“列名称”,列表名称) 请指导您需要使用要传递的列表定义多个UDF实例。 因为列表是本地scala变量,所以可以在调用之前执行(spark将udf发送给各个执行者) e、 g 您可以使用lit将常量值传递给udf,也可以

我有一个spark scala udf,它将一个参数作为dataframe的列,另一个参数作为List,但当我运行该函数时,它会抛出指向List参数的错误

类型不匹配,找到spark.sql.row,需要spark.sql.column

我运行udf的参数如下:

自定义项名称($“列名称”,列表名称)


请指导

您需要使用要传递的列表定义多个UDF实例。 因为列表是本地scala变量,所以可以在调用之前执行(spark将udf发送给各个执行者) e、 g


您可以使用
lit
将常量值传递给udf,也可以使用返回udf的方式定义方法(我的首选方式):


import org.apache.spark.sql.functions.liti Arnon我按照您的建议进行了更改,但出现了错误我的代码是,import org.apache.spark.sql.functions.lit df_col.withColumn(“newcol”),udf_getMaxMatch($“col”,lit(五十) )我在org.apache.spark.sql.catalyst.expressions.literal$.apply(literals.scala:77)…..上得到错误为=>java.lang.RuntimeException:不支持的文本类型类scala.collection.immutable.$colon$colon列表([12],[1234])。udfval udf的定义是什么{(inpStr:String,codeList:List[String])=>{def getMaxIter(inpStr:String,codeList:List[String],maxMatch:String):String={if(codeList.isEmpty){maxMatch}else{val-cmp=codeList.head if(cmp==inpStr.substring(0,cmp.length)){if(cmp.length>maxMatch.length){getMaxIter(inpStr,codeList.tail,cmp)}else{getMaxIter(inpStr{getMaxIter(inpStr,codeList.tail,maxMatch)}}如果(codeList.isEmpty){“}其他{getMaxIter(inpStr,codeList,”)}}抱歉我的错误-修复了答案
import org.apache.spark.sql.functions._
val df=List("A","B").toDF
def to_be_udf(s: String, l : List[String])=if (l.isEmpty) "" else "has values"
val udf1=udf((s:String) => to_be_udf (s,List("a")))
val udf2=udf((s:String) => to_be_udf (s,List()))
df.select(udf1($"value"),udf2($"value")).show()

+----------+----------+
|UDF(value)|UDF(value)|
+----------+----------+
|has values|          |
|has values|          |
+----------+----------+
def udf_name(List_name:List[String]) = {
  udf((name:String) => {
    // do something 
    List_name.contains(name)
  })
}

val List_name : List[String] = ???

df
  .withColumn("is_name_in_list", udf_name(List_name)($"column_name"))