gpt4 book ai didi

java - 我如何使用java Spark转置csv数据

转载 作者:行者123 更新时间:2023-12-01 18:22:14 25 4
gpt4 key购买 nike

我正在使用 java Spark,我想知道是否可以转换下面给出的示例数据

Incremental Cost Number | Approver Names                          
---------------------------------------------------------------------------------
S703401 |Ryan P Cassidy|Christopher J Mattingly|Frank E
LaSota|Ryan P Cassidy|Anthony L Locricchio|Jason Monte

变成这样的事情。

Incremental Cost Number| Approver Names                          
-------------------------------------------
S703401 | Ryan P Cassidy
S703401 | Christopher J Mattingly
S703401 | Frank E LaSota
S703401 | Ryan P Cassidy
S703401 | Anthony L Locricchio
S703401 | Jason Monte

此外,我导入的文件是逗号分隔的 csv 文件,只是特定列包含多个由管道符号分隔的值。同样,如果我有多个增量成本数值。

最佳答案

如果您有多列,您可以执行如下操作

  import org.apache.spark.sql.functions._

val df = Seq(("S703401","Ryan P Cassidy|Christopher J Mattingly|Frank E
LaSota|Ryan P Cassidy|Anthony L Locricchio|Jason
Monte","xyz|mnp|abc")).toDF("Incremental Cost Number","Approver
Names","3rd Column")


df.withColumn("Approver Names", explode(split(col("Approver Names"), "\\|")))
.withColumn("3rd Column", explode(split(col("3rd Column"), "\\|")))
.show()


+-----------------------+--------------------+-----------+
|Incremental Cost Number| Approver Names| 3rd Column|
+-----------------------+--------------------+-----------+
| S703401|Ryan P Cassidy|Ch...|xyz|mnp|abc|
+-----------------------+--------------------+-----------+

+-----------------------+--------------------+----------+
|Incremental Cost Number| Approver Names|3rd Column|
+-----------------------+--------------------+----------+
| S703401| Ryan P Cassidy| xyz|
| S703401| Ryan P Cassidy| mnp|
| S703401| Ryan P Cassidy| abc|
| S703401|Christopher J Mat...| xyz|
| S703401|Christopher J Mat...| mnp|
| S703401|Christopher J Mat...| abc|
| S703401| Frank E LaSota| xyz|
| S703401| Frank E LaSota| mnp|
| S703401| Frank E LaSota| abc|
| S703401| Ryan P Cassidy| xyz|
| S703401| Ryan P Cassidy| mnp|
| S703401| Ryan P Cassidy| abc|
| S703401|Anthony L Locricchio| xyz|
| S703401|Anthony L Locricchio| mnp|
| S703401|Anthony L Locricchio| abc|
| S703401| Jason Monte| xyz|
| S703401| Jason Monte| mnp|
| S703401| Jason Monte| abc|
+-----------------------+--------------------+----------+

关于java - 我如何使用java Spark转置csv数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60282985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com