Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.apache.hadoop.ramapo;
- import java.io.IOException;
- import java.util.StringTokenizer;
- import java.util.Map;
- import java.util.TreeMap;
- import java.util.Arrays;
- import java.util.ArrayList;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.Writable;
- import org.apache.hadoop.io.IntWritable;
- import org.apache.hadoop.io.LongWritable;
- import org.apache.hadoop.io.NullWritable;
- 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.FileInputFormat;
- import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
- public class Project2{
- public static class MyMapper
- extends Mapper<LongWritable, Text, IntWritable, IntWritable>{
- public void map (LongWritable key, Text value, Context context) throws IOException, InterruptedException{
- String[] line = value.toString().split("\t");
- //String[] line = value.toString().split(",");
- Integer user = Integer.parseInt(line[0]);
- Integer movie = Integer.parseInt(line[1]);
- context.write(new IntWritable(user), new IntWritable(movie));
- }
- }
- public static class MyReducer
- extends Reducer<IntWritable, IntWritable, IntWritable, Text> {
- // first TM is for UserID : movies; the second is UserID : friendList
- private TreeMap<Integer, ArrayList<Integer>> TM1 = new TreeMap<Integer, ArrayList<Integer>>();
- public TreeMap<Integer, ArrayList<Integer>> TM2 = new TreeMap<Integer, ArrayList<Integer>>();
- // will be used for the context (output will be the list of a user's liked movies)
- String outputString = "";
- public void reduce (IntWritable key, Iterable<IntWritable> value, Context context) throws IOException, InterruptedException{
- // create a treemap where key is UserID and value is a
- // list of UserID's watched movies
- for(IntWritable x: value){
- Integer movieVal = x.get();
- ArrayList<Integer> valList = TM1.get(key.get());
- // if not in list, add new list to TM, else
- // append to TM's value list
- if (valList == null) {
- valList = new ArrayList<Integer>();
- valList.add(movieVal);
- TM1.put(key.get(), valList);
- } else {
- valList.add(movieVal);
- }
- }
- // populate the second TM; this one will contain UserID: Friend List
- for(ArrayList<Integer> x: TM1.values()){
- outputString = Integer.toString(x.get(0));
- for (int i = 1; i < x.size(); i++)
- {
- outputString = outputString + "," + Integer.toString(x.get(i)) ;
- }
- outputString = outputString + "\n";
- }
- context.write(new IntWritable(key.get()), new Text(outputString));
- }
- }
- public static void main(String[] args) throws Exception {
- Configuration conf = new Configuration();
- Job job = Job.getInstance(conf, "Project2");
- job.setJarByClass(Project2.class);
- job.setMapperClass(MyMapper.class);
- //job.setCombinerClass(MyReducer.class);
- job.setReducerClass(MyReducer.class);
- job.setMapOutputKeyClass(IntWritable.class);
- job.setMapOutputValueClass(IntWritable.class);
- job.setOutputKeyClass(IntWritable.class);
- job.setOutputValueClass(Text.class);
- FileInputFormat.addInputPath(job, new Path(args[0]));
- FileOutputFormat.setOutputPath(job, new Path(args[1]));
- System.exit(job.waitForCompletion(true) ? 0 : 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement