如何在 MapReduce Python Streaming 中对多个字段进行排序?

How to sort with multiple fields in MapReduce Python Streaming?

我在将 MapReduce 与流和 Python 结合使用时遇到排序问题。

这是一个更大问题的一部分,但它可以减少(没有双关语意:))到这个:

>> cat inputFile.txt
a       b       1       file1
a       b       2       file1
e       f       0       file2
d       c       3       file3
d       e       2       file4
a       c       5       file5
a       b       3       file1
d       c       2       file3
e       f       2       file2
a       c       4       file5
d       e       10      file4

第一列和第二列是键。

我希望 map 阶段的输出按这种方式排序(首先按第 1 列,然后按数字 2,然后按 3):

>>sort -k1,1 -k2,2 -k3n,3 inputFile.txt
a       b       1       file1
a       b       2       file1
a       b       3       file1
a       c       4       file5
a       c       5       file5
d       c       2       file3
d       c       3       file3
d       e       2       file4
d       e       10      file4
e       f       0       file2
e       f       2       file2

这里的第四列是关于我希望文件如何用于 reduce 步骤的提示,但是如果两个键在同一个文件中也没关系(只要每个键的所有实例都在一个文件中)单个文件)。 为此,我 运行 以下命令:

hadoop jar /usr/lib/hadoop/hadoop-streaming.jar -D stream.num.map.output.key.fields=2 -D mapred.text.key.comparator.options="-k3,3" -D mapred.text.key.partitioner.options="-k3,3" -mapper cat -reducer cat -input /user/hadoop/inputFile.txt -output /user/hadoop/output

此命令的输出未排序。例如:

>>cat output/part-00066
a       b       2       file1
a       b       3       file1
a       b       1       file1

备注:

这就像我缺少的一些非常基本的东西,我在这里做错了什么?

非常感谢您的帮助!

在尝试了几乎所有可能的组合后,我发现这行得通:

    hadoop jar /usr/lib/hadoop/hadoop-streaming.jar \
    -D \ 
 mapred.output.key.comparator.class=org.apache.hadoop.mapred.lib.KeyFieldBasedComparator\ 
-D stream.num.map.output.key.fields=4 \
    -D mapred.text.key.partitioner.options=-k1,2 \
    -D mapred.text.key.comparator.options=-"-k1,1 -k2,2 -k3n,3" \
    -input /user/hadoop/inputFile.txt \
    -output /user/hadoop/output \
    -mapper cat -reducer cat \
    -partitioner org.apache.hadoop.mapred.lib.KeyFieldBasedPartitioner

可以找到进一步的解释 here:

关键(同样,没有双关语意:))是使用 KeyFieldBasedPartitioner 作为分区程序。