代码如下:
Map 任务:输出时 key 为 由两个String 记录的ID表示的 二度人脉关系,value 为 这个二度关系产生的间接人的ID
- public void map(Text key, TextPair values, Context context) throws IOException,InterruptedException{
- Map<String, String> first_follow = new HashMap<String, String>();
- Map<String, String> second_befollow = new HashMap<String, String>();
- String _key = key.toString();
- String[] follow = values.getFirst().toString().split(Separator.TABLE_String);
- String[] second = values.getSecond().toString().split(Separator.TABLE_String);
- for(String sf : follow){
- first_follow.put(sf , _key );
- }
- for(String ss : second){
- second_befollow.put(ss , _key );
- }
- for(Entry<String, String> f : first_follow.entrySet()){
- for(Entry<String, String> b : second_befollow.entrySet()){
- context.write(new TextPair(f.getKey() ,b.getKey()), new Text(key));
- }
- }
- }
Reduce任务:输出时 key 仍然为二度人脉关系, value 为所有间接人 的ID以逗号分割。
- protected void reduce(TextPair key, Iterable<Text> values, Context context)
- throws IOException, InterruptedException {
- StringBuilder resutl = new StringBuilder();
- for (Text text : values){
- resutl.append(text.toString()).append(",");
- }
- context.write(key, new Text(resutl.toString()));
- }
到