gpt4 book ai didi

hadoop - 使用 mapreduce 的每个组的前 K 个值

转载 作者:可可西里 更新时间:2023-11-01 16:32:08 25 4
gpt4 key购买 nike

我想编写一个 map-reduce 算法来为每个组查找前 N 个值(A 或 D 顺序)

Input data
a,1
a,9
b,3
b,5
a,4
a,7
b,1
c,1
c,9
c,-2
d,1
b,1
a,10
1,19

output type 1
a 1,4,7,9 ,10 , 19
b 1,,1,3,5
c -2,1,9
d 1

output type 2
a 19, 10 , 9,7,4,1
b 5,3,1,1
c 9,1,-2
d 1

前 3 的输出类型 1

a 1,4,7
b 1,,1,3
c -2,1
d 1

请指导我

最佳答案

您需要编写一个映射器,用逗号分隔输入行并生成一对 TextIntWritable:

Text('a,1') -> (mapper) -> Text('a'), IntWritable(1)

在 reducer 中,您将拥有组和值列表。您需要使用 priority queue 从列表中选择前 K 值:

// add all values to priority queue
PriorityQueue<Integer> queue = new PriorityQueue<Integer>();
for (IntWritable value : values)
queue.add(value.get());

// get first K elements from priority queue
String topK = String.valueOf(queue.poll());
for (int i = 0; i < K - 1; ++i)
topK += ", " + queue.poll();

关于hadoop - 使用 mapreduce 的每个组的前 K 个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25819379/

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