我只是在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根据键的前三个字符对其进行分区。即标签
此外,我正在使用ValueGroupingComparator对一组值进行分组即TAG窗口
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());
}
}
*
然后我需要根据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中发现了与我的问题相关的以下两个链接。
Map Reduce 中的二次排序
MapReduce(辅助)排序/过滤-如何进行?
让我试试运气。