我正在使用EMR步进功能来分析数据。我想存储已分析数据帧的计数,以确定是否可以将其保存为csv或镶木地板。我更喜欢CSV,但是如果大小太大,我将无法下载并在笔记本电脑上使用。我使用count()
方法将其存储到int变量limit
当我尝试使用以下代码时:
coalesce(1).write.format("text").option("header", "false").mode("overwrite").save("output.txt")
它说:
int没有任何称为write的属性
是否有将整数或字符串写入文件的方法,以便我可以在s3存储桶中打开它并在EMR步骤运行后进行检查?
您可以parallelize
int变量创建一个rdd
,然后使用.saveAsTextFile
]写入HDFS>
,然后以df.show() #+---+ #| _1| #+---+ #| a| #| b| #+---+ limit=df.count() spark.sparkContext.parallelize([limit]).coalesce(1).saveAsTextFile("<path>") #content of file #cat <path>/part-00000 #2
其他方法是从
dataframe
创建count variable
csv
格式写为标题false
。from pyspark.sql.types import *
spark.createDataFrame(spark.sparkContext.parallelize([limit]),IntegerType()).coalesce(1).write.format("csv").option("header", "false").mode("overwrite").save("<path>")
#or in text format
spark.createDataFrame(spark.sparkContext.parallelize([limit]),StringType()).coalesce(1).write.format("text").mode("overwrite").save("<path>")
#cat part-*
#2