spark saveAsTextFile

来源:互联网 发布:js ide 编辑:程序博客网 时间:2024/06/06 19:54

当我运行完一个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.txthadoop fs -getmerge /qy/151231  /usr/qy/data/151231.txt
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

也可以:

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.txthadoop fs -getmerge /qy/151231  /usr/qy/data/151231.txt
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

也可以:

hadoop fs -cat /some/where/on/hdfs/job-output/part-r-* > TheCombinedResultOfTheJob.txt
0 0