# The Old MapReduce

The JobTracker is responsible for resource management, tracking resource usage and job life-cycle management, e.g. scheduling job tasks, tracking progress, providing fault-tolerance for tasks.

The TaskTracker is the per-node slave for JobTracker, takes orders from the JobTracker to launch or tear-down tasks, and provides task status information to the JobTracker periodically.

For those years, we are benefited from the MapReduce framework, it’s the most successful programming model in the big data world.

But MapReduce is not everything, we need to do graph processing, or real-time stream processing, since Hadoop is essentially batch oriented, we have to look for other systems to do those work.

The fundamental idea of YARN is to split up the two major responsibilities of the JobTracker i.e. resource management and job scheduling/monitoring, into separate daemons: a global ResourceManager (RM) and per-application ApplicationMaster (AM).

The ResourceManager is responsible for allocating resources to the running applications.

The NodeManager is a per-machine slave, works on launching the application’s containers, monitoring the resource usage, and reporting them to the ResourceManager.

The ApplicationMaster is a per-application framework, which runs as a normal container, responsible for negotiating appropriate resource containers from ResourceManager, tracking their status and monitoring for progress.

My intention is to read the source code of hadoop, so I prefer to build a hadoop distribution from the source code.

First, git clone from github or apache.

Second, checkout the branch-2.2.0 branch, which is quite stable.

Third, apply below patch,

Fourth, type and run.

mvn package -Pdist -DskipTests -Dtar

# The Installation Guide

This is a great guide to install Hadoop 2.2.0.

I did a single installation, after configuring everything, hdfs can be setup and daemons are started by below scripts.

Then, run date -u commands on two containers.

The logs are printed:

And the results are:

That’s my first YARN job running!

# Colorful Season

– Taken on November 30, 2013 (flickr)

# An Approach for User Behavior Clustering

## Story

We, programmers built Apps for people to use, sometimes, we could benefit from our users, too.

We could collect anonymous data from users by recording their behaviors on using our App, then analyzing those data, we could find the most favorable features of our App for us to plan for future development, we could uncover some hidden needs of users for us to add new features or create new Apps, we could cluster the users and use different marketing strategy on each users group, etc.

This post will be an example of how I do user clustering.

Imagine I have a music player app, which has 2 millions users.

All data in my hand is how many times a user played, downloaded, purchased, and shared the songs as well as his active days (If a user opens the app one day, then the active days increment by one) as follows.

100035 7 53 0 0 4
150079 45 312 3 8 63
... ... ... ... ... ...
199972 114 2425 82 25 205

## k-means Algorithms

k-means clustering aims to partition n observations into k clusters and each cluster is represented by its cluster center.

Euclidean distance can be used to represent the distance of each point.

Given cluster centers, we can simply assign each point to its nearest center. Similarly, if we know the assignment of points to clusters, we can compute the centers by their means.

This introduces a chicken-and-egg problem.

The general computer science answer to chicken-and-egg problems is iteration. We will start with a guess of the cluster centers, for example, randomly choose k points as cluster centers. Based on that guess, we will assign each data point to its closest center. Then we can recompute the cluster centers on these new assignments.

Repeat above process until clusters stop moving.

If you want to know more, please click.

## Solution

### Identify the features

From the data, there are five columns, “played”, “downloaded”, “purchased”, “shared”, and “active days”.

The first four are user behaviors, and we believe all are important, so those four will be our features.

### Normalize the data

But the data is not “balanced”, some values are hundreds of times bigger than the others. Luckily, we have “active days”, simply divide each feature value by its “active days”, then the values are “balanced”.

### Clustering

We will use scipy, believe me, it’s a great tool.

First, import the packages and load the data.

Then trying k = 4,

and get the centers of each cluster.

Below code will assign the code(cluster) to each subjects(observations).

And calculates each cluster size.

Then I cried, look at the biggest cluster, the fourth one, the number of songs played, downloaded, purchased, and shared per active day by users are all nearly 0.

The final truth is although I have 2 million users, nearly all are zombie users.

(Disclaimer, the data mentioned in this post is faked)