好友推荐案例

一、编写类

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);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值