【Nutch】Linux下單機配置Nutch .

來源:互聯網
上載者:User

  

1.環境介紹

作業系統:Red hat linux 9

Nutch版本:nutch-0.9,下載:http://apache.etoak.com/lucene/nutch/

JDK版本:JDK 1.6

Apache Tomcat版本:apache-tomcat-6.0.18

http://apache.etoak.com/tomcat/tomcat-6/v6.0.18/bin/apache-tomcat-6.0.18.tar.gz

2.配置前提2.1 安裝jdk 1.6

首先下載jdk安裝包jdk-1_6_0_13-linux-i586-rpm.bin

第一步:# chmod +x  jdk-1_6_0_13-linux-i586-rpm.bin (獲得執行許可權)

第二步:# ./jdk-1_6_0_13-linux-i586-rpm.bin (產生rpm安裝包)

第三步:# rpm -ivh jdk-1_6_0_13-linux-i586.Rpm(安裝JDK)

        安裝完畢後,jdk預設安裝在/usr/java/目錄下

第四步:配置JAVA環境變數。

在/etc/profile中設定環境變數

[root@red-hat-9 root]# vi /etc/profile

加入以下語句:

JAVA_HOME=/usr/java/jdk1.6.0_13

export JAVA_HOME

CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib        (注意是冒號)

export CLASSPATH

PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH

 

[root@red-hat-9 root]# chmod +x /etc/profile (執行許可權)

[root@red-hat-9 root]# source /etc/profile (此後設定有效)

2.2 安裝Tombact

第一步:設定環境變數(不是必須的)

[root@red-hat-9 program]# vi /etc/profile

    export JDK_HOME=$JAVA_HOME

[root@red-hat-9 program]# source /etc/profile

第二步:安裝tomnact,解壓到某目錄下即可

tar xf apache-tomcat-6.0.18.tar.gz

mv  apache-tomcat-6.0.18 /zkl/progaram/

第三步:如何使用Apache Tomcat

①首先啟動Tomcat,只需執行以下命令

# /zkl/program/apache-tomcat-6.0.18/bin/startup.sh

②Tomcat的網頁主目錄是/zkl/program/apache-tomcat-6.0.18/webapps/,只需在webapps目錄中添加相應網頁即可在瀏覽器訪問,Tomcat預設目錄是webapps下的ROOT目錄。

http://127.0.0.1:8080/  訪問tomcat預設主目錄,ROOT

http://127.0.0.1:8080/luceneweb 將luceneweb放入webapps中

③Apache http伺服器的連接埠是 80,http://127.0.0.1訪問的是Apache主目錄

  Apache Tomcat伺服器連接埠是8080,二者不衝突,若有衝突,則可以修改tomcat設定檔server.xml

vi /zkl/program/apache-tomcat-6.0.18/conf/server.xml

<!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
    <Connector port="8080" maxHttpHeaderSize="8192"
        maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
        enableLookups="false" redirectPort="8443" acceptCount="100"
        c disableUploadTimeout="true"
        URIEncoding="UTF-8" useBodyEncodingForURI="true" />   

預設服務連接埠為8080,若有衝突(如Apache),則可通過此設定檔更改連接埠(藍色);如果配置後nutch出現中文亂碼問題,則增加編碼配置(紅色)

 

3.配置和應用Nutch3.1 配置Nutch

首先,下載Nutch-0.9.tar.gz;

第一步,解壓安裝包

#tar zxvf Nutch-0.9.tar.gz
#mv Nutch-0.9 /zkl/ir/nutch-0.9

第二步,測試Nutch

    # /zkl/ir/nutch-0.9/bin/nutch

若出現以下文字,則安裝成功:

Usage: nutch COMMAND

where COMMAND is one of:

  crawl             one-step crawler for intranets

  readdb            read / dump crawl db

第三步,設定Nutch

①設定抓取網站的入口網址

[root@red-hat-9 nutch-0.9]# cd /zkl/ir/nutch-0.9/

[root@red-hat-9 nutch-0.9]# mkdir urls

