Reduce-Side使用Java加入ArrayIndexOutOfBounds异常

时间:2014-09-14 17:48:19

标签: java exception mapreduce

尝试在3个数据集上运行Reduce Side连接。不幸的是,我一直得到一个ArrayIndex异常。我尝试过尝试和处理它。抓住,但无济于事。有人可以推荐一个解决方案吗?

package Joins;

import java.io.IOException;
import java.util.*;
import java.util.Map.Entry;

import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.Reducer;

public class JoinReducer extends Reducer<Text, Text, Text, Text> 
{
Map<String, String> divStkJoin = new HashMap<String, String>();
Map<String, String> divStkMetaJoin = new HashMap<String, String>();
Map<String, String> stockData = new HashMap<String, String>();
Map<String, String> metaData = new HashMap<String, String>();
Map<String, String> divData = new HashMap<String, String>();
Text k = new Text();
Text v = new Text();

public void setup(Context context)
{   
    metaData.clear();
    divData.clear();
    stockData.clear();
    divStkJoin.clear();
    divStkMetaJoin.clear();
}

public void reduce(Text keys, Iterable<Text> values, Context context)
{

    Iterator it = values.iterator();

    while(it.hasNext()){
    String [] keyLine = keys.toString().split(","); 
    String valueLine = values.toString();

    try {

        if(keyLine[4].equals("_s"))
        {
            String keyLineStock = keyLine[0] + "," + keyLine[1] +"," + keyLine[2] + "," +   keyLine[3];
            stockData.put(keyLineStock, valueLine);

        }
        else if(keyLine[4].equals("_d"))
        {
            String keyLineDiv = keyLine[0] + "," + keyLine[1] +"," + keyLine[2] + "," + keyLine[3];
             divData.put(keyLineDiv, valueLine);
        }

        else if (keyLine[1].equals("_m"))
        {
            String keyLineMeta = keyLine[0];
            metaData.put(keyLineMeta, valueLine);
        }
        else
            return;

    } catch (ArrayIndexOutOfBoundsException e){return;}
    }   



        //JOINS

        for(Entry<String, String> entryStock: stockData.entrySet())
            for(Entry<String, String> entryDiv: divData.entrySet())
                {
                    if(entryStock.getKey().equals(entryDiv.getKey()))
                    {
                        divStkJoin.put(entryStock.getKey(), entryStock.getValue()+ ","+ entryDiv.getValue());
                    }
                }
        for(Entry<String, String> entrydivStkJoin: divStkJoin.entrySet())
        {
            String [] entrydivStkJoinKeyArr = entrydivStkJoin.getKey().toString().split(",");
            for(Entry<String, String> meta: metaData.entrySet())    
            {
                String [] metaArr = meta.getKey().split(",");

                if(metaArr[0].equals(entrydivStkJoinKeyArr[1]))
                {
                    divStkMetaJoin.put(entrydivStkJoin.toString(), meta.getValue());
                }
            }
        }
    }   



public void cleanup(Context context) throws IOException, InterruptedException
{

        String keyJ;
        String valJ;
    for(Map.Entry<String, String> entry : divStkMetaJoin.entrySet())
    {
        keyJ=entry.getKey();
        valJ=entry.getValue();
        Text k = new Text(keyJ);
        Text v = new Text(valJ);

        context.write(k, v);
    }
}

}

1 个答案:

答案 0 :(得分:1)

我认为错误来自这一行:

if(keyLine[4].equals("_s"))

如果keyLine null 或keyLine&lt; 4:

 if(ss == null || ss.length()<4){
     return;
 }