有人可以解释spark rdd.map如何决定从文本文件中读取由单词组成的行吗?

问题描述 投票:0回答:1

我有当前代码

lines = sc.textFile("file:///SparkCourse/ml-100k/u.data")
ratings = lines.map(lambda x: x.split()[2])
result = ratings.countByValue()

据此,“x”最终代表文本文件中的一行: 文本文件摘录:

196 242 3   881250949
186 302 3   891717742
22  377 1   878887116
244 51  2   880606923
166 346 1   886397596

地图为什么/如何将其拆分为每行并返回一行作为“x”?

  • 与根据空白返回每个单独的文字相反?
  • map是否总是寻找换行?我们可以指定任何其他区别吗?
python apache-spark functional-programming
1个回答
0
投票
这就是 sc.textFile 的作用:将文件读入字符串 RDD:

SparkContext.textFile(name, minPartitions=None, use_unicode=True)[source] Read a text file from HDFS, a local file system (available on all nodes), or any Hadoop-supported file system URI, and return it as an RDD of Strings
参见

文档

并且

RDD.map

重新调整新的RDD,将函数应用于RDD的每个元素。在这种情况下,元素是文件中的整行。

© www.soinside.com 2019 - 2024. All rights reserved.