Mapper tarafından Reducer'da yayılan en yaygın anahtarı bulmalıyım. Benim düşürücü bu şekilde çalışıyor: Bu en küçük mesafeler ile K örneklerini bulur ve çıkış dosyasına yazarReducer'da en yaygın anahtarı bulma, Hata: java.lang.ArrayIndexOutOfBoundsException: 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("@"); //format of value from mapper: "[email protected]"
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);
}
}
. Ama TreeMap'ımdaki en yaygın anahtarı bulmalıyım. Bu yüzden aşağıda gibi çalışıyorum:
Error: java.lang.ArrayIndexOutOfBoundsException: 1
at KNN$MyReducer.reduce(KNN.java:108)
at KNN$MyReducer.reduce(KNN.java:98)
at org.apache.hadoop.mapreduce.Reducer.run(Reducer.java:171)
Bunu düzeltmek için bana yardım edebilir:
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("@");
double distance = Double.parseDouble(v[1]);
k_closest_points.put(distance, new Text(value));
if (k_closest_points.size() > K)
k_closest_points.remove(k_closest_points.lastKey());
}
TreeMap<String, Integer> class_counts = new TreeMap<String, Integer>();
for (Text value : k_closest_points.values()) {
String[] tmp = value.toString().split("@");
if (class_counts.containsKey(tmp[0]))
class_counts.put(tmp[0], class_counts.get(tmp[0] + 1));
else
class_counts.put(tmp[0], 1);
}
context.write(NullWritable.get(), new Text(class_counts.lastKey()));
}
}
Sonra bu hata alıyorum? Sen
"@"
üzerine Ayrılıyorlar
double distance = Double.parseDouble(v[1]);
ve dizede olmayabilir:
'double distance = Double.parseDouble (v [1]); 'Burası gerçekleşiyor. Değerde bir "@" olduğundan emin misin? – Tgsmith61591
Evet, eminim. İlk versiyonun çıkışı şöyle: [email protected] Ve ayrıca birincisi problemsiz çalışıyor. –
Olasılıkları azaltmak için 'v' ve' tmp' boyutlarını kontrol edin. – Berger