Business Intelligence & Big Data Analytics - 2 Darpan Naik – 60
Practical 5:
Aim:
Write a map-reduce program to determine the average ratings of movies. The
input consists of a series of lines, each containing a movie number, user number,
rating and a timestamp
Steps:
1. Open Eclipse IDE and create a new project with a class files – [Link]
Open [Link] and paste the following code:
[Link]
package Movie;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
import [Link].*;
public class MovieRating
{
public static class MovieRatingsMapper extends Mapper<LongWritable, Text , IntWritable,
Text>
{
public void map(LongWritable key, Text value, Context context) throws
IOException, InterruptedException
{
String valueStr = [Link]();
int index = [Link](',');
if(index != -1)
{
try
Business Intelligence & Big Data Analytics - 2 Darpan Naik – 60
{
IntWritable keyUserID = new
IntWritable([Link]([Link](0, index)));
[Link](keyUserID, new Text([Link](index
+ 1)));
}
catch(Exception e)
{
// You could get a NumberFormatException
}
}
}
}
public static class MovieRatingsReducer extends Reducer<IntWritable, Text, IntWritable,
Text>
{
public void reduce(IntWritable key, Iterable<Text> values, Context context) throws
IOException, InterruptedException
{
int movieCount = 0;
int movieRatingCount = 0;
String movieValues = "";
for (Text value : values)
{
String[] tokens = [Link]().split(",");
if([Link] == 2)
{
movieRatingCount += [Link](tokens[1].trim()); //
You could get a NumberFormatException
movieCount++;
movieValues = [Link]([Link]() + " ");
}
}
[Link](key, new Text([Link](movieCount) + "," +
[Link](movieRatingCount) + ",(" + [Link]() + ")"));
}
}
public static void main(String[] args) throws Exception
{
Configuration conf = new Configuration();
Job job = [Link](conf,"CompositeKeyExample");
[Link]([Link]);
[Link]([Link]);
Business Intelligence & Big Data Analytics - 2 Darpan Naik – 60
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link](job, new Path("/MovieRating/Input/[Link]"));
[Link](job, new Path("/MovieRating/Output"));
[Link]([Link](true) ? 0:1);
}
}
Output:
Business Intelligence & Big Data Analytics - 2 Darpan Naik – 60