com.jbw.recommendsystem.joinim.JoinMRD.java Source code

Java tutorial

Introduction

Here is the source code for com.jbw.recommendsystem.joinim.JoinMRD.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.joinim;

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.MultipleInputs;
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.joinim.JoinMRD -Diteminput=/user/jiabw/out/rs/item_1/part-r-00000 -Dmatrixinput=/user/jiabw/out/rs/matrixlist_1/part-r-00000 -Doutput=/user/jiabw/out/rs/join_1
/**
 *
 * @author alvin
 */
public class JoinMRD extends Configured implements Tool {

    static class IteamMapper extends Mapper<LongWritable, Text, Text, 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) {
                context.write(new Text(ss[0]), new Text(ss[1]));
            }
        }

    }

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

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

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

        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context)
                throws IOException, InterruptedException {
            //            StringBuilder sb = new StringBuilder();
            //            for (Text t : values) {
            //                sb.append(t.toString());
            //            }
            //            context.write(key, new Text(sb.toString()));
            List<String> i1 = new ArrayList<>();
            List<String> i2 = new ArrayList<>();
            for (Text t : values) {
                String s = t.toString();
                String ss = s.substring(1, s.length() - 1);
                String fs[] = ss.split(",");
                for (String st : fs) {
                    if (st.trim().startsWith("10")) {
                        i1.add(st);
                    } else {
                        i2.add(st);
                    }
                }
            }
            //context.write(new Text(i1.toString()), new Text(i2.toString()));

            //            for (String ke : i1.keySet()) {
            //                StringBuilder sb = new StringBuilder();
            //                sb.append("[");
            //                for (String ke2 : i2.keySet()) {
            //                    sb.append(ke2).append(":").append(i1.get(ke) * i2.get(ke2)).append(",");
            //                }
            //                sb.delete(sb.length() - 1, sb.length());
            //                sb.append("]");
            //                context.write(new Text(ke), new Text(sb.toString()));
            //            }
            //20001   [10004:1.0, 10001:1.0, 10005:1.0][20007:1, 20001:3, 20002:2, 20005:2, 20006:2]
            //i1=[10004:1.0,  10001:1.0,  10005:1.0]     i2=[20007:1,  20001:3,  20002:2,  20005:2,  20006:2]
            //10001   [20001:3.0, 20002:2.0, 20005:2.0, 20006:2.0, 20007:1.0]
            for (int i = 0; i < i1.size(); i++) {
                String ss1[] = i1.get(i).split(":");
                StringBuilder sb = new StringBuilder();
                sb.append("[");
                for (int j = 0; j < i2.size(); j++) {
                    String ss2[] = i2.get(j).split(":");
                    sb.append(ss2[0]).append(":")
                            .append(Double.parseDouble(ss1[1].trim()) * Double.parseDouble(ss2[1].trim()))
                            .append(",");
                }
                sb.delete(sb.length() - 1, sb.length());
                sb.append("]");
                context.write(new Text(ss1[0].trim()), new Text(sb.toString()));
            }
        }

    }

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

        Job job = Job.getInstance(conf);
        job.setJobName("jjj");
        job.setJarByClass(JoinMRD.class);

        MultipleInputs.addInputPath(job, itemPath, TextInputFormat.class, IteamMapper.class);
        MultipleInputs.addInputPath(job, matrixPath, TextInputFormat.class, MatrixMapper.class);
        job.setMapOutputKeyClass(Text.class);

        job.setReducerClass(JoinReducer.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        TextOutputFormat.setOutputPath(job, out);

        job.setOutputKeyClass(Text.class);

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

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