您的代码的根本问题是您使用的函数。函数传递给map
应该采用一个参数。使用以下任一方法:
reader.map(lambda x: x[1])
or just:
reader.values()
只要keyClass
and valueClass
匹配数据,这应该是您在这里需要的全部内容,并且不需要额外的类型转换(这是由内部处理的)sequenceFile
)。用 Scala 写:
Welcome to
____ __
/ __/__ ___ _____/ /__
_\ \/ _ \/ _ `/ __/ '_/
/___/ .__/\_,_/_/ /_/\_\ version 2.1.0
/_/
Using Scala version 2.11.8 (OpenJDK 64-Bit Server VM, Java 1.8.0_111)
Type in expressions to have them evaluated.
Type :help for more information.
scala> :paste
// Entering paste mode (ctrl-D to finish)
sc
.parallelize(Seq(
("foo", """{"foo": 1}"""), ("bar", """{"bar": 2}""")))
.saveAsSequenceFile("example")
// Exiting paste mode, now interpreting.
用 Python 阅读:
Welcome to
____ __
/ __/__ ___ _____/ /__
_\ \/ _ \/ _ `/ __/ '_/
/__ / .__/\_,_/_/ /_/\_\ version 2.1.0
/_/
Using Python version 3.5.1 (default, Dec 7 2015 11:16:01)
SparkSession available as 'spark'.
In [1]: Text = "org.apache.hadoop.io.Text"
In [2]: (sc
...: .sequenceFile("example", Text, Text)
...: .values()
...: .first())
Out[2]: '{"bar": 2}'
Note:
旧版 Python 版本支持元组参数解包:
reader.map(lambda (_, v): v)
不要使用它对于应该向前兼容的代码。