我知道这是一个旧帖子。但是,为了帮助别人寻找同样的,这里是我怎么写的两列RDD到一个CSV文件中PySpark 1.6.2
的RDD:
>>> rdd.take(5)
[(73342, u'cells'), (62861, u'cell'), (61714, u'studies'), (61377, u'aim'), (60168, u'clinical')]
现在代码:
# First I convert the RDD to dataframe
from pyspark import SparkContext
df = sqlContext.createDataFrame(rdd, ['count', 'word'])
的DF:
>>> df.show()
+-----+-----------+
|count| word|
+-----+-----------+
|73342| cells|
|62861| cell|
|61714| studies|
|61377| aim|
|60168| clinical|
|59275| 2|
|59221| 1|
|58274| data|
|58087|development|
|56579| cancer|
|50243| disease|
|49817| provided|
|49216| specific|
|48857| health|
|48536| study|
|47827| project|
|45573|description|
|45455| applicant|
|44739| program|
|44522| patients|
+-----+-----------+
only showing top 20 rows
现在写为CSV
# Write CSV (I have HDFS storage)
df.coalesce(1).write.format('com.databricks.spark.csv').options(header='true').save('file:///home/username/csv_out')
P.S:我只是一个初学者从这里的帖子学习Stackoverflow。所以我不知道这是否是最好的方法。但它对我有用,我希望它能帮助别人!
如何映射为字符串?我的意思是在'toLine(data)'里写什么? –
对不起,我以为你知道基本的Python。我已将它添加到答案中。 –
正如有疑问,那么这将保存'csv'文件吗?在代码所在的同一个目录中?我可以将它保存到其他目录(使用'saveAsTextFile('/ home/files/labels-and-predictions.csv')')吗? –