[root@red-hat-9 nutch-0.9]# vi urls/urls_crawl.txt

或者不用建立目錄,直接建立一個檔案urls_crawl.txt,我們採用此法,

[root@red-hat-9 nutch-0.9]# vi urls_crawl.txt

寫入要抓取(crawl)網站的入口網址,即從此入口開始抓取當前網域名稱下的任何URL頁面,例如:

http://english.gu.cas.cn/ag/

 

②指定爬取過濾規則

編輯nutch的URL過濾規則檔案conf/crawl-urlfilter.txt

[root@red-hat-9 nutch-0.9]# vi conf/crawl-urlfilter.txt

修改

# accept hosts in MY.DOMAIN.NAME
# +^http://(/[a-z0-9]*/.)*MY.DOMAIN.NAME/

這是你想要爬取網站的網域名稱,表示爬取當前網站下的所有URL頁面,爬取起始網站在中已經設定。

③過濾字元設定

如果爬取網站的url含有以下過濾字元,如 ? 和 = ,而你又需要這些訪問,可以更改過濾表

# skip URLs containing certain characters as probable queries, etc.
-[?*!@=]
改為
-[*!@]

④修改conf/nutch-site.xml

修改為

<configuration>
        <property>
                <name>http.agent.name</name>     http.agent.name屬性
                <value>gucas.ac.cn</value>    該值是被抓取網站的名稱,自己設定,

在nutch檢索中會用到
        </property>
        <property>
                <name>http.agent.version</name>
                <value>1.0</value>
        </property>

<property>

           <name>searcher.dir</name>

           <value>/zkl/ir/nutch-0.9/gucas</value>

           <description> Path to root of crawl</description>

</property>
</configuration>
如果沒有配置此agent,爬取時會出現 Agent name not configured! 的錯誤。

⑤開始爬取

運行crawl命令抓取網站內容

[root@red-hat-9 nutch-0.9]# bin/nutch crawl urls_crawl.txt -dir gucas -depth 50 -threads 5 -topN 1000 >& logs/logs_crawl.log

 

·-dir   dirnames  設定儲存所抓取網頁的目錄.

·-depth  depth    表明抓取網頁的層次深度

·-delay  delay    表明訪問不同主機的延時,單位為“秒”

·-threads  threads   表明需要啟動的線程數

·-topN  1000   表明只抓取每一層的前N個URL

在上述命令的參數中,urls_crawl.txt 就是剛才建立的那個包含儲存了抓取網址的檔案urls_crawl.txt的目錄;dir指定抓取內容所存放的目錄,這裡是gucas;depth表示以要抓取網站頂級網址為起點的爬行深度;threads指定並發的線程數;topN表明只抓取每一層的前N個URL;最後的logs/ logs_crawl.log表示把抓取過程顯示的內容儲存在logs目錄下的檔案logs_crawl.log中,以便剖析器的運行情況。

此命令運行完後,將會在nutch-0.9目錄下產生gucas目錄,並存放有抓取的檔案和產生的索引,此外將會在nutch-0.9目錄下剩下logs目錄,此目錄下產生一個檔案logs_crawl.log,儲存的是抓取日誌。

如果gucas在運行前已存在,則運行時將報錯:gucas already exist。建議先刪除這個目錄,或者指定其他的目錄存放抓取的網頁。

完成上述的各步操作,此時資料的抓取順利完成了。

測試:bin/nutch org/apache/nutch/searcher/NutchBean the 

查詢關鍵字“the” 。

 

上面只是抓取單個網站,沒有體現網路蜘蛛從多個網站爬取資料的優點,下面舉例說明爬取多個網站資料時的情況:

在Nutch主目錄下建立檔案multiurls.txt檔案,寫入希望下載的網址列表

http://www.pcauto.com.cn/

http://www.xcar.com.cn/

http://auto.sina.com.cn

修改過濾規則檔案crawl-urlfilter.txt,允許下載任意網站

# accept hosts in MY.DOMAIN.NAME

+^                                  //預設允許所有網站連結

