spark saveAsTextFile

xiaoxiao2021-02-28  68

当我运行完一个Spark程序想把结果保存为saveAsTextFile,  结果使用Hadoop fs -ls la /qy/151231.txt后发现里面有一系列的part,好几千个。  原因:  运行Spark的时候把数据分成了很多份(partition),每个partition都把自己的数据保存在partxxx文件夹。  如果想保存为一份的话,就要:  先collect  或者

data.coalesce(1,true).saveAsTextFile() 1 1

You can also use repartition(1), which is just a wrapper for coalesce() with the suffle argument set to true.  但是如果你的数据很多,还是不要这样做了。

如果已经存了很多个part:  可以把大文件夹getmerge:

hadoop fs -getmerge /output/dir/on/hdfs/ /desired/local/output/file.txt hadoop fs -getmerge /qy/151231 /usr/qy/data/151231.txt 123 123

也可以:

data.coalesce(1,true).saveAsTextFile() 1 1

You can also use repartition(1), which is just a wrapper for coalesce() with the suffle argument set to true.  但是如果你的数据很多,还是不要这样做了。

如果已经存了很多个part:  可以把大文件夹getmerge:

hadoop fs -getmerge /output/dir/on/hdfs/ /desired/local/output/file.txt hadoop fs -getmerge /qy/151231 /usr/qy/data/151231.txt 123 123

也可以:

hadoop fs -cat /some/where/on/hdfs/job-output/part-r-* > TheCombinedResultOfTheJob.txt
转载请注明原文地址: https://www.6miu.com/read-96529.html

最新回复(0)