设为首页 收藏本站
查看: 756|回复: 0

[经验分享] 初学Hadoop,统计Top10单词

[复制链接]

尚未签到

发表于 2016-12-7 06:38:26 | 显示全部楼层 |阅读模式
课程明明叫SeachTechnology,本以为可以趁机好好学一下Lucene和Nutch,结果Project却是使用分布式计算框架Map/Reduce的开源项目Hadoop进行文档关键词的自动提取,算了,既来之则安之,都是Doug Cutting的作品啊。
Project要求是给定250个文章的摘要(trial data),通过三个步骤
1.preprocessing such as Part-of-Speech tagging,lemmatization and stemming
2.candidate generation
3.candidate ranking
提取前十的关键字,然后将算法在test data上运行,与人工提取的关键字进行比较来评价算法的优劣,要求使用Map/Reduce以使得算法可以在大规模数据上运行。
学习Hadoop的最好资料应该是[url = http://download.csdn.net/source/1491868]Hadoop: The Definitive Guide[/url]。
花了一天时间在Ubuntu上配置完了环境,先在eclipse下写了个统计Top10单词的程序进行一下standalone模式下的试验。期间涉及到一个停用词的删除问题,发现Lucene下面有一个StopFilter可用?得好好研究下

TopWords.java
import java.io.*;
import java.util.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;

public class TopWords {
private static boolean enalbeRemoveStopWords = false;  
public static class Map extends MapReduceBase implements
Mapper<LongWritable, Text, Text, WordCountPair> {
private Text word = new Text();
private Text location = new Text();
private final String[] PUNCTUATIONS = {"\\?","\\.","\\[","\\]",",","\\(","\\)"};
private final String[] STOP_WORDS ={
"\\ba\\b", "\\ban\\b", "\\band\\b", "\\bare\\b","\\bas\\b","\\bat\\b","\\bbe\\b","\\bbut\\b",
"\\bby\\b", "\\bfor\\b", "\\bif\\b", "\\bin\\b", "\\binto\\b", "\\bis\\b", "\\bit\\b",
"\\bno\\b", "\\bnot\\b", "\\bof\\b", "\\bon\\b", "\\bor\\b", "\\bs\\b", "\\bsuch\\b",
"\\bthat\\b", "\\bthe\\b", "\\btheir\\b", "\\bthen\\b", "\\bthere\\b","\\bthese\\b",
"\\bthey\\b", "\\bthis\\b", "\\bto\\b", "\\bwas\\b", "\\bwill\\b", "\\bwith\\b" };
/* the output of map is <filename,<word,frequency>>*/
public void map(LongWritable key, Text value,
OutputCollector<Text, WordCountPair> output, Reporter reporter)
throws IOException {
/*get the filename*/
FileSplit fileSplit = (FileSplit) reporter.getInputSplit();
String fileName = fileSplit.getPath().getName().replaceAll("\\.txt", " :");
location.set(fileName);
/*normalize the words*/
String line = value.toString().toLowerCase();
for(String s : PUNCTUATIONS)
line = line.replaceAll(s, "");
if(enalbeRemoveStopWords){
for(String s : STOP_WORDS)
line = line.replaceAll(s, "");
}
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
output.collect(location,new WordCountPair(word,1));
}
}
}
/*Rudece传进来的是 <filename,List(<word,frequency>) >*/
public static class Reduce extends MapReduceBase implements
Reducer<Text, WordCountPair, Text, Text> {
List<WordCountPair> list = new ArrayList<WordCountPair>();
public void reduce(Text key, Iterator<WordCountPair> values,
OutputCollector<Text, Text> output, Reporter reporter)
throws IOException {
list.removeAll(list);
/*get the statistics of all the <word,frequency>pair in a list*/
while (values.hasNext()) {
WordCountPair temp = values.next();
//System.out.println(temp);
int i;
if( (i=list.indexOf(temp)) != -1)
list.get(i).frequency++;
else
list.add(new WordCountPair(temp));
}
/*sort the list according to frequency and output the top 10*/
Collections.sort(list);
//System.out.println(list.toString());
StringBuilder topWords = new StringBuilder();
boolean first = true;
for(int i=0;i<Math.min(10, list.size());i++){
if(!first)
topWords.append(",");
first = false;
topWords.append(list.get(i).word.toString());
}
output.collect(key, new Text(topWords.toString()));
}
}
public static void main(String[] args) throws Exception {
if(args.length < 2){
System.err.println("Usage:Java TopWords input output [-stop]");
System.exit(1);
}
JobConf conf = new JobConf(TopWords.class);
conf.setJobName("TopWords");
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(WordCountPair.class);
conf.setMapperClass(Map.class);
conf.setReducerClass(Reduce.class);
conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);
FileInputFormat.setInputPaths(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path(args[1]));
if(args.length == 3){
if(args[2].equals("-stop"))
enalbeRemoveStopWords = true;
else{
System.err.println("Usage:Java TopWords input output [-stop]");
System.exit(1);
}
}
JobClient.runJob(conf);
}
}


import java.io.*;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
/**
* WordCountPair是自定义类型,需要实现Writable接口(如果作为Key的话,则需要实现WritableComparable接口
*/
public class WordCountPair implements Writable,Comparable<WordCountPair>{
public Text word;
public int frequency;
public WordCountPair(Text word, int frequency){
this.word = new Text(word);
this.frequency = frequency;
}
public WordCountPair(){
this(new Text(), 1);
}
public WordCountPair(WordCountPair wcp){
this(wcp.word,wcp.frequency);
}
public void readFields(DataInput in) throws IOException {
word.readFields(in);
frequency = in.readInt();
}
public void write(DataOutput out) throws IOException {
word.write(out);
out.writeInt(frequency);
}
@Override
public boolean equals(Object o) {
if (o instanceof WordCountPair) {
WordCountPair wcp = (WordCountPair) o;
return word.equals(wcp.word);
}
return false;
}
/* sort according to the frequency of the word,descending order*/
public int compareTo(WordCountPair other) {
return other.frequency - this.frequency;
}
public String toString() {
return word + "," + frequency;
}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-310566-1-1.html 上篇帖子: Nutch and Hadoop Tutorial Finished 下篇帖子: Hadoop为开发平台
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表