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)