数据准备:
A:B,C,D,F,E,O
B:A,C,E,KC:F,A,D,ID:A,E,F,LE:B,C,D,M,LF:A,B,C,D,E,O,MG:A,C,D,E,FH:A,C,D,E,OI:A,OJ:B,OK:A,C,DL:D,E,FM:E,F,GO:A,H,I,J需求:
1.先求出A、B、C、….等是谁的好友
2.求出哪些人两两之间有共同好友,及他俩的共同好友都有谁?
需求解读:
1.有题目可得,该关系为单项关系可以理解为关注,即A关注的为BCDEF,B关注的为AK,所以求A B C...是谁的关注,即需要将冒号后边的作为key,前边的作为value进行map,在reduce的时候在合并即可。
2.求两两之间的共同关注,这时我们需要借助1题产生的结果文件,map是从后边的value值两两组合生成两两关系。作为新的key值输入,新的value为1题的key。
reduce的时候,将结果合并即可
源代码如下
第一题:
package Demo5;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.Text;import org.apache.hadoop.mapreduce.Job;import org.apache.hadoop.mapreduce.Mapper;import org.apache.hadoop.mapreduce.Reducer;import org.apache.hadoop.mapreduce.Reducer.Context;import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;/** * @author 星际毁灭 * 找出ABC等人的好友 * */import Demo4.Log1;public class Friend1 { public static class Map extends Mapper
第二题:
package Demo5;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.Text;import org.apache.hadoop.mapreduce.Job;import org.apache.hadoop.mapreduce.Mapper;import org.apache.hadoop.mapreduce.Reducer;import org.apache.hadoop.mapreduce.Reducer.Context;import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;/** * @author 王翌淞 * * 求出他们的共同好友 * */public class Friend2 { public static class Map extends Mapper{ private static Text newKey=new Text(); private static final IntWritable one = new IntWritable(1); public void map(Object key,Text value,Mapper .Context context) throws IOException, InterruptedException{ String line=value.toString(); String[] array=line.split("\t"); String first=array[0]; String second=array[1]; String[] friend=second.split(","); System.out.println(first+"\t"+second); //通过两层的for循环,A的好友进行组合,有顺序 for(int i=0;i { public void reduce(Text key,Iterable values,Context context) throws IOException, InterruptedException{ String res=""; for(Text val:values) { res+=val.toString()+" "; } context.write(key,new Text(res)); } } public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{ System.setProperty("hadoop.home.dir", "H:\\文件\\hadoop\\hadoop-2.6.4"); Configuration conf=new Configuration(); Path in=new Path("hdfs://192.168.6.132:9000/wys/out/friend1/part-r-00000"); Path out=new Path("hdfs://192.168.6.132:9000/wys/out/friend2"); Job job =new Job(conf,"OneSort"); FileInputFormat.addInputPath(job,in); FileOutputFormat.setOutputPath(job,out); job.setJarByClass(Friend2.class); job.setMapperClass(Friend2.Map.class); job.setReducerClass(Friend2.Reduce.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); job.waitForCompletion(true); System.exit(job.waitForCompletion(true) ? 0 : 1); } }