PageRank implemented by mapreduce

Source: Internet
Author: User
Input Format:
A 1 B, c, d
B 1 c, d
Map:
B A 1/3
A 1/3
D A 1/3
A | B, c, d
C B 1/2
D B 1/2
B | c, d
Reduce:
B (1-0.85) + 0.85*1/3 c, d

C (1-0.85) + 0.85*5/6
D (1-0.85) + 0.85*5/6
A (1-0.85) + 0.85*1 B, c, d


Import Java. io. ioexception; import Org. apache. hadoop. conf. configuration; import Org. apache. hadoop. FS. path; import Org. apache. hadoop. io. longwritable; import Org. apache. hadoop. io. text; import Org. apache. hadoop. mapreduce. job; import Org. apache. hadoop. mapreduce. mapper; import Org. apache. hadoop. mapreduce. reducer; import Org. apache. hadoop. mapreduce. lib. input. fileinputformat; import Org. apache. hadoop. mapreduce. lib. output. fileoutputformat; public class pagerankiter {Private Static final double damping = 0.85; public static class pritermapper extends mapper <longwritable, text> {public void map (longwritable key, text value, context context) throws ioexception, interruptedexception {string line = value. tostring (); string [] tuple = line. split ("\ t"); string pagekey = tuple [0]; double pr = double. parsedouble (tuple [1]); If (tuple. length> 2) {string [] linkpages = tuple [2]. split (","); For (string linkpage: linkpages) {string prvalue = pagekey + "\ t" + String. valueof (Pr/linkpages. length); context. write (new text (linkpage), new text (prvalue);} context. write (new text (pagekey), new text ("|" + tuple [2]) ;}} public static class priterreducer extends reducer <text, text> {public void reduce (Text key, iterable <text> values, context) throws ioexception, interruptedexception {string links = ""; double PageRank = 0; For (text value: values) {string TMP = value. tostring (); If (TMP. startswith ("|") {links = "\ t" + TMP. substring (TMP. indexof ("|") + 1); // index starts from 0 to continue;} string [] tuple = TMP. split ("\ t"); If (tuple. length> 1) PageRank + = double. parsedouble (tuple [1]);} PageRank = (double) (1-damping) + damping * PageRank; // The iteration formula context of PageRank. write (new text (key), new text (string. valueof (PageRank) + Links);} public static void main (string [] ARGs) throws exception {configuration conf = new configuration (); job job2 = new job (Conf, "pagerankiter"); job2.setjarbyclass (pagerankiter. class); job2.setoutputkeyclass (text. class); job2.setoutputvalueclass (text. class); job2.setmapperclass (pritermapper. class); job2.setreducerclass (priterreducer. class); fileinputformat. addinputpath (job2, New Path (ARGs [0]); fileoutputformat. setoutputpath (job2, New Path (ARGs [1]); job2.waitforcompletion (true );}}
Input is the output above
Input Format:
A PR
B PR
...

Import Java. io. ioexception; import Org. apache. hadoop. conf. configuration; import Org. apache. hadoop. FS. path; import Org. apache. hadoop. io. longwritable; import Org. apache. hadoop. io. floatwritable; import Org. apache. hadoop. io. writablecomparable; import Org. apache. hadoop. io. writablecomparator; import Org. apache. hadoop. io. text; import Org. apache. hadoop. mapreduce. job; import Org. apache. hadoop. mapreduce. mapper; import Org. apache. hadoop. mapreduce. lib. input. fileinputformat; import Org. apache. hadoop. mapreduce. lib. output. fileoutputformat; public class pagerankviewer {public static class pagerankviewermapper extends mapper <longwritable, text, floatwritable, text> {private text outpage = new text (); Private floatwritable outpr = new floatwritable (); public void map (longwritable key, text value, context) throws ioexception, interruptedexception {string [] line = value. tostring (). split ("\ t"); string page = line [0]; float Pr = float. parsefloat (line [1]); outpage. set (PAGE); outpr. set (PR); context. write (outpr, outpage) ;}/ ** reload the key comparison function so that it goes through shuffle and sort in reverse order (from large to small) output **/public static class descfloatcomparator extends floatwritable. comparator {// @ override public float compare (writablecomparator A, writablecomparable <floatwritable> B) {return-Super. compare (a, B);} public int compare (byte [] B1, int S1, int L1, byte [] B2, int S2, int l2) {return-Super. compare (B1, S1, L1, B2, S2, L2) ;}} public static void main (string [] ARGs) throws exception {configuration conf = new configuration (); job job3 = new job (Conf, "pagerankviewer"); job3.setjarbyclass (pagerankviewer. class); job3.setoutputkeyclass (floatwritable. class); job3.setsortcomparatorclass (descfloatcomparator. class); job3.setoutputvalueclass (text. class); job3.setmapperclass (pagerankviewermapper. class); fileinputformat. addinputpath (job3, New Path (ARGs [0]); fileoutputformat. setoutputpath (job3, New Path (ARGs [1]); job3.waitforcompletion (true );}}

  

PageRank implemented by mapreduce

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.