如何定义自定义text.comparator

rqqzpn5f  于 2021-06-03  发布在  Hadoop
关注(0)|答案(1)|浏览(227)

我的Map器将向reducer发出<text,text>,键实际上是double,例如,<'34.90','hello'>,表示“hello”的平均计数是34.90。另外,输入格式为:hello 34.90 1:10;小说2:20;。。。,看来我不能用倒计时器了。
我尝试使用'doublewritable'作为键,但我不知道如何发出它,collect似乎只发出<text,text>。
问题是,929.00比93.00小,这是事实,所以我想定义一个新的text.comparator,我看到了源代码

public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2)
{
  int n1 = WritableUtils.decodeVIntSize(b1[s1]);
  int n2 = WritableUtils.decodeVIntSize(b2[s2]);
  return compareBytes(b1, s1 + n1, l1 - n1, b2, s2 + n2, l2 - n2);
}

我不明白 s1+n1 以及 l1-n1 意思。
谢谢你的帮助。

vshtjzan

vshtjzan1#

你可以用 FloatWritable . 下面是如何在mapper中发射它。

public class CheckMapper extends Mapper<LongWritable,Text,FloatWritable,Text> 
{
//calculation on your hello count
public void map(LongWritable key,Text value,Context context) throws IOException,InterruptedException
    {
float var = hello_count;
String otheroutput = //do something with value
context.write(new FloatWritable(var),new Text(otheroutput));
}
}

您不需要在这里定义一个定制的comparator类,floatwriteable的内置功能将解决这个问题。

相关问题