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 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;
- // Input-> utr.txt -> ut.csv -> uu.csv
- // first step: output is <userId, likedMovieId> (where liked is 3.0 or greater)
- public class Project{
- public static class MyMapper
- extends Mapper<LongWritable, Text, IntWritable, Text> {
- public void map (LongWritable key, Text value, Context context) throws IOException, InterruptedException{
- String[] lines = value.toString().split(",");
- Integer user = Integer.parseInt(lines[0]);
- Integer movie = Integer.parseInt(lines[1]);
- double rating = Double.parseDouble(lines[2]);
- context.write(new IntWritable(user), new Text(Integer.toString(movie) + "," + Double.toString(rating)));
- }
- }
- public static class MyReducer
- extends Reducer<IntWritable, Text, IntWritable, IntWritable> {
- public void reduce (IntWritable key, Iterable<Text> value, Context context) throws IOException, InterruptedException{
- for(Text val : value) {
- String[] lines = val.toString().split(",");
- Integer movie = Integer.parseInt(lines[0]);
- Double rating = Double.parseDouble(lines[1]);
- if (rating >= 3) {
- System.out.println(key.toString() + " " + String.valueOf(rating));
- context.write(key, new IntWritable(movie));
- }
- }
- }
- }
- public static void main(String[] args) throws Exception {
- Configuration conf = new Configuration();
- Job job = Job.getInstance(conf, "Project");
- job.setJarByClass(Project.class);
- job.setMapperClass(MyMapper.class);
- //job.setCombinerClass(MyReducer.class);
- job.setReducerClass(MyReducer.class);
- job.setMapOutputKeyClass(IntWritable.class);
- job.setMapOutputValueClass(Text.class);
- job.setOutputKeyClass(IntWritable.class);
- job.setOutputValueClass(IntWritable.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