两周前,我参考Hadoop权威指南2,写了一个读取SequenceFile的类,想读取Nutch抓取下来的segments文件。当时如果Nutch抓取的是utf8编码的中文页面就可以正常读取,但如果网页是中文编码的话就会出现乱码,比如我抓取了www.163.com,用
bin/nutch crawl urls/ -dir crawl/www.163.com/ -depth 3 -topN 50 -thread 5
由于163是用的gb2312编码的,读取crawl/www.163.com/segments/下面的文件就有乱码。同样可以用readseg一试
bin/nutch readseg -dump crawl/www.163.com/segments/20120513192613/ crawl/www.163.com/readseg-result1
查看readseg-result1/dump文件发现Content::中的网页源码信息中的中文是乱码,但观察其上方的ParseData::和ParseText::却不是乱码,于是我看了Nutch的readseg命令的源代码:org.apache.nutch.segment.SegmentReader
找到其map和reduce方法,想进行改造重新实现一个MySegmentReader,在map方法:
public void map(WritableComparable key, Writable value, OutputCollector<Text, NutchWritable> collector,
Reporter reporter) throws IOException {
// convert on the fly from old formats with UTF8 keys.
// UTF8 deprecated and replaced by Text.
if (key instanceof Text) {
newKey.set(key.toString());
key = newKey;
}
collector.collect((Text) key, new NutchWritable(value));
}
和reduce方法:
public void reduce(Text key, Iterator<NutchWritable> values, OutputCollector<Text, Text> output, Reporter reporter)
throws IOException {
StringBuffer dump = new StringBuffer();
dump.append("\nRecno:: ").append(recNo++).append("\n");
dump.append("URL:: " + key.toString() + "\n");
while (values.hasNext()) {
Writable value = values.next().get(); // unwrap
if (value instanceof CrawlDatum) {
dump.append("\nCrawlDatum::\n").append(((CrawlDatum) value).toString());
} else if (value instanceof Content) {
dump.append("\nContent::\n").append(((Content) value).toString());
} else if (value instanceof ParseData) {
dump.append("\nParseData::\n").append(((ParseData) value).toString());
} else if (value instanceof ParseText) {
dump.append("\nParseText::\n").append(((ParseText) value).toString());
} else if (LOG.isWarnEnabled()) {
LOG.warn("Unrecognized type: " + value.getClass());
}
}
output.collect(key, new Text(dump.toString()));
}
我尝试了多种转换编码的方式,然后重新执行MySegmentReader,却总是得到同样的结果Content::中的网页源码信息中的中文始终是乱码。
我想为什么不直接用ParseText::中的编码正确的信息呢?
根据reduce中
else if (value instanceof ParseText) {
dump.append("\nParseText::\n").append(((ParseText) value).toString());
}
的提示,我想可以重新写一个解析类的reduce方法,专门用于生成这种纯文本的ParseText的文件,然后交给Mahout的org.apache.mahout.text.SequenceFilesFromDirectory类
去处理。
今天我又用之前写的读取SequenceFile的类来读取了crawl/www.163.com/segments/20120513192613/parse_text/part-00000/data的信息,就是正常的无乱码的文本:
bin/hadoop jar ~/Desktop/SequenceFileReader.jar segments/20120513192613/parse_text/part-00000/data segdata-parse-text
产生的结果如下:
[7261*] http://news.163.com/12/0511/16/8183GF9B0001124J.html
分享到:
相关推荐
apache-nutch-2.3.1-src.tar.gz
nutch1.5-官方包,网络爬虫。
nutch配置nutch-default.xml
apache-nutch-1.3 的源码包,需要的可以看下
nutch不用安装,是个应用程序,下载后为nutch-1.6.tar.gz,双击桌面上的cygwin快捷方式;执行以下命令: $ cd D:/Downloads/Soft $ tar zxvf nutch-1.0.tar.gz 在e盘下面出现nutch-0.9文件夹说明解压成功了.然后环境...
Nutch是一款刚刚诞生的完整的开源搜索引擎系统,可以结合数据库进行索引,能快速构建所需系统。Nutch 是基于Lucene的,Lucene为 Nutch 提供了文本索引和搜索的API,所以它使用Lucene作为索引和检索的模块。Nutch的...
Nutch 是一个开源Java 实现的搜索引擎。这里是它的安装包。
nutch-param-setnutch-param-setnutch-param-setnutch-param-set
这是未编译nutch的源码。可以配合这个博客:http://www.cnblogs.com/xxx0624/p/4172601.html
apache-nutch-2.3.1-src.tar ,网络爬虫的源码, 用ivy2管理, ant runtime 编译 apache-nutch-2.3.1-src.tar ,网络爬虫的源码, 用ivy2管理, ant runtime 编译
apache-nutch-1.6-src.tar.gz 来自APACHE官网,本人亲自测试可以使用。
apache-nutch-1.4-bin.tar.gz.part2
一个开源Java 实现的搜索引擎nutch
nutch-1.0-dev.jar nutch devlope
apache-nutch-1.4-bin.part2
apache-nutch-1.4-bin.part1
Nutch是一个由Java实现的,刚刚诞生开放源代码(open-source)的web搜索引擎。Nutch目前最新的版本为version1.4。这个为nutch的最新版 1.4。
apache-nutch-2.3-src.zip来自APACHE官方网站,亲自测试可以使用。
apache-nutch-1.4-bin.tar.gz.part1