源码:
package com.recommend;
import com.recommend.util.TextTup;
import com.recommend.util.TextTupGroupComparator;
import com.recommend.util.TextTupSortComparator;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
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.MultipleInputs;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.jobcontrol.ControlledJob;
import org.apache.hadoop.mapreduce.lib.jobcontrol.JobControl;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import java.io.IOException;
import java.util.Iterator;
//看过去的代码就跟和过去的自己沟通一样,可以看到当时自己的思路。和写作不同,代码可以看到自己精巧的设计。唯代码和文章以郷自己。2017-11-4
public class GetRecommend extends Configured implements Tool{
//main方法
public static void main(String[] args) throws Exception {
System.exit(ToolRunner.run(new GetRecommend(),args));
}
//job0负责将购物清单按物品进行分组
public static class PlinksMapper extends Mapper<LongWritable,Text,Text,Text>{
String user;
String good;
int amount;
private Text key = new Text();
private Text value = new Text();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] split = value.toString().split(" ");
user=split[0];
good=split[1];
amount=Integer.valueOf(split[2]);
this.key.set(good);
//this.value.set(user+":"+amount);
this.value.set(user+":"+amount);
context.write(this.key,this.value);
}
}
public static class PlinksReducer extends Reducer<Text,Text,Text,Text> {
Text goods =new Text();
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
StringBuffer buffer= new StringBuffer();
for (Text good:values){
buffer.append(good+"\t");
}
goods.set(buffer.toString());
context.write(key,goods);
}
}
//job1负责将购物清单里用户和物品进行分组
public static class UserOfGoodsMapper extends Mapper<LongWritable,Text,Text,Text> {
String user;
String good;
//这里的amount为后期加强推荐准确度做准备,amount为客户购买该物品的数量
//int amount;
private Text key = new Text();
private Text value = new Text();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] split = value.toString().split(" ");
user=split[0];
good=split[1];
//amount=Integer.valueOf(split[2]);
this.key.set(user);
this.value.set(good);
context.write(this.key,this.value);
}
}
public static class UserOfGoodsReducer extends Reducer<Text,Text,Text,Text> {
//NullWritable n=NullWritable.get();
Text key = new Text();
Text goods =new Text();
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
StringBuffer buffer= new StringBuffer();
for (Text good:values){
buffer.append(good+"\t");
}
goods.set(buffer.toString());
context.write(key,goods);
}
}
//job2:从全局角度得到物品的相似度矩阵,就是商场的全部购物清单中物品A与哪些物品同时被客户购买了(同时出现-->共现)。此处原始相似度的数值均设为1,可为以后扩展这一块做预备。
public static class GoodsOf