【发布时间】:2015-06-10 20:05:12
【问题描述】:
我是 Scala 和 Spark 的新手。我试图在地图转换期间返回多个键值对。我的输入数据是一个简单的 CSV 文件。
1、2、3 4、5、6 7、8、9我的 Scala 脚本如下所示。
class Key(_i:Integer, _j:Integer) {
def i = _i
def j = _j
}
class Val(_x:Double, _y:Double) {
def x = _x
def y = _y
}
val arr = "1,2,3".split(",")
for(i <- 0 until arr.length) {
val x = arr(i).toDouble
for(j <- 0 until arr.length) {
val y = arr(j).toDouble
val k = new Key(i, j)
val v = new Val(x, y)
//note that i want to return the tuples, (k, v)
}
}
我希望能够使用上面的 for 循环和数据结构来返回多个元组 (k, v)。类似于下面的代码。
val file = sc.textFile("/path/to/test.csv")
file.map(line => {
val arr = line.split(",")
for(i <- 0 until arr.length) {
val x = arr(i).toDouble
for(j <- (i+1) until arr.length) {
val y = arr(j).toDouble
val k = new Index(i,j)
val v = new Val(x,y)
(k,v)
}
}
}).collect //reduceByKey is not there, reduce is there, but not what i want
当我将上面的代码复制/粘贴到 lambda 表达式中(并在 Scala REPL shell 上运行)时,我收到以下错误:
错误:简单表达式的非法开始 val arr = line.split(",") ^我也意识到我仍然停留在命令式/过程式编程思维中,所以请多多包涵(以及 Scala/Spark 的新手)。
【问题讨论】:
标签: scala apache-spark scala-collections