如何产生的RDD写入到星火蟒蛇csv文件 [英] How to write the resulting RDD to a csv file in Spark python
问题描述
我有一个结果RDD labelsAnd predictions = testData.map(拉姆达LP:lp.label)的.zip(predictions)
。这有输出格式为:
I have a resulting RDD labelsAndPredictions = testData.map(lambda lp: lp.label).zip(predictions)
. This has output in this format:
[(0.0, 0.08482142857142858), (0.0, 0.11442786069651742),.....]
我要的是创建有一列的CSV文件标签
(在上面的输出元组的第一部分),一个用于 predictions
(元组输出的第二部分)。但我不知道如何使用Python写一个CSV文件中的火花。
What I want is to create a CSV file with one column for labels
(the first part of the tuple in above output) and one for predictions
(second part of tuple output). But I don't know how to write to a CSV file in Spark using Python.
我怎样才能创建一个CSV上述输出文件?
How can I create a CSV file with the above output?
推荐答案
只是地图
的RDD(的行 labelsAnd predictions
)为字符串(该CSV的线),然后使用 rdd.saveAsTextFile()
。
Just map
the lines of the RDD (labelsAndPredictions
) into strings (the lines of the CSV) then use rdd.saveAsTextFile()
.
def toCSVLine(data):
return ','.join(str(d) for d in data)
lines = labelsAndPredictions.map(toCSVLine)
lines.saveAsTextFile('hdfs://my-node:9000/tmp/labels-and-predictions.csv')
这篇关于如何产生的RDD写入到星火蟒蛇csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!