Hadoop Reducer没有从Mapper接收完整的输入

时间:2018-12-08 09:16:56

标签: hadoop mapreduce

我是Hadoop的新手,我正在尝试一个简单的mapreduce程序,该程序将找到给定一对朋友的共同朋友。 输入是一个JSON文件,其中包含一个人和他们的朋友数组。 Mapper正确处理了输入,但减速器未正确接收它。 确切地说,reducer中的“ count”仅为1(我在日志中检查了它) 我在这个问题上坚持了很长时间,我们将为您提供任何帮助。

映射器代码:

public class FriendMapper extends Mapper<LongWritable, Text, FriendPair, FriendArray>{

@Override
public void map(LongWritable key, Text value,Context context) throws IOException, InterruptedException {

    StringTokenizer st = new StringTokenizer(value.toString(), "\t");
    String person = st.nextToken();
    String friends = st.nextToken();

    Friend f1 = populateFriend(person); 
    List<Friend> friendList =  populateFriendList(friends); 

    Friend[] friendArray = Arrays.copyOf(friendList.toArray(), friendList.toArray().length, Friend[].class);
    FriendArray fArray = new FriendArray(Friend.class, friendArray);

    for (Friend f2 : friendList) {
        FriendPair fp = new FriendPair(f1,f2);
        context.write(fp, fArray);
    }
}

private Friend populateFriend(String friendJSON) {
    Friend friend = null;
    JSONParser parser = new JSONParser();

    try {
        Object obj = parser.parse(friendJSON); 
        JSONObject jObj = (JSONObject)obj;

        Long lid = (Long) jObj.get("id");
        IntWritable id = new IntWritable(lid.intValue());
        Text name = new Text((String) jObj.get("name"));
        Text hometown = new Text((String) jObj.get("homeTown"));

        friend = new Friend(id, name, hometown);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return friend;
}

private List<Friend> populateFriendList(String friendJSON){
    List<Friend> friendList =  new ArrayList<Friend>();

    try {
        JSONParser parser = new JSONParser();
        Object obj = (Object)parser.parse(friendJSON.toString());
        JSONArray jArray = (JSONArray) obj; 

        for(Object ob : jArray) {
            JSONObject jObj = (JSONObject) ob;
            Long lid = (Long) jObj.get("id");
            IntWritable id = new IntWritable(lid.intValue());
            Text name = new Text((String) jObj.get("name"));
            Text hometown = new Text((String) jObj.get("homeTown"));

            Friend friend = new Friend(id, name, hometown);
            friendList.add(friend);
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return friendList;
}

}

减速器代码:

 public class FriendReducer extends Reducer<FriendPair, FriendArray, FriendPair, FriendArray> {
    @Override
    public void reduce(FriendPair key,Iterable<FriendArray> values,Context context) throws IOException, InterruptedException {
        List<Friend[]> fArrayList = new ArrayList<Friend[]>();
        List<Friend> commonFriendsList = new ArrayList<Friend>();

        int count=0;
        for(FriendArray fArray : values) {
            Friend[] f = Arrays.copyOf(fArray.get(), fArray.get().length,Friend[].class);
            fArrayList.add(f);
            count++;    
        }

        if(count!=2) {
            return;
        }

        for(Friend outerf:fArrayList.get(0)) {
            for(Friend innerf:fArrayList.get(1)) {
                if(outerf.equals(innerf)) {
                    commonFriendsList.add(innerf);
                }
            }
        }

        Friend[] commonFriendArray = Arrays.copyOf(commonFriendsList.toArray(), commonFriendsList.toArray().length,Friend[].class);
        context.write(key, new FriendArray(Friend.class, commonFriendArray));
    }

}

比较和等于好友对的方法(这将确定2个好友相同)

public int compareTo(Object o) {        
        FriendPair fp = (FriendPair) o;
        int cmp=-1;
        if(getF1().compareTo(fp.getF1())==0 || getF1().compareTo(fp.getF2())==0)
            cmp=0;
        if(cmp!=0) {
            return cmp;
        }
        cmp=-1;
        if(getF2().compareTo(fp.getF1())==0 || getF2().compareTo(fp.getF2())==0)
            cmp=0;
        return cmp;
    }

    public boolean equals(Object o) {   
        FriendPair fp = (FriendPair) o;
        boolean eq = getF1().equals(fp.getF1())||getF1().equals(fp.getF2());
        if(!eq)
            return eq;
        return getF2().equals(fp.getF1())||getF2().equals(fp.getF2());                  
    }

输入json文件:

{"id":1,"name":"nithin","homeTown":"tvm"}   [{"id":2,"name":"ruben","homeTown":"ktm"},{"id":3,"name":"abel","homeTown":"mum"},{"id":4,"name":"xyz","homeTown":"crack"}]
{"id":3,"name":"abel","homeTown":"mum"} [{"id":1,"name":"nithin","homeTown":"tvm"},{"id":2,"name":"ruben","homeTown":"ktm"},{"id":8,"name":"1233","homeTown":"4444"}]
{"id":2,"name":"ruben","homeTown":"ktm"}    [{"id":1,"name":"nithin","homeTown":"tvm"},{"id":3,"name":"abel","homeTown":"mum"},{"id":8,"name":"1233","homeTown":"4444"}]

0 个答案:

没有答案