合并mapreduce作业的输出文件

6ovsh4lw  于 2021-06-04  发布在  Hadoop
关注(0)|答案(4)|浏览(526)

我用python编写了一个mapper和reducer,并使用hadoop流在amazon的elasticmapreduce(emr)上成功地执行了它。
最终结果文件夹包含三个不同文件part-00000、part-00001和part-00002中的输出。但我需要一个文件的输出。有办法吗?
以下是我的Map程序代码:

  1. # !/usr/bin/env python
  2. import sys
  3. for line in sys.stdin:
  4. line = line.strip()
  5. words = line.split()
  6. for word in words:
  7. print '%s\t%s' % (word, 1)

这是我的减速机代码

  1. # !/usr/bin/env python
  2. from operator import itemgetter
  3. import sys
  4. current_word = None
  5. current_count = 0
  6. word = None
  7. max_count=0
  8. for line in sys.stdin:
  9. line = line.strip()
  10. word, count = line.split('\t', 1)
  11. try:
  12. count = int(count)
  13. except ValueError:
  14. continue
  15. if current_word == word:
  16. current_count += count
  17. else:
  18. if current_word:
  19. # write result to STDOUT
  20. if current_word[0] != '@':
  21. print '%s\t%d' % (current_word, current_count)
  22. if count > max_count:
  23. max_count = count
  24. current_count = count
  25. current_word = word
  26. if current_word == word:
  27. print '%s\t%s' % (current_word, current_count)

我需要这个输出作为一个单独的文件。

3htmauhk

3htmauhk1#

我最近也遇到了同样的问题,实际上combiner应该完成这个任务,但是我无法实现。我所做的是;
步骤1:mapper1.py减速器1.py
输入:s3://../data/
输出s3://../small\u输出/
第二步:mapper2.py减速器2.py
输入s3://../数据/
输出:s3://../output2/
第三步:mapper3.py减速器3.py
输入:s3://../output2/
输出:s3://../最终输出/
我假设我们需要step1的输出作为step3的单个文件。
在mapper2.py的顶部,有这样的代码;

  1. if not os.path.isfile('/tmp/s3_sync_flag'):
  2. os.system('touch /tmp/s3_sync_flag')
  3. [download files to /tmp/output/]
  4. os.system('cat /tmp/output/part* > /tmp/output/all')

如果是块,则检查是否有多个Map器执行。

展开查看全部
mctunoxg

mctunoxg2#

一种非常简单的方法(假设是linux/unix系统):

  1. $ cat part-00000 part-00001 part-00002 > output
m0rkklqb

m0rkklqb3#

对小数据集/处理使用单个reduce,或对作业的输出文件使用getmerge选项。

k5hmc34c

k5hmc34c4#

我对上述问题的解决方案是执行以下hdfs命令:

  1. hadoop fs -getmerge /hdfs/path local_file

其中/hdfs/path是包含作业输出的所有部分(part-*****)的路径。hadoop fs的-getmerge选项将所有作业输出合并到本地文件系统上的单个文件中。

相关问题