从 RDD[string] 中获取字段

getting fields out of RDD[string]

我正在尝试从 RDD 中提取第 7 和第 9 个字段。我使用了以下代码

val logData = sc.textFile("path",2).map(item => {val comps=item.split(" "); (comps(6).toFloat, comps(8).toFloat)})

但是,我得到的输出是

(x1,y1)

(x2,y2)

(x3,y3)

因为我需要输出

x1 y1

x2 y2

x3 y3

谁能给我一个解决方案

你是指字符串,比如“0.54 0.123”?如果是这样,您可以替换:

(comps(6).toFloat, comps(8).toFloat)

s"${comps(6).toFloat} ${comps(8).toFloat}"

(或者您可以使用 f"${comps(6).toFloat}%0.3f ${comps(8).toFloat}%0.3f" 或类似的代替 s"..." 来更好地控制格式)。