看nutch学习hadoop的编程
来源:互联网 发布:重庆淘宝设计运营培训 编辑:程序博客网 时间:2024/05/17 03:52
刚下了最新版本的nutch1.0.*,发现nutch的搜索已经转由solr来实现了。nutch上有很多hadoop的应用,可以作为案例学习,看人家如何使用hadoop来实现,这对于刚接触hadoop编程的人来说,这是一个比较好的选择,怎么说nutch也算是hadoop的起源地。。。新版本的nutch使用的hadoop也是比较新的版本。。
看一下nutch的index模块,使用的hadoop的mapreduce方式来实现建索引:
map: 从序列化的文件解析出key与value值。
public void map(Textkey,Writablevalue,
OutputCollector<Text,NutchWritable>output,Reporter reporter) throws IOException {
output.collect(key,new NutchWritable(value));
}
reduce:key为文档主键,value为文档+对应操作
public void reduce(Textkey,Iterator<NutchWritable>values,
OutputCollector<Text,NutchIndexAction>output,Reporter reporter)
throws IOException {
.......reporter.incrCounter("IndexerStatus","Documents added",1);
NutchIndexAction action = new NutchIndexAction(doc,NutchIndexAction.ADD);
output.collect(key,action);
.......
}
map只是简单的将输入文件简单的收集起来,而reduce也只是封装了文档数据,这样大量的输入源可以并发处理封装数据源。真正提交索引的是在FileOutputFormat上操作,reduce中的key值是文档主键,value封装了是文档与之对应的操作
public classIndexerOutputFormatextendsFileOutputFormat<Text,NutchIndexAction> {
@Override
public RecordWriter<Text,NutchIndexAction> getRecordWriter(FileSystemignored,
JobConf job, String name, Progressable progress) throws IOException {
// populate JobConf with field indexing options
IndexingFilters filters =new IndexingFilters(job);
finalNutchIndexWriter[]writers =
NutchIndexWriterFactory.getNutchIndexWriters(job);
//初始化每个witer
for (finalNutchIndexWriterwriter :writers) {
writer.open(job,name);
}
return new RecordWriter<Text,NutchIndexAction>() {
public void close(Reporterreporter)throws IOException {
for (final NutchIndexWriter writer : writers) {
writer.close();
}
}
//处理每个记录
public void write(Textkey,NutchIndexActionindexAction) throws IOException {
for (final NutchIndexWriter writer : writers) {
if (indexAction.action ==NutchIndexAction.ADD) {
writer.write(indexAction.doc);
}
if (indexAction.action ==NutchIndexAction.DELETE) {
writer.delete(key.toString());
}
}
}
};
}
}
处理每一个reduce后的记录:RecordWriter,调用 public void write(Text key, NutchIndexAction indexAction) 。nutch只是简单地使用solr的提交索引方式 ,实现的SolrWriter
首先初始化:
public void open(JobConfjob,Stringname)throws IOException {
SolrServerserver =SolrUtils.getCommonsHttpSolrServer(job);
init(server, job);
}
处理每条记录,首先是删除操作:
public void delete(Stringkey)throws IOException {
if (delete) {
try {
solr.deleteById(key);
numDeletes++;
} catch (finalSolrServerExceptione) {
throwmakeIOException(e);
}
}
}
增加操作:将增加的文档放到缓冲区上,达到一定数量时才进行提交 或者结束前调用
inputDocs.add(inputDoc);
if (inputDocs.size() +numDeletes >=commitSize) {
try {
LOG.info("Indexing " +Integer.toString(inputDocs.size()) +" documents");
LOG.info("Deleting " +Integer.toString(numDeletes) +" documents");
numDeletes = 0;
UpdateRequest req =new UpdateRequest();
req.add(inputDocs);
req.setParams(params);
req.process(solr);
} catch (finalSolrServerExceptione) {
throwmakeIOException(e);
}
inputDocs.clear();
}
:
- 看nutch学习hadoop的编程
- nutch-1.7-学习笔记(2)-org.apache.nutch.crawl.Generator.java-关于Hadoop的partition
- nutch-1.7-学习笔记(2)-org.apache.nutch.crawl.Generator.java-关于Hadoop的partition
- Hadoop-Nutch学习整理(持续更新)
- nutch+hadoop
- nutch+hadoop
- nutch hadoop
- 基于Nutch和Hadoop的简易搜索引擎
- Nutch 1.3 学习笔记 12 Nutch 2.0 的主要变化
- Nutch 1.3 学习笔记 12 Nutch 2.0 的主要变化
- 学习Nutch
- 学习nutch
- nutch学习
- nutch学习
- nutch+hadoop环境搭建
- nutch与hadoop
- Nutch+Hadoop集群搭建
- Nutch+Hadoop集群搭建
- grep不查找隐藏文件夹的方法
- JCS---一个实现
- 普通Java类获取Spring的ApplicationContext
- java inputstream 读取数据
- 对不起,你的服务器没有安装动易组件(PE_Article6.dll),所以不能使用动易系统。"---解决方
- 看nutch学习hadoop的编程
- zoj一道题:fire net 给的测试用例都通过就是不能ac求助测试
- 键盘事件解析
- CentOS 更改MySQL数据库目录位置
- 汇编语言编写十进制数到二进制数的进制转换并显示的子程序
- Hibernate读书笔记-----Hibernate的关联映射之N-1关联映射
- wifi详解(二)
- Magento 保存Authorized.net 的信用卡号
- XML解析