无法将 pyspark 输出发送到本地文件系统中的文件

cannot send pyspark output to a file in the local file system

我是 运行 spark 上的 pyspark 作业(单节点,独立)并尝试将输出保存在本地文件系统的文本文件中。

input = sc.textFile(inputfilepath)
words = input.flatMap(lambda x: x.split())
wordCount = words.countByValue()

wordCount.saveAsTextFile("file:///home/username/output.txt")

我收到一条错误消息

AttributeError: 'collections.defaultdict' object has no attribute 'saveAsTextFile'

基本上无论我添加到 'wordCount' 对象,例如 collect() 或 map() 它 returns 同样的错误。当输出到终端(使用 for 循环)时,代码可以正常工作,但我无法确定将输出发送到文件时缺少什么。

您调用的 countByValue() method 正在返回一个字数字典。这只是一个标准的 python 字典,没有任何可用的 Spark 方法。

您可以使用your favorite method将字典保存到本地。