MapReduce购物推荐引擎

源码:

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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值