一、编写类
public class RecommendOne {
public static class OneMapper extends Mapper<LongWritable, Text, Text, Text> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] split = value.toString().split(":");
String user = split[0];
String[] split1 = split[1].split(",");
for (String s : split1) {
context.write(new Text(s), new Text(user));
}
}
}
public static class OneReducer extends Reducer<Text, Text, Text, Text> {
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
List<String> list = new ArrayList<>();
for (Text value : values) {
list.add(value.toString());
}
for (int i = 0; i < list.size() - 1; i++) {
for (int j = i + 1; j < list.size(); j++) {
context.write(new Text(list.get(i)+"-"list.get(j)),key);
}
}
}
}
}
二、测试类:
public class OneSubmitter {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(OneSubmitter.class);
job.setMapperClass(RecommendOne.OneMapper.class);
job.setReducerClass(RecommendOne.OneReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
File file = new File("output");
if (file.exists()) {
FileUtils.deleteDirectory(file);
}
FileInputFormat.setInputPaths(job, new Path("input"));
FileOutputFormat.setOutputPath(job, new Path("output"));
job.setNumReduceTasks(1);
boolean b = job.waitForCompletion(true);
System.exit(b ? 0 : 1);
}
}
三、编写类:
public class RecommendTwo {
public static class TwoMapper extends Mapper<LongWritable, Text, Text, Text> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] line = value.toString().split("\t");
context.write(new Text(line[0]),new Text(line[1]));
}
}
public static class TwoReducer extends Reducer<Text, Text, Text, Text> {
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
StringBuffer sb = new StringBuffer();
for (Text value : values) {
sb.append(value.toString() + " ");
}
context.write(new Text((key)), new Text(sb.toString()));
}
}
}
注意Debug测试,防止数组下标越界错误!!!!
四、测试类:
public class TwoSubmitter {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Job job = Job.getInstance(new Configuration());
job.setJarByClass(TwoSubmitter.class);
job.setMapperClass(RecommendTwo.TwoMapper.class);
job.setReducerClass(RecommendTwo.TwoReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
File file = new File("output1");
if (file.exists()){
FileUtils.deleteDirectory(file);
}
FileInputFormat.setInputPaths(job, new Path("output"));
FileOutputFormat.setOutputPath(job, new Path("output1"));
job.setNumReduceTasks(1);
boolean res = job.waitForCompletion(true);
System.exit(res ? 0 : 1);
}
}