在Reducer中查找最常见的键,错误:java.lang.ArrayIndexOutOfBoundsException:1

在Reducer中查找最常见的键,错误:java.lang.ArrayIndexOutOfBoundsException:1,java,hadoop,mapreduce,reduce,Java,Hadoop,Mapreduce,Reduce,我需要在Reducer中找到Mapper发出的最常见的键。我的减速器通过以下方式工作正常: public static class MyReducer extends Reducer<NullWritable, Text, NullWritable, Text> { private Text result = new Text(); private TreeMap<Double, Text> k_closest_points= new TreeMap<

我需要在Reducer中找到Mapper发出的最常见的键。我的减速器通过以下方式工作正常:

public static class MyReducer extends Reducer<NullWritable, Text, NullWritable, Text> {
    private Text result = new Text();
    private TreeMap<Double, Text> k_closest_points= new TreeMap<Double, Text>();
    public void reduce(NullWritable key, Iterable<Text> values, Context context)
            throws IOException, InterruptedException {

        Configuration conf = context.getConfiguration();
        int K = Integer.parseInt(conf.get("K"));
        for (Text value : values) {
            String v[] = value.toString().split("@");    //format of value from mapper: "Key@1.2345"
            double distance = Double.parseDouble(v[1]);
            k_closest_points.put(distance, new Text(value));    //finds the K smallest distances
            if (k_closest_points.size() > K)
                k_closest_points.remove(k_closest_points.lastKey());
        }
        for (Text t : k_closest_points.values())    //it perfectly emits the K smallest distances and keys
            context.write(NullWritable.get(), t);
    }
}

你能帮我解决这个问题吗?

有几件事。。。首先,你的问题是:

double distance = Double.parseDouble(v[1]);
您正在拆分
“@”
,它可能不在字符串中。如果不是,它将抛出
OutOfBoundsException
。我想补充一项条款,例如:

if(v.length < 2)
    continue;
应该是:

class_counts.put(tmp[0], class_counts.get(tmp[0]) + 1);
在一个可能很大的
地图中查找两次密钥也很昂贵。以下是我如何根据您提供给我们的内容重新编写减速机(这是完全未经测试的):

你保留的最近的两点是什么<代码>“同等接近”
“超远”
。这是因为不能有同一密钥的两个实例。因此,您的算法无法打破联系。要解决这个问题,您可以做以下几件事:

<强>第一< /强>,如果您在“代码>减速器< /代码>中执行此操作,您知道您的传入数据不会引起<代码> OutOfMeMyRebug < /代码>,请考虑使用不同的排序结构,如<代码> TeSeSe<代码>,并建立自定义的<代码>可比的< /Cord>对象,它将排序:

static class KNNEntry implements Comparable<KNNEntry> {
    final Text text;
    final Double dist;

    KNNEntry(Text text, Double dist) {
        this.text = text;
        this.dist = dist;
    }

    @Override
    public int compareTo(KNNEntry other) {
        int comp = this.dist.compareTo(other.dist);
        if(0 == comp)
            return this.text.compareTo(other.text);
        return comp;
    }
}
静态类knentry实现可比较{
最后文本;
最后双区;
KNENTRY(文本,双距离){
this.text=文本;
this.dist=dist;
}
@凌驾
公共内部比较(KNentry其他){
int comp=此.dist.compareTo(其他.dist);
如果(0==comp)
返回this.text.compareTo(other.text);
返回补偿;
}
}
然后使用
TreeSet
,而不是
TreeMap
,它将根据上面构建的
比较器
逻辑对自身进行内部排序。然后,在完成所有键之后,只需迭代第一个
k
,并按顺序保留它们。不过,这有一个缺点:如果数据确实很大,则可以通过将所有值从reducer加载到内存中,从而使heapspace溢出

第二个选项:使上面构建的
knentry
实现
可写可比
,并从您的
映射器发出该选项,然后使用该选项处理条目的排序。这会变得有点毛茸茸的,因为您必须使用大量的映射器,然后只有一个还原器来捕获第一个
k
。如果您的数据足够小,请尝试第一个选项,以允许打破平局


但是,回到您最初的问题,您得到了一个
出界异常
,因为您试图访问的索引不存在,即输入
字符串中没有“@”

double距离=double.parseDouble(v[1])这就是它发生的地方。你确定值中有“@”吗?是的,我非常确定。第一个版本的输出如下所示:Corsa@0.1951866287909985. 第一个也可以正常工作。检查
v
tmp
的大小,缩小可能性。首先,非常感谢您的建议。我将尝试实施您提供的第一种方法。但映射发出的值中包含@符号。因为第一个版本的输出正是我所期望的(class@distance). 在添加新行以查找最常用的键之后,它开始抱怨索引边界。请检查我的编辑。我重写了你的减速机。我认为这可能是tmp的问题。试试看它是否有效。。。
class_counts.put(tmp[0], class_counts.get(tmp[0] + 1));
class_counts.put(tmp[0], class_counts.get(tmp[0]) + 1);
public static class MyReducer extends Reducer<NullWritable, Text, NullWritable, Text> {
    private Text result = new Text();
    private TreeMap<Double, Text> k_closest_points = new TreeMap<Double, Text>();

    public void reduce(NullWritable key, Iterable<Text> values, Context context)
            throws IOException, InterruptedException {

        Configuration conf = context.getConfiguration();
        int K = Integer.parseInt(conf.get("K"));

        for (Text value : values) {
            String v[] = value.toString().split("@");
            if(v.length < 2)
                continue; // consider adding an enum counter

            double distance = Double.parseDouble(v[1]);
            k_closest_points.put(distance, new Text(v[0])); // you've already split once, why do it again later?

            if (k_closest_points.size() > K)
                k_closest_points.remove(k_closest_points.lastKey());
        }


        // exit early if nothing found
        if(k_closest_points.isEmpty())
            return;


        TreeMap<String, Integer> class_counts = new TreeMap<String, Integer>();
        for (Text value : k_closest_points.values()) {
            String tmp = value.toString();
            Integer current_count = class_counts.get(tmp);

            if (null != current_count) // avoid second lookup
                class_counts.put(tmp, current_count + 1);
            else
                class_counts.put(tmp, 1);
        }

        context.write(NullWritable.get(), new Text(class_counts.lastKey()));
    }
}
int k = 2;
TreeMap<Double, Text> map = new TreeMap<>();
map.put(1.0, new Text("close"));
map.put(1.0, new Text("equally close"));
map.put(1500.0, new Text("super far"));
// ... your popping logic...
static class KNNEntry implements Comparable<KNNEntry> {
    final Text text;
    final Double dist;

    KNNEntry(Text text, Double dist) {
        this.text = text;
        this.dist = dist;
    }

    @Override
    public int compareTo(KNNEntry other) {
        int comp = this.dist.compareTo(other.dist);
        if(0 == comp)
            return this.text.compareTo(other.text);
        return comp;
    }
}