您的位置:首页 > 其它

MapReduce案例5——求互粉好友对

2018-03-16 21:30 253 查看
题目:
A:B,C,D,F,E,O
B:A,C,E,K
C:F,A,D,I
D:A,E,F,L
E:B,C,D,M,L
F:A,B,C,D,E,O,M
G:A,C,D,E,F
H:A,C,D,E,O
I:A,O
J:B,O
K:A,C,D
L:D,E,F
M:E,F,G
O:A,H,I,J,K
求互粉好友对:例如A的粉丝有B,B的粉丝有A,则为一对互粉好友对
思路:将数据按照从小到大的顺序形成好友对,作为key值,在reduce里面统计key的值,如果key数目为2,即认为是互为好友对。/**
* @author: lpj
* @date: 2018年3月16日 下午7:16:47
* @Description:
*/
package lpj.reduceWork;

import java.io.IOException;
import java.security.cert.CertPathValidatorException.Reason;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
/**
*
*/
public class EachFansMR {

public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
// conf.addResource("hdfs-site.xml");//使用配置文件
// System.setProperty("HADOOP_USER_NAME", "hadoop");//使用集群
FileSystem fs = FileSystem.get(conf);//默认使用本地

Job job = Job.getInstance(conf);
job.setJarByClass(EachFansMR.class);
job.setMapperClass(EachFansMR_Mapper.class);
job.setReducerClass(EachFansMR_Reducer.class);

job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(NullWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
//
// String inputpath = args[0];
// String outpath = args[1];

Path inputPath = new Path("d:/a/homework5.txt");
Path outputPath = new Path("d:/a/homework5");
if (fs.exists(inputPath)) {
fs.delete(outputPath, true);
}

FileInputFormat.setInputPaths(job, inputPath);
FileOutputFormat.setOutputPath(job, outputPath);
boolean isdone = job.waitForCompletion(true);
System.exit(isdone ? 0 : 1);
}

public static class EachFansMR_Mapper extends Mapper<LongWritable, Text, Text, NullWritable>{
Text kout = new Text();
Text valueout = new Text();
@Override
protected void map(LongWritable key, Text value,Context context)throws IOException, InterruptedException {
//A:B,C,D,F,E,O组合为两两好友对
String [] reads = value.toString().trim().split(":");
String kk = reads[0];
String [] friends = reads[1].split(",");
for(int i = 0; i < friends.length; i ++){
String vv = friends[i];
if (kk.compareTo(vv) < 0) {
kout.set(kk + "-" + vv);
}else {
kout.set(vv + "-" + kk);
}
context.write(kout, NullWritable.get());
}
}
}
public static class EachFansMR_Reducer extends Reducer<Text, NullWritable, Text, NullWritable>{
Text kout = new Text();
Text valueout = new Text();
@Override
protected void reduce(Text key, Iterable<NullWritable> values, Context context)throws IOException, InterruptedException {
int count = 0;
//判断好友对数目
for(NullWritable text : values){
count ++;
}
if (count == 2) {
context.write(key, NullWritable.get());
}
}

}

}结果:16对A-B
A-C
A-D
A-F
A-O
B-E
C-F
D-E
D-F
D-L
E-L
E-M
F-M
H-O
I-O
J-O
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息