我是Scala和Spark的新手,我已经尝试过以下代码,但无法正常工作。谁能帮助我(仅在Scala中没有数据框架)帮助我解决问题。之后,我必须再做一些转换
代码:
val file = sc.textFile("winners.csv")
file.map(_.split(",")).map{x=>if(x(0)==x(2)) x(1) else if(x(1)==x(2)) x(0), x(2)}
输入: Team_1,Team_2,优胜者
团队A,团队B,团队B
Team_C,Team_D,Team_C
团队B,团队C,团队B
Team_A,Team_D,Team_A
Team_D,Team_B,并列
输出:
失败者,获胜者
团队A,团队B
团队D,团队C
Team_C,Team_B
团队D,团队A
不需要搭配
答案 0 :(得分:2)
// Assuming `textFile` reads the lines to a Seq[String]
val file = sc.textFile("winners.csv")
file
.tail
.map(_.split(","))
.filter(_(2) != "Tied")
.map(x => if (x(0) == x(2)) s"${x(1)},${x(0)}" else s"${x(0)},${x(1)}")
答案 1 :(得分:2)
If
杂乱无章,请尽可能使用match:
file.map(_.split(",")).collect {
case Array(a, b, c) if a == c => s"$b,$c"
case Array(a, b, c) if b == c => s"$a,$c"
}