Java tutorial
/* * 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.cooc; import java.io.IOException; 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.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.lib.input.TextInputFormat; import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; import org.apache.hadoop.mapreduce.lib.reduce.IntSumReducer; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; //yarn jar target/RecommendSystem-0.jar com.jbw.recommendsystem.cooc.CoocMRD -Dinput=/user/jiabw/out/rs/relation_1/part-r-00000 -Doutput=/user/jiabw/out/rs/cooc_1 /** * * @author alvin */ public class CoocMRD extends Configured implements Tool { static class CoocMapper extends Mapper<LongWritable, Text, Text, IntWritable> { private final IntWritable v = new IntWritable(1); private final Text k = new Text(); @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String[] ss = value.toString().split("\t"); if (ss.length == 2) { String[] vv = ss[1].split(","); for (String vv1 : vv) { for (String vv2 : vv) { k.set(getPair(vv1, vv2)); context.write(k, v); } } } } public String getPair(String a, String b) { // if (a.compareTo(b) < 0) { // return "[" + a + "," + b + "] "; // } else { // return "[" + b + "," + a + "] "; // } return a + "," + b; } } // static class CoocReducer extends Reducer<Text, IntWritable, Text, IntWritable> { // // @Override // protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { // int num = 0; // for (IntWritable i : values) { // num += i.get(); // } // context.write(key, new IntWritable(num)); // } // // } @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(CoocMRD.class); surJob.setJobName("Coor"); surJob.setMapperClass(CoocMapper.class); surJob.setReducerClass(IntSumReducer.class); surJob.setMapOutputKeyClass(Text.class); surJob.setMapOutputValueClass(IntWritable.class); surJob.setOutputKeyClass(Text.class); surJob.setOutputValueClass(IntWritable.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 CoocMRD(), args)); } }