The requirements are as follows:
Some input files. The first column represents the IP address, and the even column afterwards represents the search term. The numbers (odd series) represent the number of searches, separated by "\ t. Now, you need to perform word segmentation and calculate the word frequency. Here, you do not consider the number of searches, which may be page turning or searching links.
Here, the IK word segmentation package is used for Chinese word segmentation, and the source code is directly put into SRC. Thanks to ik word segmentation.
The procedure is as follows:
<Span style = "font-size: 14px;"> package seg; import Java. io. bytearrayinputstream; import Java. io. ioexception; import Java. io. inputstream; import Java. io. inputstreamreader; import Java. io. reader; import Java. util. arraylist; import Java. util. list; import Org. apache. hadoop. conf. configuration; import Org. apache. hadoop. conf. configured; import Org. apache. hadoop. FS. filesystem; import Org. apache. hadoop. FS. path; import o RG. apache. hadoop. io. intwritable; 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; import Org. apache. hadoop. util. genericoptionspa Rser; import Org. apache. hadoop. util. tool; import Org. apache. hadoop. util. toolrunner; import Org. wltea. analyzer. core. iksegmenter; import Org. wltea. analyzer. core. lexeme;/*** @ author zhf * @ version Creation Time: 3:04:40, January 1, August 16, 2014 */public class segmenttool extends configured implements tool {public static void main (string [] ARGs) throws exception {int exitcode = toolrunner. run (New segmenttool (), argS); system. ex It (exitcode) ;}@ overridepublic int run (string [] arg0) throws exception {configuration conf = new configuration (); string [] ARGs = new genericoptionsparser (Conf, arg0 ). getremainingargs (); If (ARGs. length! = 2) {system. err. println ("Usage: seg. segmenttool <input> <output> "); system. exit (2);} job = new job (Conf, "nseg. jar "); filesystem FS = filesystem. get (CONF); If (FS. exists (New Path (ARGs [1]) FS. delete (New Path (ARGs [1]), true); job. setjarbyclass (segmenttool. class); job. setmapperclass (segmentmapper. class); job. setcombinerclass (segreducer. class); job. setreducerclass (segreducer. class); job. setmapoutputkeyclass (Te XT. class); job. setmapoutputvalueclass (intwritable. class); job. setoutputkeyclass (text. class); job. setoutputvalueclass (intwritable. class); fileinputformat. addinputpath (job, new path (ARGs [0]); fileoutputformat. setoutputpath (job, new path (ARGs [1]); Return job. waitforcompletion (true )? 0: 1;} public static class segmentmapper extends mapper <longwritable, text, text, intwritable> {private iksegmenter IKS = new iksegmenter (true); private text word = new text (); private Final Static intwritable one = new intwritable (1); public void map (longwritable key, text value, context) throws ioexception, interruptedexception {string line = value. tostring (). trim (); string [] STR = line. split ("\ t "); For (INT I = 1; I <Str. length; I + = 2) {string TMP = STR [I]; If (TMP. startswith ("HTTP") continue; List <string> List = segment (TMP); For (string S: List) {word. set (s); context. write (word, one) ;}} private list <string> segment (string Str) throws ioexception {byte [] BYT = Str. getbytes (); inputstream is = new bytearrayinputstream (BYT); reader = new inputstreamreader (is); IKS. reset (Reader); lexeme; List <strin G> List = new arraylist <string> (); While (lexeme = IKS. Next ())! = NULL) {string text = lexeme. getlexemetext (); list. add (text) ;}return list ;}public static class segreducer extends CER <text, intwritable, text, intwritable> {private intwritable result = new intwritable (); public void reduce (Text key, iterable <intwritable> values, context) throws ioexception, interruptedexception {int sum = 0; For (intwritable VAL: values) sum + = Val. get (); result. set (SUM); context. write (Key, result) ;}}</span>
The hadoop environment used is hadoop 2.3.0-cdh5.0.0. Three hadoop-related jars need to be introduced: hadoop-mapreduce-client-core-2.0.0-cdh4.6.0.jar, hadoop-common-2.0.0-cdh4.6.0.jar, and commons-cli-1.2.jar.
After packaging, run the command: yarn jar Seg. Jar Seg. segmenttool/test/user/zhf/input/test/user/zhf/Output
The output result is as follows:
<Span style = "font-size: 18px; "> ad 1 nearby 2 Chen 22 Chen Jon 1 Chan Yi Xun 1 Chen Yi 2 limit 4 Shaanxi 4 in addition to individual 1 privacy 1 next door 1 Integrated 4 highlights 1 rain 2 snow 5 dew 1 green 7 Qingdao 2 </span>
However, there is no sorting. If the data volume is small, you can use the Linux Command: Sort-K2-n-r kw_result.txt> kw_freq.txt for sorting.
If there is a large amount of data, you can import the results into hive, because there are only two columns, hive-e "select key, count from kw_table sort by Count DESC;"> kw_freq.txt to get ordered results.
You can also sort the previous ouput as the input of the next job. The key and value output by map need to be reversed.
The Code is as follows:
<Span style = "font-size: 14px;"> package seg; import Java. io. ioexception; import Org. apache. hadoop. conf. configuration; import Org. apache. hadoop. conf. configured; import Org. apache. hadoop. FS. filesystem; import Org. apache. hadoop. FS. path; import Org. apache. hadoop. io. intwritable; import Org. apache. hadoop. io. longwritable; import Org. apache. hadoop. io. text; import Org. apache. hadoop. io. writablecomparator; import Org. apa Che. 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; import Org. apache. hadoop. util. genericoptionsparser; import Org. apache. hadoop. util. tool; import Org. apache. hadoop. util. toolrunner;/*** @ author zhf * @ email [email prote CTED] * @ version Creation Time: August 16, 2014 4:51:00 */public class sortbyfrequency extends configured implements tool {public static void main (string [] ARGs) throws exception {int exitcode = toolrunner. run (New sortbyfrequency (), argS); system. exit (exitcode) ;}@ overridepublic int run (string [] arg0) throws exception {configuration conf = new configuration (); string [] ARGs = new genericoptionsparser (Conf, arg0 ). ge Tremainingargs (); If (ARGs. length! = 2) {system. err. println ("Usage: seg. sortbyfrequency <input> <output> "); system. exit (2);} job = new job (Conf, "nseg. jar "); filesystem FS = filesystem. get (CONF); If (FS. exists (New Path (ARGs [1]) FS. delete (New Path (ARGs [1]), true); job. setjarbyclass (sortbyfrequency. class); job. setmapperclass (sortmapper. class); job. setreducerclass (sortreducer. class); job. setsortcomparatorclass (desccomparator. class); job. setmapo Utputkeyclass (intwritable. class); job. setmapoutputvalueclass (text. class); job. setoutputkeyclass (text. class); job. setoutputvalueclass (intwritable. class); fileinputformat. addinputpath (job, new path (ARGs [0]); fileoutputformat. setoutputpath (job, new path (ARGs [1]); Return job. waitforcompletion (true )? 0: 1;} public static class sortmapper extends mapper <longwritable, text, intwritable, text> {public void map (longwritable key, text value, context) throws ioexception, interruptedexception {string STR [] = value. tostring (). split ("\ t"); context. write (New intwritable (integer. valueof (STR [1]), new text (STR [0]) ;}} public static class sortreducer extends ritcer <intwritable, text, text, intwritable> {private text result = new text (); Public void reduce (intwritable key, iterable <text> values, context) throws ioexception, interruptedexception {for (Text VAL: values) {result. set (VAL); context. write (result, key) ;}} public static class desccomparator extends writablecomparator {protected desccomparator () {super (intwritable. class, true) ;}@ overridepublic int compare (byte [] arg0, int arg1, int arg2, byte [] arg3, int arg4, int arg5) {return-Super. compare (arg0, arg1, arg2, arg3, arg4, arg5) ;}@ overridepublic int compare (Object A, object B) {return-Super. compare (a, B) ;}}</span>
The result is as follows:
175 Shanghai 158 85 city 76 transport 58 TV 58 type 582 52 at 71ppt 64