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
- TM1.forEach((k,v) -> {
- TM1.forEach((k2,v2) -> {
- // determine if k and k2 are friends
- // if value contains a single instance found in value2, they are friends
- boolean isFriend = (v2.stream().filter(item -> v.contains(item.intValue())).findAny().orElse(null)) == null ? false : true;
- if(isFriend) {
- // if in map2, append value to list,
- // else create entry with k as key, and k2 as first value
- // in list
- if(TM2.containsKey(k)) {
- TM2.get(k).add(k2);
- } else {
- ArrayList<Integer> friendList = new ArrayList<Integer>();
- friendList.add(k2);
- TM2.put(k, friendList);
- }
- }
- });
- });
- TM2.forEach((k, v) -> System.out.println("UserID: " + k + "\nFriend List: " + v + "\n\n"));
- }
- for(ArrayList<Integer> x: TM2.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