[MapReduce] Find the time (in hours) for each user's favorite PO sticker

Source: Internet
Author: User
Tags stdin

From Udacity,intro to Hadoop, final Project.
The topic requests, from the forum Post's structured data, discovers each user po pastes the most time. For example, user A at 8 points per day, po the most posts, the number marked as X so reducer output is a X
Train of thought: Mapper need to output author_id, reducer to each user to do a {Hour:count} statistics, the last output of the most count hour

#mapper
#!/usr/bin/python

import sys
import csv

reader = Csv.reader (Sys.stdin, delimiter = "\ T")
Next (reader, None) for line in

reader:
    If len (line) = =
        author_id = line[3]
        hour = line[8][11:13]

        print ' {0}\t{1} '. Format (author_id, hour)


# reducer
#!/usr/bin/python

import sys



oldid = None

hours = {} for

I in range:
    hours[i] = 0 for line in

Sys.stdin:

    data = Line.strip (). Split ("\ t ")
    If len (data) = = 2:
        Thisauthorid, hour = data

        if oldid and oldid!= thisauthorid:
            print" {0}\t{1} ". f Ormat (oldid, max (hours, key=hours.get))
            oldid = Thisauthorid for
            i in Range (a):
                hours[i] = 0

        oldid = Thisauthorid
        Hours[int (hour)] + + 1

if oldid!= None:
    print "{0}\t{1}". Format (oldid, Max (hours, key= Hours.get))



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.