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.iteamlist; 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.iteamlist.ItemMRD -Dinput=/data/rmc/process/input/matrix.txt -Doutput=/user/jiabw/out/rs/item_1 /** * * @author alvin */ public class ItemMRD extends Configured implements Tool { static class ItemMapper extends Mapper<LongWritable, Text, Text, Text> { private final FileParse parser = new FileParse(); 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 { parser.parse(value.toString()); if (parser.isValid()) { k.set(parser.getGid()); v.set(parser.getUid() + ":" + parser.getPre()); context.write(k, v); } } } static class ItemReducer 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(ItemMRD.class); surJob.setJobName("item"); surJob.setMapperClass(ItemMapper.class); surJob.setReducerClass(ItemReducer.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 ItemMRD(), args)); } }