Domanda

I connect hadoop with the eclipse ,start the job by eclipse plug-in, the mapreduce job can complete successfully,but when i compile my code to jar file and then execute this job by hadoop command,it will throw errors as follows.

Error:java.lang.IndexOutOfBoundsException:Index:1,Size:1
      at java.util.ArrayList.rangecheck(Arraylist.java:635)
      at java.util.ArrayList.get(ArrayList.java:411)
      at Combiner.reduce(Combiner.java:32)
      at Combiner.reduce(Combiner.java:1)

and my code as follows:

import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.PriorityQueue;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;


public class Combiner extends MapReduceBase implements Reducer<Text,Text,Text,Text>{
    public void reduce(Text       key,Iterator<Text>values,OutputCollector<Text,Text>output,Reporter reporter)
            throws IOException{
        int num=3;
        Comparator<String> comparator=new MyComparator();
        PriorityQueue<String> queue=new PriorityQueue<String>(100,comparator);
        ArrayList<String> list=new ArrayList<String>();
        while(values.hasNext()){
            String str=values.next().toString();
            queue.add(str);
        }
        while(!queue.isEmpty()){

            list.add(queue.poll());

        }
        String getCar="";
        for(int i=0;i<num;i++){
            getCar=getCar+list.get(i)+"\n";

        }
        output.collect(new Text(""), new Text(getCar));
    }

    public class MyComparator implements Comparator<String>{
        public int compare(String s1,String s2){
            if(Long.parseLong(s1.split(",")[4])>Long.parseLong(s2.split(",")[4])){
                return 1;
            }else if(Long.parseLong(s1.split(",")[4])<Long.parseLong(s2.split(",")[4])){
                return -1;
            }else{
                return 0;
            }
        }
    }
}
È stato utile?

Soluzione

This happens, because your list has one element (Size:1), and you ask for the second element (Index:1 - Indexing starts from zero)! A simple System.out.println for each list element will help you get through...

Why do you set the number of elements to 3? If you know that it will be 3 (unlikely), then change the list to an array of size 3. If you don't know that, then change num to list.size(), like:

for(int i=0;i<list.size();i++)

But before anything else, you should understand why you get these values for this key.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top