gpt4 book ai didi

sorting - setOutputKeyComparator - Mapreduce 二次排序(ValueGrouping 之后)

转载 作者:行者123 更新时间:2023-12-02 20:12:44 25 4
gpt4 key购买 nike

我只是在 AskUbuntu 数据集上尝试使用 Mapreduce 程序。

我的映射器输出是

语法

Key: TAG-<TAG_NAME>-<PARAM>  Value: <PARAM>-<Count>

示例:
Key - TAG-windows-QUE Value - QUE-1241
Key - TAG-windows-VIEWS Value - VIEWS-4369
Key - TAG-windows-QUE Value - QUE-1
Key - TAG-windows-VIEWS Value - VIEWS-1

我使用 根据 key 的前三个字符对其进行分区分区器 .
IE。标签

我也在使用 值分组比较器 对一组值进行分组
IE。标签窗口
public static class ValueGroupingComparator implements RawComparator<Text> {

/*value grouping comparator will group by the first few letters of the key till a second hyphen (“-”) symbol is found. */
public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
String sOne = new String(b1);
String sTwo = new String(b2);

// return new Character((char)b1[0]).compareTo((char)b2[0]);
return sOne.substring(0, sOne.indexOf('-', 4)).compareTo(
sTwo.substring(0, sTwo.indexOf('-', 4)));
}

public int compare(Text o1, Text o2) {
return compare(o1.getBytes(), 0, o1.getLength(), o2.getBytes(), 0,
o2.getLength());
}
}

*

Then i am in need of sorting the keys based on in key using KeyComparator.



*

该 api 传达的信息是
setOutputKeyComparator 可以结合使用来模拟对值的二次排序。
http://hadoop.apache.org/common/docs/current/api/org/apache/hadoop/mapred/JobConf.html

我对 的预期输入 reducer
Key - TAG-windows-QUE 
Value - 1241-QUE,1-QUE,4369-VIEWS,1-VIEWS

这样我就可以从我的 Reducer 输出以下内容
Key - TAG-windows Value - QUE-1242, VIEWS-4370

我尝试了以下 KeyComparator .但我无法达到我的预期输出
public static class KeyComparator extends WritableComparator {
public KeyComparator() {
super(Text.class);
}

public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {

int hypen = '-';
int s1Ind = 0;
int s2Ind = 0;
for (int i = 4; i < b1.length; i++) {
if (b1[i] == hypen) {
s1Ind = i;
break;
}
}

for (int i = 4; i < b2.length; i++) {
if (b2[i] == hypen) {
s2Ind = i;
break;
}
}

if (s1Ind == 0 || s2Ind == 0)
System.out.println(s1Ind + "<->" + s2Ind);

int compare = compareBytes(b1, s1, s1Ind, b2, s2, s2Ind);
if (compare == 0) {
return compareBytes(b1, s1Ind + 1, l1 - s1Ind + 2, b2,
s2Ind + 1, l2 - s2Ind + 2);
}
return compare;
}
}

在这里需要 hadoop mapreduce 专家的帮助。

最佳答案

我在 stackoverflow 中找到了与我的问题相关的以下两个链接。

Secondary sorting in Map-Reduce

MapReduce (secondary) sorting / filtering - how?

让我试试运气。

关于sorting - setOutputKeyComparator - Mapreduce 二次排序(ValueGrouping 之后),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11763523/

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