# skip everything else

-.

執行抓取命令

[root@red-hat-9 nutch-0.9]# bin/nutch crawl multiurls.txt -dir mutilweb -depth 50 -threads 5 -topN 1000 >& logs/logs_crawl.log

修改conf/nutch-site.xml

修改為:

<configuration>
        <property>
                <name>http.agent.name</name>     http.agent.name屬性
                <value>* </value>                該值是網路指蜘蛛名稱,
        </property>
        <property>
                <name>http.agent.version</name>
                <value>1.0</value>
        </property>

<property>

         <name>searcher.dir</name>

         <value>/zkl/ir/nutch-0.9/gucas</value>

         <description> Path to root of crawl</description>

</property>
</configuration>
    測試:bin/nutch org/apache/nutch/searcher/NutchBean SUV 

查詢關鍵字“SUV” 。

---------------------------------------------------------------------

⑥部署web前端
將nutch主目錄下的nutch-0.9.war 包拷貝到tomcat的webapps目錄下
[root@red-hat-9 nutch-0.9]# cp nutch-0.9.war /zkl/program/apache-tomcat-6.0.18/webapps/
然後瀏覽器網址 http://localhost:8080/nutch-0.9/ ,此時war包會自動解壓,在tomcat的網頁主目錄webapps下會出現一個nutch-0.9檔案夾。

⑦修改tomcat中nutch的web配置
vi /zkl/program/apache-tomcat-6.0.18/webapps//nutch-0.9/WEB-INF/classes/nutch-site.xml

searcher.dir屬性值更改為索引產生的目錄。

<configuration>

<property>

  <name>searcher.dir</name>

  <value>/zkl/ir/nutch-0.9/gucas</value>

  <description>

  Path to root of crawl.  This directory is searched (in

  order) for either the file search-servers.txt, containing a list of

  distributed search servers, or the directory "index" containing

  merged indexes, or the directory "segments" containing segment

  indexes.

  </description>

</property>

</configuration>

3.2 應用Nutch(無結果仍未解決)

重啟tomcat,

然後訪問網址http://localhost:8080/nutch-0.9/

錯誤集錦:

①輸入關鍵字,點擊搜尋發現出現錯誤

HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: /search.jsp(151,22) Attribute value  language + "/include/header.html" is quoted with " which must be escaped when used within the value
 org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)

 

這是因為 jsp2.0 的文法改變了,

根據提示把Tomcat目錄下的webapps/nutch-0.9目錄下的 serach.jsp 中的"<%=language+"/include /header.html"%>"/> 

改成

'<%=language+"/include /header.html"%>'/> 就可以了。

②抓取時出現以下錯誤

[root@red-hat-9 nutch-0.9]# bin/nutch crawl urls -dir gucas -depth 50
threads 5 >& logs/logs1.log

[root@red-hat-9 nutch-0.9]# cat logs/logs1.log

crawl started in: gucas

rootUrlDir = 5

threads = 10

depth = 50

Injector: starting

Injector: crawlDb: gucas/crawldb

Injector: urlDir: 5

Injector: Converting injected urls to crawl db entries.

Exception in thread "main" org.apache.hadoop.mapred.InvalidInputException: Input path doesnt exist : /zkl/ir/nutch-0.9/5

        at org.apache.hadoop.mapred.InputFormatBase.validateInput(InputFormatBase.java:138)

        at org.apache.hadoop.mapred.JobClient.submitJob(JobClient.java:326)

        at org.apache.hadoop.mapred.JobClient.runJob(JobClient.java:543)

        at org.apache.nutch.crawl.Injector.inject(Injector.java:162)

        at org.apache.nutch.crawl.Crawl.main(Crawl.java:115)

 

 

When you try the grep example in the QuickStart, you get an error like the following:

org.apache.hadoop.mapred.InvalidInputException:Input path doesnt exist : /user/ross/input

 

You haven't created an input directory containing one or more text files. 

 

bin/hadoop dfs -put conf input

這是因為命令中threads 5前面的IME錯誤所導致,

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.