Search term for top N in billions of records (MapReduce implementation)

Source: Internet
Author: User

Program Examples:

Log information: used car 1345 secondhand 3416 washing machine 2789 input: N=2 output: Secondary washing machine

The map function is as follows:

Importjava.io.IOException;ImportJava.util.Map;ImportJava.util.TreeMap;Importorg.apache.hadoop.io.IntWritable;Importorg.apache.hadoop.io.NullWritable;ImportOrg.apache.hadoop.io.Text;ImportOrg.apache.hadoop.mapreduce.Mapper; Public classTopnmapperextendsMapper<object, Text, nullwritable, text> {    Privatetreemap<intwritable, Text> TM =NewTreemap<intwritable, text>(); PrivateIntwritable MyKey =Newintwritable (); PrivateText myvalue =NewText (); Private intN = 10; @Overrideprotected voidmap (Object key, Text value, Mapper<object, Text, nullwritable, text>. Context context)throwsIOException, interruptedexception {String word= Value.tostring (). Split ("\ t") [0]; intnum = Integer.parseint (value.tostring (). Split ("\ t") [1]);        Mykey.set (num);        Myvalue.set (word);        Tm.put (MyKey, myvalue); if(Tm.size () >N) Tm.remove (Tm.firstkey ()); } @Overrideprotected voidCleanup (Mapper<object, Text, nullwritable, text>. Context context)throwsIOException, interruptedexception { for(Map.entry<intwritable, text>Entry:tm.entrySet ()) {Text value=NewText (Entry.getkey () + "" +Entry.getvalue ());        Context.write (Nullwritable.get (), value); }         }}

The reduce function is as follows:

Importjava.io.IOException;ImportJava.util.TreeMap;Importorg.apache.hadoop.io.IntWritable;Importorg.apache.hadoop.io.NullWritable;ImportOrg.apache.hadoop.io.Text;ImportOrg.apache.hadoop.mapreduce.Reducer; Public classTopnreducerextendsReducer<nullwritable, Text, nullwritable, text>{    Privatetreemap<intwritable, Text> TM =NewTreemap<intwritable, text>(); PrivateIntwritable MyKey =Newintwritable (); PrivateText myvalue =NewText (); Private intN = 10; @Overrideprotected voidReduce (nullwritable key, iterable<text>values, Reducer<nullwritable, Text, nullwritable, text>. Context context)throwsIOException, interruptedexception { for(Text val:values) {string[] tmp= Val.tostring (). Split (""); Mykey.set (Integer.parseint (tmp[0])); Myvalue.set (tmp[1]);            Tm.put (MyKey, myvalue); if(Tm.size () >N) Tm.remove (Tm.firstkey ()); }         for(Text res:tm.descendingMap (). values ()) {Context.write (Nullwritable.get (), res); }    }}

Search term for top N in billions of records (MapReduce implementation)

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.