这是我的数据框:

+------------------------------------------
|value
+------------------------------------------
|[0.0, 1.0, 0.0, 7.0000000000000036, 0.0]
|[2.0000000000000036, 0.0, 2.9999999999999996, 4.0000000000000036, 5.000000000000002]
|[4.000000000000006, 0.0, 0.0, 6.000000000000006, 7.000000000000004]
+------------------------------------------

当我使用时:
dataFrame.withColumn("item_id", posexplode(dataFrame.col("value")))

我收到此错误:
org.apache.spark.sql.AnalysisException: The number of aliases supplied in the AS clause does not match the number of columns output by the UDTF expected 2 aliases but got item_id ;

因此,如何使用posexplode来“为每个元素在给定数组或map列中的位置创建一个新行”。

最佳答案

您可以将posexplodeselect一起使用,如下所示

dataframe.select($"value", posexplode($"value")).show(false)

它返回两个新列作为poscol
希望这可以帮助!

08-27 07:33