http://runtool.blog.163.com/blog/static/183144445201251625612309/
参考资料:
1.http://blog.csdn.net/forwen/article/details/4804733
2.SequenceFile介绍 :http://blog.163.com/jiayouweijiewj@126/blog/static/17123217720101121103928847/
3.http://blog.163.com/bit_runner/blog/static/53242218201141393943980/
4.http://blog.163.com/jiayouweijiewj@126/blog/static/171232177201011475716354/
1. Nutch是什么?
Nutch是一个开源的网页抓取工具,主要用于收集网页数据,然后对其进行分析,建立索引,以提供相应的接口来对其网页数据进行查询的一套工具。其底层使用了Hadoop来做分布式计算与存储,索引使用了Solr分布式索引框架来做,Solr是一个开源的全文索引框架,从Nutch 1.3开始,其集成了这个索引架构
2. 在哪里要可以下载到最新的Nutch?
在下面地址中可以下载到最新的Nutch 1.3二进制包和源代码
http://mirror.bjtu.edu.cn/apache//nutch/
3. 如何配置Nutch?
3.1 对下载后的压缩包进行解压,然后cd $HOME/nutch-1.3/runtime/local
3.2 配置bin/nutch这个文件的权限,使用chmod +x bin/nutch
3.3 配置JAVA_HOME,使用export JAVA_HOME=$PATH
4. 抓取前要做什么准备工作?
4.1 配置http.agent.name这个属性,如下
1. <pre name=“code” class=“html”><property>
2. <name>http.agent.name</name>
3. <value>My Nutch Spider</value>
4. </property>
4.2 建立一个地址目录,mkdir -p urls
在这个目录中建立一个url文件,写上一些url,如
1. http://nutch.apache.org/
4.3 然后运行如下命令
1. bin/nutch crawl urls -dir crawl -depth 3 -topN 5
注意,这里是不带索引的,如果要对抓取的数据建立索引,运行如下命令
1. bin/nutch crawl urls -solr http://localhost:8983/solr/ -depth 3 -topN 5
5. Nutch的抓取流程是什么样子的?
5.1 初始化crawlDb,注入初始url
1. <pre name=“code” class=“html”>bin/nutch inject
2. Usage: Injector <crawldb> <url_dir>
在我本地运行这个命令后的输出结果如下:
1. lemo@debian:~/Workspace/java/Apache/Nutch/nutch-1.3$ bin/nutch inject db/crawldb urls/
2. Injector: starting at 2011-08-22 10:50:01
3. Injector: crawlDb: db/crawldb
4. Injector: urlDir: urls
5. Injector: Converting injected urls to crawl db entries.
6. Injector: Merging injected urls into crawl db.
7. Injector: finished at 2011-08-22 10:50:05, elapsed: 00:00:03
5.2 产生新的抓取urls
1. bin/nutch generate
2. Usage: Generator <crawldb> <segments_dir> [-force] [-topN N] [-numFetchers numFetchers] [-adddays numDays] [-noFilter] [-noNorm][-maxNumSegments num]
本机输出结果如下:
1. lemo@debian:~/Workspace/java/Apache/Nutch/nutch-1.3$ bin/nutch generate db/crawldb/ db/segments
2. Generator: starting at 2011-08-22 10:52:41
3. Generator: Selecting best-scoring urls due for fetch.
4. Generator: filtering: true
5. Generator: normalizing: true
6. Generator: jobtracker is ’local’, generating exactly one partition.
7. Generator: Partitioning selected urls for politeness.
8. Generator: segment: db/segments/20110822105243 // 这里会产生一个新的segment
9. Generator: finished at 2011-08-22 10:52:44, elapsed: 00:00:03
5.3 对上面产生的url进行抓取
1. bin/nutch fetch
2. Usage: Fetcher <segment> [-threads n] [-noParsing]
这里是本地的输出结果:
1. lemo@debian:~/Workspace/java/Apache/Nutch/nutch-1.3$ bin/nutch fetch db/segments/20110822105243/
2. Fetcher: Your ’http.agent.name’ value should be listed first in ’http.robots.agents’ property.
3. Fetcher: starting at 2011-08-22 10:56:07
4. Fetcher: segment: db/segments/20110822105243
5. Fetcher: threads: 10
6. QueueFeeder finished: total 1 records + hit by time limit :0
7. fetching http://www.baidu.com/
8. -finishing thread FetcherThread, activeThreads=1
9. -finishing thread FetcherThread, activeThreads=
10. -finishing thread FetcherThread, activeThreads=1
11. -finishing thread FetcherThread, activeThreads=1
12. -finishing thread FetcherThread, activeThreads=0
13. -activeThreads=0, spinWaiting=0, fetchQueues.totalSize=0
14. -activeThreads=0
15. Fetcher: finished at 2011-08-22 10:56:09, elapsed: 00:00:02
我们来看一下这里的segment目录结构
1. lemo@debian:~/Workspace/java/Apache/Nutch/nutch-1.3$ ls db/segments/20110822105243/
2. content crawl_fetch crawl_generate
5.4 对上面的结果进行解析
1. <pre name=“code” class=“html”>bin/nutch parse
2. Usage: ParseSegment segment
本机输出结果:
1. <pre name=“code” class=“html”>lemo@debian:~/Workspace/java/Apache/Nutch/nutch-1.3$ bin/nutch parse db/segments/20110822105243/
2. ParseSegment: starting at 2011-08-22 10:58:19
3. ParseSegment: segment: db/segments/20110822105243
4. ParseSegment: finished at 2011-08-22 10:58:22, elapsed: 00:00:02
我们再来看一下解析后的目录结构
1. <pre name=“code” class=“html”>lemo@debian:~/Workspace/java/Apache/Nutch/nutch-1.3$ ls db/segments/20110822105243/
2. content crawl_fetch crawl_generate crawl_parse parse_data parse_text
这里多了三个解析后的目录。
5.5 更新外链接数据库
1. bin/nutch updatedb
2. Usage: CrawlDb <crawldb> (-dir <segments> | <seg1> <seg2> …) [-force] [-normalize] [-filter] [-noAdditions]
本机输出结果:
1. <pre name=“code” class=“html”>lemo@debian:~/Workspace/java/Apache/Nutch/nutch-1.3$ bin/nutch updatedb db/crawldb/ -dir db/segments/
2. CrawlDb update: starting at 2011-08-22 11:00:09
3. CrawlDb update: db: db/crawldb
4. CrawlDb update: segments: [file:/home/lemo/Workspace/java/Apache/Nutch/nutch-1.3/db/segments/20110822105243]
5. CrawlDb update: additions allowed: true
6. CrawlDb update: URL normalizing: false
7. CrawlDb update: URL filtering: false
8. CrawlDb update: Merging segment data into db.
9. CrawlDb update: finished at 2011-08-22 11:00:10, elapsed: 00:00:01
这时它会更新crawldb链接库,这里是放在文件系统中的,像taobao抓取程序的链接库是用redis来做的,一种key-value形式的NoSql数据库。
5.6 计算反向链接
1. <pre name=“code” class=“html”>bin/nutch invertlinks
2. Usage: LinkDb <linkdb> (-dir <segmentsDir> | <seg1> <seg2> …) [-force] [-noNormalize] [-noFilter]
本地输出结果:
1. <pre name=“code” class=“html”>lemo@debian:~/Workspace/java/Apache/Nutch/nutch-1.3$ bin/nutch invertlinks db/linkdb -dir db/segments/
2. LinkDb: starting at 2011-08-22 11:02:49
3. LinkDb: linkdb: db/linkdb
4. LinkDb: URL normalize: true
5. LinkDb: URL filter: true
6. LinkDb: adding segment: file:/home/lemo/Workspace/java/Apache/Nutch/nutch-1.3/db/segments/20110822105243
7. LinkDb: finished at 2011-08-22 11:02:50, elapsed: 00:00:01
5.7 使用Solr为抓取的内容建立索引
1. bin/nutch solrindex
2. Usage: SolrIndexer <solr url> <crawldb> <linkdb> (<segment> … | -dir <segments>
Nutch端的输出如下:
1. lemo@debian:~/Workspace/java/Apache/Nutch/nutch-1.3$ bin/nutch solrindex http://127.0.0.1:8983/solr/ db/crawldb/ db/linkdb/ db/segments/*
1. SolrIndexer: starting at 2011-08-22 11:05:33
1. SolrIndexer: finished at 2011-08-22 11:05:35, elapsed: 00:00:02
Solr端的部分输出如下:
1. INFO: SolrDeletionPolicy.onInit: commits:num=1
2. commit{dir=/home/lemo/Workspace/java/Apache/Solr/apache-solr-3.3.0/example/solr/data/index,segFN=segments_1,version=1314024228223,generation=1,filenames=[segments_1]
3. Aug 22, 2011 11:05:35 AM org.apache.solr.core.SolrDeletionPolicy updateCommits
4. INFO: newest commit = 1314024228223
5. Aug 22, 2011 11:05:35 AM org.apache.solr.update.processor.LogUpdateProcessor finish
6. INFO: {add=[http://www.baidu.com/]} 0 183
7. Aug 22, 2011 11:05:35 AM org.apache.solr.core.SolrCore execute
8. INFO: [] webapp=/solr path=/update params={wt=javabin&version=2} status=0 QTime=183
9. Aug 22, 2011 11:05:35 AM org.apache.solr.update.DirectUpdateHandler2 commit
10.INFO: start commit(optimize=false,waitFlush=true,waitSearcher=true,expungeDeletes=false)
5.8 在Solr的客户端查询
在浏览器中输入
1. http://localhost:8983/solr/admin/
查询条件为baidu
输出的XML结构为
如果你要以HTML结构显示把Solr的配置文件solrconfig.xml中的content改为如下就可以
<field name=”content” type=”text”
stored=”true” indexed=”true”/>
1. </pre><blockquote style=“margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 40px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ”><div><blockquote style=“margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 40px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ”><div></div></blockquote></div></blockquote><p></p><span style=“white-space:pre”></span><pre name=“code” class=“html”> <pre name=“code” class=“html”><response>
2. <lst name=“responseHeader”>
3. <int name=“status”>0</int>
4. <int name=“QTime”>0</int>
5. <lst name=“params”>
6. <str name=“indent”>on</str>
7. <str name=“start”>0</str>
8. <str name=“q”>baidu</str>
9. <str name=“version”>2.2</str>
10.<str name=“rows”>10</str>
11.</lst>
12.</lst>
13.<result name=“response” numFound=“1″ start=“0″>
14.<doc>
15.<float name=“boost”>1.0660036</float>
16.<str name=“digest”>7be5cfd6da4a058001300b21d7d96b0f</str>
17.<str name=“id”>http://www.baidu.com/</str>
18.<str name=“segment”>20110822105243</str>
19.<str name=“title”>百度一下,你就知道</str>
20.<date name=“tstamp”>2011-08-22T14:56:09.194Z</date>
21.<str name=“url”>http://www.baidu.com/</str>
22.</doc>
23.</result>
24.</response>
1. <pre name=“code” class=“html”><blockquote style=“margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 40px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ”><pre name=“code” class=“html”>
6 参考
http://wiki.apache.org/nutch/RunningNutchAndSolr
作者:http://blog.csdn.net/amuseme_lu
=====================
http://haomou.net/?p=1212
Nutch 1.6 入门安装配置(集成solr)
测试环境 kubuntu12.04 jdk1.7.0_15 nutch 1.6 solr3.6.2
介绍
apachen nutch 是一个用java写的开源网络爬虫。使用它我们可以自动找到超链接,并且减少很多维护工作。例如,检测坏的链接,将爬过的网站copy下来。solr是一个开源的全文检索框架,使用它我们可以搜索nutch抓去来的网页。集成Nutch和solr是一件非常简单的事情。
apache nutch 支持solr的盒外集成(out-the-box),非常简单。nutch也不再tomcat来运行以前的那个web程序了,并且不用lucene来检索了。
步骤:
1.安装Nutch(二进制发行版)
首先去官网下载二进制包(apache-nutch-1.6-bin.zip), 解压缩,将出现apache-nutch-1.6文件夹,进入文件夹cd apache-nutch-1.6。从现在起我们将用${NUTCH_RUNTIME_HOME}代表 apache-nutch-1.6。
2.验证是否安装正确
运行 bin/nutch 你看见如下文字说明安装正确了
Usage: nutch [-core] COMMAND
如果出现permission denied 那么说明没有运行权限,给其加上运行权限chmod +x bin/nutch。
如果看见JAVA_HOME not set,那么说明你的电脑没有安装jdk或是没有设置JAVA_HOME。安装jdk很简单这里就不说了。
3.抓取你的第一个网站
在conf/nutch-site.xml文件中添加你的代理名字
<property>
<name>http.agent.name</name>
<value>My Nutch Spider</value>
</property>
——————————————-示例—————————————————————–
<?xml version=”1.0″?>
<?xml-stylesheet type=”text/xsl” href=”configuration.xsl”?>
<!– Put site-specific property overrides in this file. –>
<configuration>
<property>
<name>http.agent.name</name>
<value>oscar</value>
<description>HTTP ’User-Agent’ request header. MUST NOT be empty -
please set this to a single word uniquely related to your organization.
NOTE: You should also check other related properties:
http.robots.agents
http.agent.description
http.agent.url
http.agent.email
http.agent.version
and set their values appropriately.
</description>
</property>
</configuration>
————————————————————————————————————–
创建一个urls文件夹,mkdir -p urls 然后进入文件夹,创建一个文本文件seed.txt。touch seed.txt。在文件中写入
http://nutch.apache.org/它是我们想要抓取的网站。然后编辑conf/regex-urlfilter.txt把
# accept anything else
+.
替换成
+^http://([a-z0-9]*\.)*nutch.apache.org/这样他只会抓取nutch.apache.org域名里面的网页。
3.1 使用crawl命令
bin/nutch crawl urls -dir crawl -depth 3 -topN 5
其中,
urlDir就是种子url的目录地址
-solr <solrUrl>为solr的地址(如果没有则为空)
-dir 是保存爬取文件的目录
-threads 是爬取线程数量(默认10)
-depth 是爬取深度 (默认5)
-topN 是访问的广度 (默认是Long.max)
运行完成后你将看到这些目录产生了
crawl/crawldb
crawl/linkdb
crawl/segments
4.部署搜索用的solr
去solr官网下载二进制的文件,解压缩下载的文件。将得到apache-solr-3.6文件夹,接下来我们将用${APACHE_SOLR_HOME}代表该目录。进入${APACHE_SOLR_HOME}/example。然后运行如下命令java -jar start.jar
5.验证安装是否正确
带开浏览器在地址兰中输入
http://localhost:8983/solr/admin/
http://localhost:8983/solr/admin/stats.jsp看到如下界面说明安装成功了
6.集成nutch和solr
现在我们已经把solr和nutch都安装好了。nutch也已经抓取了数据。接下来将用solr搜索抓取到的链接。
运行如下命令
cp ${NUTCH_RUNTIME_HOME}/conf/schema.xml ${APACHE_SOLR_HOME}/example/solr/conf/
重启solr
运行solr索引命令
bin/nutch solrindex http://127.0.0.1:8983/solr/ crawl/crawldb -linkdb crawl/linkdb crawl/segments/*
这个命令将把抓取到的数据发送到solr进行索引。
如果一切进行顺利的话,我们现在可以在 http://localhost:8983/solr/admin/进行搜索了。
如果你想要看到原始的HTML,改变schema.xml文件
<field name=”content” type=”text” stored=”true” indexed=”true”/>
六22013 年 6 月 2 日 • By 曹欢欢 • This entry was posted in 搜索引擎 and tagged nutch, solr. Bookmark the permalink.
Post navigation
← Apache nutch1.5 & Apache solr3.6