MapReduce处理求共同好友

//map
package com.gh.day2_4;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
//A: b,c,f,h,j
//B:f,g,d
friend person
//map输出为b,c,f,h,j /A
public class FriendMap extends Mapper<LongWritable, Text,Text,Text> {
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text,Text,Text>.Context context) throws IOException, InterruptedException {
String[] split = value.toString().split("?;
String person = split[0];
String split1 = split[1];
String[] friend = split1.split(",");
for (String friends:friend
) {
context.write(new Text(friends), new Text(person));
}
}
}

//reduce
package com.gh.day2_4;

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

import java.io.IOException;

                          friend          person

// reduce处理成 A: b ,d,f,g
public class FriendReduce extends Reducer<Text,Text,Text,Text> {
protected void reduce(Text friend, Iterable values, Reducer<Text,Text,Text,Text>.Context context) throws IOException, InterruptedException {
StringBuffer sb=new StringBuffer();
for (Text friends:values
) {
sb.append(friends).append(",");
}
context.write(friend,new Text(sb.toString()));
}
}

//map2
package com.gh.day2_4;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.util.Arrays;
person friend
//对persons进行排序,排列成A-B c,
public class FriendMap2 extends Mapper<LongWritable, Text,Text,Text> {
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text,Text,Text>.Context context) throws IOException, InterruptedException {
String[] split = value.toString().split("\t");
String friend = split[0];
String split1 = split[1];
String[] person = split1.split(",");
Arrays.sort(person);
for (int i = 0; i <person.length-1 ; i++) {
for (int j = i+1; j <person.length ; j++) {
context.write(new Text(person[i]+"-"+person[j]), new Text(friend));
}
}
}
}

//reduce2
package com.gh.day2_4;

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

import java.io.IOException;
//处理成 A-B C,D,F
public class FriendReduce2 extends Reducer<Text,Text,Text,Text> {
protected void reduce(Text person, Iterable values, Reducer<Text,Text,Text,Text>.Context context) throws IOException, InterruptedException {
StringBuffer sb=new StringBuffer();
for (Text friends:values
) {
sb.append(friends).append(",");
}
context.write(person,new Text(sb.toString()));
}
}

package com.gh.day2_4;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class FriendText {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
String inPath = “D:/ww/input11”;
String outPath = “D:/ww/output11”;
Configuration conf = new Configuration();
Job job = null;
job = Job.getInstance(conf);
job.setJarByClass(FriendText.class);
job.setMapperClass(FriendMap.class);
job.setReducerClass(FriendReduce.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(inPath));
FileOutputFormat.setOutputPath(job, new Path(outPath));
boolean b = job.waitForCompletion(true);
System.exit(b ? 0 : -1);

}

}

package com.gh.day2_4;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class FriendText2 {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
String inPath = “D:/ww/output11”;
String outPath = “D:/ww/output12”;
Configuration conf = new Configuration();
Job job = null;
job = Job.getInstance(conf);
job.setJarByClass(FriendText2.class);
job.setMapperClass(FriendMap2.class);
job.setReducerClass(FriendReduce2.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(inPath));
FileOutputFormat.setOutputPath(job, new Path(outPath));
boolean b = job.waitForCompletion(true);
System.exit(b ? 0 : -1);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值