mapreduce倒排索引算法_第1頁
mapreduce倒排索引算法_第2頁
mapreduce倒排索引算法_第3頁
mapreduce倒排索引算法_第4頁
mapreduce倒排索引算法_第5頁
已閱讀5頁,還剩2頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、mapreduce程序設計報告姓名: 學號: 題目: 莎士比亞文集倒排索引算法 1、 實驗環境聯想pc機虛擬機:vm 10.0操作系統:centos 6.4hadoop版本:hadoop 1.2.1jdk版本:jdk-7u25eclipse版本:eclipse-sdk-4.2.2-linux-gtk-x86_642、 實驗設計及源程序2.1實驗說明對莎士比亞文集文檔數據進行處理,對莎士比亞文集文檔數據進行倒排索引處理,結果輸出到指定文件2.2實驗設計(1)invertedindexmapper類這個類實現 mapper 接口中的 map 方法,輸入參數中的 value 是文本文件中的一行,利用

2、正則表達式對數據進行處理,使文本中的非字母和數字符號轉換成空格,然后利用stringtokenizer 將這個字符串拆成單詞,最后將輸出結果,outkey為單詞+單詞所在的文件名,outvalue為1。public static class invertedindexmapper extends mapper private final static intwritable one = new intwritable(1); public void map(object key, text value, context context ) throws ioexception, interru

3、ptedexception /獲取文件名以及預處理 filesplit filesplit =(filesplit)context.getinputsplit(); string filename =filesplit.getpath().getname(); string line=value.tostring(); string s; /利用正則表達式除去非數字和字母的符號 pattern p =ppile(w+); matcher m=p.matcher(line); string line2=m.replaceall( ); stringtokenizer itr = new stri

4、ngtokenizer(line2); /按照空格對字符串進行劃分 while (itr.hasmoretokens() s=itr.nexttoken().tolowercase(); if(!ls.contains(s) text filename_num=new text(s+,+filename);/將單詞和單詞所在的文件名進行合并 context.write(filename_num, one); (2)invertedindexpartitioner類這個類是自定義的partitioner類,通過復寫getpartition() 方法來自定義子集的分區key。將 key按照分隔符進

5、行分割,取key的前面部分進行分區,將相同的(即單詞相同)分入同一個reduce。public static class invertedindexpartitioner extends hashpartitioner public int getpartition(text key,intwritable value,int numreducetasks)text key1 =new text(key.tostring().split(,)0); super.getpartition(key1,value,numreducetasks); return 0; (3)combinereduce

6、r類這個類是在map輸出結果之后輸入reduce之前做的一個操作,是一個小型的reduce操作,這個操作可以減少reduce階段的工作量,從而優化性能。public static class combinereducer extends reducer public void reduce(text key, iterable values, context context ) throws ioexception, interruptedexception int sum = 0; for (intwritable val : values) sum += val.get(); contex

7、t.write(key,new intwritable(sum);(4)invertedindexredecuer類這個類實現了reducer接口中的reduce方法,map的結果經過combine處理之后,數據輸入reduce,key為單詞+單詞所在文件名,value為單詞的詞頻數,由于要實現倒排,所以key只能為單詞,取key的第一部分即單詞,把key的第二部分即文件名和原有value合并和新的value,作為新的key和value,然后輸出結果,outkey為單詞,outvalue為文件名+單詞詞頻數。public static class invertedindexreducer ex

8、tends reducer private text filename_num=new text(); stringbuilder all=new stringbuilder(); public void reduce(text key, iterable values, context context ) throws ioexception, interruptedexception text key1=new text(key.tostring().split(,)0); /表示單詞 int sum = 0;/p為定義的一個list類型的全局變量,用來存儲每個單詞的所在文件名和詞頻數 n

9、ewkey為定義的一個text的全局變量 for (intwritable val : values) sum += val.get(); if(newkey = null | !newkey.equals(key1) if(newkey!= null) stringbuffer all =new stringbuffer(); for(text t:p) all.append(t.tostring(); all.append(;); context.write(newkey, new text(all.tostring(); p.clear(); /每一個單詞的結果輸出完畢后,p要格式化 n

10、ewkey.set(key1);/每一個單詞的結果輸出完畢后,換成另一個單詞開始計數 filename_num=new text(key.tostring().split(,)1+sum); p.add(filename_num); /reduce階段的清理工作,用來輸出最后一個單詞的結果 public void cleanup(context context) throws ioexception, interruptedexception stringbuffer all =new stringbuffer(); for(text t:p) all.append(t); all.appen

11、d(;); context.write(newkey, new text(all.tostring(); (6)主程序定義了一個job,進行一個必要的設置。 public static void main(string args) throws exception configuration conf = new configuration(); string otherargs = new genericoptionsparser(conf, args).getremainingargs(); if (otherargs.length != 2) system.err.println(usa

12、ge: wordcount ); system.exit(2); string uri=hdfs:/localhost:8000/user/tzj/stop_words;/從hdfs讀取停詞 filesystem fs=filesystem.get(uri.create(uri), conf); fsdatainputstream in =fs.open(new path(uri); inputstreamreader lsr=new inputstreamreader(in); bufferedreader buf=new bufferedreader(lsr); string input;

13、 while(input=buf.readline()!=null) ls.add(input); system.out.println(the stop_words are:); iterator it =ls.iterator(); while(it.hasnext()system.out.print(it.next()+ ); system.out.println(); job job = new job(conf, word count); fileinputformat.addinputpath(job, new path(otherargs0); fileoutputformat.

14、setoutputpath(job, new path(otherargs1); job.setjarbyclass(invertedindex.class); job.setmapperclass(invertedindexmapper.class);/設置partitioner類job.setpartitionerclass(invertedindexpartitioner.class);/設置combiner類 job.setcombinerclass(combinereducer.class); job.setreducerclass(invertedindexreducer.class); job.setinputformatclass(textinputformat.class); job.setmapoutputkeyclass(text.class); job.setmapoutputvalueclass(intwritable.class); job.setoutpu

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論