com.jbw.recommendsystem.martrixlist.MartrixListMRD.java Source code

Java tutorial

Introduction

Here is the source code for com.jbw.recommendsystem.martrixlist.MartrixListMRD.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jbw.recommendsystem.martrixlist;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

//yarn jar target/RecommendSystem-0.jar com.jbw.recommendsystem.martrixlist.MartrixListMRD -Dinput=/user/jiabw/out/rs/cooc_1/part-r-00000 -Doutput=/user/jiabw/out/rs/matrixlist_1

/**
 *
 * @author alvin
 */
public class MartrixListMRD extends Configured implements Tool {

    static class MListMapper extends Mapper<LongWritable, Text, Text, Text> {

        private final Text k = new Text();
        private final Text v = new Text();

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String ss[] = value.toString().split("\t");
            if (ss != null && ss.length == 2) {
                String sss[] = ss[0].split(",");
                if (sss != null && sss.length == 2) {
                    context.write(new Text(sss[0]), new Text(sss[1] + ":" + ss[1]));
                }
            }
        }
    }

    static class MListReducer extends Reducer<Text, Text, Text, Text> {

        private final List<String> items = new ArrayList<>();

        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context)
                throws IOException, InterruptedException {
            items.clear();
            for (Text t : values) {
                items.add(t.toString());
            }
            context.write(key, new Text(items.toString()));
        }

    }

    @Override
    public int run(String[] strings) throws Exception {
        Configuration conf = getConf();
        Path in = new Path(conf.get("input"));
        Path out = new Path(conf.get("output"));

        Job surJob = Job.getInstance(conf);
        surJob.setJarByClass(MartrixListMRD.class);
        surJob.setJobName("user");

        surJob.setMapperClass(MListMapper.class);
        surJob.setReducerClass(MListReducer.class);

        surJob.setMapOutputKeyClass(Text.class);
        surJob.setMapOutputValueClass(Text.class);

        surJob.setOutputKeyClass(Text.class);
        surJob.setOutputValueClass(Text.class);

        surJob.setInputFormatClass(TextInputFormat.class);
        surJob.setOutputFormatClass(TextOutputFormat.class);

        TextInputFormat.addInputPath(surJob, in);
        TextOutputFormat.setOutputPath(surJob, out);

        return surJob.waitForCompletion(true) ? 0 : 1;
    }

    public static void main(String[] args) throws Exception {
        System.exit(ToolRunner.run(new MartrixListMRD(), args));
    }
}