3

下面是我的 Spark 1.6 代码。我正在尝试将其转换为 Spark 2.3,但使用拆分时出现错误。

火花1.6代码:

val file = spark.textFile(args(0))
val mapping = file.map(_.split('/t')).map(a => a(1))
mapping.saveAsTextFile(args(1))

火花2.3代码:

val file = spark.read.text(args(0))
val mapping = file.map(_.split('/t')).map(a => a(1)) //Getting Error Here
mapping.write.text(args(1))

错误信息:

value split is not a member of org.apache.spark.sql.Row
4

1 回答 1

5

spark.textFilewhich 返回 a不同RDDspark.read.text返回的 aDataFrame本质上是 a RDD[Row]。您可以map使用部分函数执行,如下例所示:

// /path/to/textfile:
// a    b   c
// d    e   f

import org.apache.spark.sql.Row

val df = spark.read.text("/path/to/textfile")

df.map{ case Row(s: String) => s.split("\\t") }.map(_(1)).show
// +-----+
// |value|
// +-----+
// |    b|
// |    e|
// +-----+
于 2019-08-04T14:07:00.677 回答