0

我只是在 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

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

我也使用ValueGroupingComparator对一组值进行分组,即。标签窗口

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());
        }
    }

*

然后我需要使用 KeyComparator 根据 in 键对键进行排序。

*

该 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 专家的帮助。

4

1 回答 1

1

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

Map-Reduce 中的二次排序

MapReduce(二级)排序/过滤 - 如何?

让我试试运气。

于 2012-08-17T19:21:44.013 回答