MapReduce案例5——求互粉好友对
2018-03-16 21:30
253 查看
题目:
思路:将数据按照从小到大的顺序形成好友对,作为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
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
相关文章推荐
- MapReduce—案例(六)求互粉好友对
- MapReduce案例4——求两两共同好友
- 使用MapReduce实现寻找共同好友的案例
- MapReduce—案例(五)求两两共同好友
- MapReduce练习-----互粉好友对
- 小马马说MapReduce算法案例(1):二度人脉好友推荐
- Hadoop MapReduce案例word count本地环境运行时遇到的一些问题
- MapReduce 案例之数据去重
- Hadoop MapReduce 简单案例--求平均值
- hadoop的mapreduce常见算法案例有几种
- MapReduce应用案例--简单的数据去重
- hadoop - MapReduce初级案例
- MapReduce案例1——求学生成绩普通版
- MapReduce案例11——影评分析2(三表联合查询)
- MapReduce案例——影评分析1(两表联合查询)
- Hadoop集群MapReduce经典案例
- MapReduce案例学习(4) 求各个城市的员工的总工资
- Hbase与Mapreduce集成的案例
- Hadoop集群-MapReduce初级案例
- MapReduce找共同好友