我有一个“数据集(行)”如下
+-----+--------------+
|val | history |
+-----+--------------+
|500 |[a=456, a=500]|
|800 |[a=456, a=500]|
|784 |[a=456, a=500]|
+-----+--------------+
此处val是“字符串”,历史记录是“字符串数组”。我正在尝试将val列中的内容添加到history列中,以便我的数据集如下所示:
+-----+---------------------+
|val | history |
+-----+---------------------+
|500 |[a=456, b=500, c=500]|
|800 |[a=456, b=500, c=800]|
|784 |[a=456, b=500, c=784]|
+-----+---------------------+
这里讨论了一个类似的问题https://stackoverflow.com/a/49685271/2316771,但是我不知道scala并且无法创建类似的java解决方案。
请帮我用Java实现
在Spark 2.4中(不是之前),您可以使用concat
函数合并两个数组。就您而言,您可以执行以下操作:
df.withColumn("val2", concat(lit("c="), col("val")))
.select(concat(col("history"), array(col("val2")));
注意:我第一次使用concat
concat字符串,第二次使用concat数组。array(col("val2"))
创建一个元素数组。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句