gpt4 book ai didi

sorting - 如何在 hadoop 的洗牌/排序阶段进行数字排序?

转载 作者:可可西里 更新时间:2023-11-01 14:10:56 28 4
gpt4 key购买 nike

数据看起来像这样,第一个字段是一个数字,

3 ...
1 ...
2 ...
11 ...

我想根据第一个字段按数字而不是按字母顺序对这些行进行排序,这意味着排序后它应该如下所示,

1 ...
2 ...
3 ...
11 ...

但是 hadoop 一直给我这个,

1 ...
11 ...
2 ...
3 ...

如何改正?

最佳答案

假设您正在使用 Hadoop Streaming,您需要使用 KeyFieldBasedComparator 类。

  1. -D mapred.output.key.comparator.class=org.apache.hadoop.mapred.lib.KeyFieldBasedComparator 应该添加到流命令

  2. 您需要提供使用 mapred.text.key.comparator.options 所需的排序类型。一些有用的是 -n:数字排序,-r:反向排序

示例:

使用以下代码创建身份映射器和缩减器

这是 ma​​pper.pyreducer.py

#!/usr/bin/env python
import sys
for line in sys.stdin:
print "%s" % (line.strip())

这是input.txt

1
11
2
20
7
3
40

这是Streaming命令

$HADOOP_HOME/bin/hadoop  jar $HADOOP_HOME/hadoop-streaming.jar 
-D mapred.output.key.comparator.class=org.apache.hadoop.mapred.lib.KeyFieldBasedComparator
-D mapred.text.key.comparator.options=-n
-input /user/input.txt
-output /user/output.txt
-file ~/mapper.py
-mapper ~/mapper.py
-file ~/reducer.py
-reducer ~/reducer.py

你会得到所需的输出

1   
2
3
7
11
20
40

注意:

  1. 我使用了简单的一键输入。但是,如果您有多个键和/或分区,则必须根据需要编辑 mapred.text.key.comparator.options。由于我不知道你的用例,我的例子仅限于此

  2. 需要身份映射器,因为您至少需要一个映射器才能运行 MR 作业。

  3. 需要 Identity reducer,因为如果它是纯 map only 作业,则洗牌/排序阶段将无法工作。

关于sorting - 如何在 hadoop 的洗牌/排序阶段进行数字排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13331722/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com