MapReduce练习-----互粉好友对
2018-03-23 08:03
190 查看
数据:
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 。 那么A和B就是互粉好友。
思路:
对每一行数据进行组合输出 (person-person,1),
然后再Reducer阶段进行统计,等于2的就是互粉好友对;
问题:将B-A转换成A-B这种形式;
方案:比较两个字符之间的大小,小的在前;import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
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 EachOtherFriend {
public static class MyMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
@Override
protected void map(LongWritable key, Text value,Context context) throws IOException, InterruptedException {
String[] lines = value.toString().split(":");
char person = lines[0].charAt(0);
for (String str : lines[1].split(",")) {
char friend = str.charAt(0);
String per_per = "";
if(person > friend){
per_per += friend+"-"+person;
}else{
per_per += person+"-"+friend;
}
context.write(new Text(per_per), new IntWritable(1));
}
}
}
public static class MyReducer extends Reducer<Text, IntWritable, Text, NullWritable>{
@Override
protected void reduce(Text key, Iterable<IntWritable> values,Context context)
throws IOException, InterruptedException {
int num = 0;
for (IntWritable it : values) {
num++;
}
if(num > 1){
context.write(key, null);
}
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(EachOtherFriend.class);
job.setMapperClass(MyMapper.class);
job.setReducerClass(MyReducer.class);
job.setMapOutpu
4000
tKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
FileInputFormat.setInputPaths(job, new Path("G:/files/input"));
FileOutputFormat.setOutputPath(job, new Path("G:/files/output"));
boolean isDone = job.waitForCompletion(true);
System.exit(isDone ? 0:1);
}
}结果如下:
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 。 那么A和B就是互粉好友。
思路:
对每一行数据进行组合输出 (person-person,1),
然后再Reducer阶段进行统计,等于2的就是互粉好友对;
问题:将B-A转换成A-B这种形式;
方案:比较两个字符之间的大小,小的在前;import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
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 EachOtherFriend {
public static class MyMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
@Override
protected void map(LongWritable key, Text value,Context context) throws IOException, InterruptedException {
String[] lines = value.toString().split(":");
char person = lines[0].charAt(0);
for (String str : lines[1].split(",")) {
char friend = str.charAt(0);
String per_per = "";
if(person > friend){
per_per += friend+"-"+person;
}else{
per_per += person+"-"+friend;
}
context.write(new Text(per_per), new IntWritable(1));
}
}
}
public static class MyReducer extends Reducer<Text, IntWritable, Text, NullWritable>{
@Override
protected void reduce(Text key, Iterable<IntWritable> values,Context context)
throws IOException, InterruptedException {
int num = 0;
for (IntWritable it : values) {
num++;
}
if(num > 1){
context.write(key, null);
}
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(EachOtherFriend.class);
job.setMapperClass(MyMapper.class);
job.setReducerClass(MyReducer.class);
job.setMapOutpu
4000
tKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
FileInputFormat.setInputPaths(job, new Path("G:/files/input"));
FileOutputFormat.setOutputPath(job, new Path("G:/files/output"));
boolean isDone = job.waitForCompletion(true);
System.exit(isDone ? 0:1);
}
}结果如下:
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案例5——求互粉好友对
- MapReduce—案例(六)求互粉好友对
- MapReduce练习----求共同好友
- MapReduce—案例(五)求两两共同好友
- MapReduce练习一(计数,去重,排序,平均成绩)
- MapReduce 练习二 找朋友
- MapReduce实现二度好友关系
- Mapreduce实例---共同好友
- 023_数量类型练习——Hadoop MapReduce手机流量统计
- MapReduce实现之 查找共同好友
- Hadoop第7周练习—MapReduce进行数据查询和实现推简单荐系统(转)
- mapreduce应用-找出扣扣共同好友
- Hadoop—MapReduce计算气象温度等例子---练习
- MapReduce练习二(单表关联,多表关联,倒排索引)
- 9。24 mapreduce简单实例练习
- hadoop之MapReduce练习-二次排序
- HTMO DOM部分---小练习;列表之间移动、日期选择、好友选中、滑动效果、滚动条效果、飞入飞出效果。
- Hadoop—MapReduce练习(数据去重、数据排序、平均成绩、倒排索引)
- 关于flash好友列表的练习
- MapReduce练习(三)