Scala 以Map作为输入的Spark UDF
我正在创建一个UDF,如下所示:Scala 以Map作为输入的Spark UDF,scala,apache-spark,Scala,Apache Spark,我正在创建一个UDF,如下所示: UserDefinedFunction userDefinedFunction = functions.udf(String value, Map<String, Integer> map) -> { return map.get(value) }, DataTypes.IntegerType ); UserDefinedFunction UserDefinedFunction=functions.udf(字符串值,
UserDefinedFunction userDefinedFunction = functions.udf(String value, Map<String, Integer> map) -> {
return map.get(value)
}, DataTypes.IntegerType
);
UserDefinedFunction UserDefinedFunction=functions.udf(字符串值,映射)->{
返回map.get(值)
},DataTypes.IntegerType
);
我有一个java.util.Map
需要传递到这个函数中:
Dataset<Row> newDataset = oldDataset.withColumn("newColumn",
userDefinedFunction.apply(
col("valueColumnName"), <<pass java map here>>);
Dataset newDataset=oldDataset.withColumn(“newColumn”,
userDefinedFunction.apply(
col(“valueColumnName”),);
如何将这个java.util.Map
传递到UDF中
我尝试将Java映射转换为Scala映射,但typedLit
引发以下异常:
不支持的文本类型类scala.collection.immutable.Map$Map2 Map(…)
您有两个选项:使用spark.sql.function Map\u值将Map值提取到数组中,或使用udf。
对于udf,您可以这样做,这里我提取映射键“hello”对应的core值:
val df1 = Seq(
("1", Map("hello" -> "firas")),
("1", Map("hello2" -> "moadh", "hello" -> "firas2"))
).toDF("id", "map")
import org.apache.spark.sql.functions.{col, udf}
def mapUdf(value: String) = udf((map1: Map[String, String]) => map1(value))
df1.withColumn("mapValue", mapUdf(("hello"))(col("map")))
I get this:
+---+---------------------------------+---------
| id| map |mapValue|
+---+---------------------------------+---------
| 1| [hello -> firas] | firas|
| 1|[hello2 -> moadh,hello -> firas2]| firas2|
+---+---------------------------------+---------
您有两个选项:使用spark.sql.function map_值将map值提取到数组中,或使用udf。 对于udf,您可以这样做,这里我提取映射键“hello”对应的core值:
val df1 = Seq(
("1", Map("hello" -> "firas")),
("1", Map("hello2" -> "moadh", "hello" -> "firas2"))
).toDF("id", "map")
import org.apache.spark.sql.functions.{col, udf}
def mapUdf(value: String) = udf((map1: Map[String, String]) => map1(value))
df1.withColumn("mapValue", mapUdf(("hello"))(col("map")))
I get this:
+---+---------------------------------+---------
| id| map |mapValue|
+---+---------------------------------+---------
| 1| [hello -> firas] | firas|
| 1|[hello2 -> moadh,hello -> firas2]| firas2|
+---+---------------------------------+---------
我建议了一个解决方案,但我不确定你想做什么。这是一个关于使用Java Spark的问题。我想唯一的响应者没有注意到。我建议了一个解决方案,但我不确定你想做什么。这是一个关于使用Java Spark的问题。我想唯一的响应者没有注意到。我需要查找第v列的外部映射值关于此UDF问题的任何建议plz我需要在外部地图中查找列值关于此UDF问题的任何建议plz