Facebook Likes Analyzer MapReduce Case Study

It is a small java codes that shows how to use Hadoop to analyze facebook data i.e likes. It tells the most frequent likers in the friend circle.

Facebook Like Aggregator Likester Tracks What’s Hot On Facebook

package com.hadoopgyaan.facebook.likes.;

import java.io.*;

import java.util.Iterator;

import java.util.List;

import com.restfb.*;

import com.restfb.types.Likes;

import com.restfb.types.User;

public class FacebookLikesAggregator {

// have to generate again at https://developers.facebook.com/tools/explorer/ while demo

private static final String ACCESS_TOKEN = “CAACEdEose0cBAhybhhyguijuyyhkjoiuynjiuhybiuhhytbihyZB9beehdyvYxVGeUK2l3PvOUQUMVgzoHldbhieLYE6ttxXzJ0d5zBNSnlxLcJPyT3pZCXYmY0JrHAzeKsxOVC2EWx9EPAlaOw6V8SsgBKC8wZAsXMve0jogkiAXZBBt8lD2HrfrGnERY4gsBZBWv3IFPoEdKTPPVXHRYaoj”;

private static final String FIELD_SEPARATOR = “,”;

private static final String FILE_NAME = “facebook-file.tsv”;

public static void main(String[] args) {

FileWriter writer = null;

PrintWriter printWriter = null;

try {

File file = new File(FILE_NAME);

if(file.exists()) file.delete();

file.createNewFile();

writer = new FileWriter(file);

printWriter = new PrintWriter(writer);

// create connection with facebook

FacebookClient facebookClient = new DefaultFacebookClient(ACCESS_TOKEN, Version.VERSION_2_5);

// fetch all my friends (post 2.0 version fb is not returning all friends)

Connection<User> myFriends = facebookClient.fetchConnection(“me/friends”, User.class);

Iterator<List<User>> iter = myFriends.iterator();

int usersCount = 0;

while (iter.hasNext()) {

List<User> users = iter.next();

for (User usr : users) {

int likes = getUsersLikes(facebookClient, usr.getId());

printDataToTabDelimitedFile(printWriter, usr.getId(), usr.getName(), likes);

usersCount++;

}

}

System.out.println(“Total users aggregated : ” + usersCount);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

try {

writer.close();

printWriter.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

public static int getUsersLikes(FacebookClient facebookClient, String userId) {

Connection<Likes> userLikes = facebookClient.fetchConnection(userId + “/likes”, Likes.class);

Iterator<List<Likes>> iter = userLikes.iterator();

int likesCount = 0;

while (iter.hasNext()) {

List<Likes> likes = iter.next();

likesCount = likesCount + likes.size();

}

return likesCount;

}

public static void printDataToTabDelimitedFile(PrintWriter writer, String userId, String userName, int likes) {

StringBuilder userActivity = new StringBuilder();

userActivity.append(userId).append(FIELD_SEPARATOR);

userActivity.append(userName).append(FIELD_SEPARATOR);

userActivity.append(likes);

writer.print(userActivity);

writer.println();

}

}

 

Facebook Mapper Class :

package com.hadoopgyaan.facebook.likes;

import java.io.IOException;

import org.apache.hadoop.io.LongWritable;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Mapper;

public class FacebookMapper extends Mapper<LongWritable,Text,LongWritable,Text>{

public LongWritable userLikes = new LongWritable();

public Text userName = new Text();

public void map(LongWritable key, Text value, Context context) throws IOException,InterruptedException{

String[] split = value.toString().split(“,”);

userName.set((split[1]));

if (split.length > 2){

try{

userLikes.set(Long.parseLong((split[2])));

context.write(userLikes,userName);

}catch(NumberFormatException e){

// cannot parse

}

}

}

}

Facebook Reducer Class :

package com.hadoopgyaan.facebook.likes;

import java.io.IOException;

import java.util.Iterator;

import org.apache.hadoop.io.LongWritable;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Reducer;

public class FacebookReducer extends Reducer<LongWritable,Text , Text, LongWritable> {

@Override

public void reduce(LongWritable key, Iterable<Text> users, Context context)

throws IOException, InterruptedException {

//long likesCount = 0;

// LongWritable likes = new LongWritable();

for (Text user : users) {

context.write(user, key);

}

}

}

Facebook Driver Class :

package com.hadoopogyaan.facebook.likes;

import org.apache.hadoop.conf.Configured;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.IntWritable;

import org.apache.hadoop.io.LongWritable;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Job;

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import org.apache.hadoop.util.Tool;

import org.apache.hadoop.util.ToolRunner;

public class FacebookDriver extends Configured implements Tool{

public int run(String[] args) throws Exception{

if(args.length != 2){

System.out.println(“Usage:CountWordsDriver <input path> <output path>”);

System.exit(-1);

}

Job job = new Job();

job.setJarByClass(FacebookDriver.class);

job.setJobName(“Facebook likes analytics”);

FileInputFormat.addInputPath(job, new Path(args[0]));

FileOutputFormat.setOutputPath(job, new Path(args[1]));

job.setMapperClass(FacebookMapper.class);

job.setReducerClass(FacebookReducer.class);

job.setMapOutputKeyClass(LongWritable.class);

job.setMapOutputValueClass(Text.class);

job.setSortComparatorClass(LongWritable.DecreasingComparator.class);

System.exit(job.waitForCompletion(true) ? 0:1);

boolean success = job.waitForCompletion(true);

return success ? 0:1;

}

public static void main(String[] args) throws Exception{

FacebookDriver driver = new FacebookDriver();

int exitcode = ToolRunner.run(driver, args);

System.exit(exitcode);

}

}

 

I hope this tutorial will surely help you. If you have any questions or problems please let me know.

Happy Hadooping with Patrick..

Leave a Reply

Your email address will not be published. Required fields are marked *