我有一个 DataFrame 命令:
+-----------------+-----------+--------------+
| Id| Order | Gender|
+-----------------+-----------+--------------+
| 1622|[101330001]| Male|
| 1622| [147678]| Male|
| 3837| [1710544]| Male|
+-----------------+-----------+--------------+
我想对 ID 和性别进行分组,然后聚合订单。
我正在使用 org.apache.spark.sql.functions 包,代码如下:
DataFrame group = orders.withColumn("orders", col("order"))
.groupBy(col("Id"), col("Gender"))
.agg(collect_list("products"));
但是,由于列 Order 的类型为数组,因此我收到此异常,因为它需要原始类型:
User class threw exception: org.apache.spark.sql.AnalysisException: No handler for Hive udf class org.apache.hadoop.hive.ql.udf.generic.GenericUDAFCollectList because: Only primitive type arguments are accepted but array<string> was passed as parameter 1
我查看了包,有数组的排序函数,但没有聚合函数。知道怎么做吗?谢谢。
在这种情况下,您可以定义自己的函数并将其注册为 UDF
val userDefinedFunction = ???
val udfFunctionName = udf[U,T](userDefinedFunction)
然后,而不是然后将该列传递到该函数内,以便将其转换为原始类型,然后将其传递到 with Columns 方法中。
像这样的事情:
val dataF:(Array[Int])=>Int=_.head
val dataUDF=udf[Int,Array[Int]](dataF)
DataFrame group = orders.withColumn("orders", dataUDF(col("order")))
.groupBy(col("Id"), col("Gender"))
.agg(collect_list("products"));
我希望它有效!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)