MapReduce Code

Input Data

2012-01-01    2:01    Omaha    Book    10.51    Visa

it’s tab delimited, values will be the date, the time, the store name, a description of the item, the cost, and the method of payment.
Mapper Code (

    for line in sys.stdin:
        data = line.strip().split("\t")
            date, time, storename, productname, cost, paymethod = data
            print "{0}\t{1}".format(storename, cost)

Reducer Code (
In my case, i have a single Reducer, because that’s the Hadoop default, so it will get all the keys. If i had specified more than one Reducer, each would receive some of the keys, along with all the values from all the Mappers for those keys.

Hadoop Streaming allows you to write your Mappers and Reducers in pretty much any language, rather than forcing you to use Java.

salesTotal = 0
oldStore = None
for line in sys.stdin:
	data_sale = line.strip().split("\t")
	if len(data_sale) != 2: 

	thisStore, thisSale = data_sale
	if oldStore and oldStore!=thisStore:
    		print oldStore,"\t", salesTotal
    		salesTotal = 0

	oldStore = thisStore
	salesTotal += float(thisSale)
if oldStore != None:
	print oldStore,"\t", salesTotal

Once the Mapper is done, the Hadoop framework passes the intermediate data to the Reducers. What’s the process called that happens between Mappers and Reducers?

The process is called the Shuffle and Sort. It ensures that the values for any particular key are collected together, and sends the keys and their lists of values to the Reducer.

Test with a small data set before you run your code on your entire, huge set of data.

Command line test

cd ..
ls code
Filed1    f2    f3    f4    f5    f6
Fielda    fb    fc    fd    fe    ff
F3    f5
Fc    fe

Sample data file test

ls ../data
head -50 ../data/purchases.txt > testdata.txt
cat testdata.txt | ./
cat testdata.txt | ./ | sort
cat testdata.txt | ./ | sort | ./

Cluster test

hs myinput output

Open web browser, localhost:50030/jobtracker.jsp
Here you can see that there’s one running job, and when we click on it we can see the Mappers and Reducers running.

job tracker

If a Mapper or Reducer fails, you can actually drill down and view the logs from that particular piece of code.
if click on job name (running jobs), page will display the progress and other information.
If click map or reduce task,- see completed/ running task list

Putting it all together

data > hadoop -mkdir myinput
data > hadoop fs -put purchases.txt myinput
code > hs myinput output
code > hadoop fs -cat myoutput/part-00000 | less
code > hadoop fs -get myoutput/part-00000 localmyoutput.txt
code > hadoop fs -rm -r -f myoutput


Sqoop – The data would have come from database tables and we’d use Sqoop to import it into HDFS


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: