共計 998 個字符,預計需要花費 3 分鐘才能閱讀完成。
mapreduce 中 wordcount 的 java 實現是怎樣的,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面丸趣 TV 小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
用 java 模擬詞頻統計。
有 3 個文件:text1: hello world
text2:hello hadoop
text3:hello mapreduce
對上面的文件進行詞頻統計:結果應該是:hello:3; hadoop:1; world:1; mapreduce:1
代碼實現如下:
package count;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.StringTokenizer;
public class Count { public static void main(String[] args) { String [] text = new String[]{ hello world , hello hadoop , hello mapreduce
Hashtable ht = new Hashtable();
for(int i=0; i i++){ StringTokenizer st = new StringTokenizer(text[i]);
while(st.hasMoreTokens()){ String word = st.nextToken();
if(!ht.containsKey(word))
ht.put(word, new Integer(1));
else{ int wc = ((Integer)ht.get(word)).intValue() + 1;
ht.put(word, new Integer(wc));
}
}
}
for(Iterator itr = ht.keySet().iterator(); itr.hasNext();){ String word = (String)itr.next();
System.out.print(word + : + (Integer)ht.get(word)+
}
}
}
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注丸趣 TV 行業資訊頻道,感謝您對丸趣 TV 的支持。
正文完