---
title: "nutch分布式搭建"
author: "iammonster"
date: "2010-04-06T17:54:09Z"
tags: []
categories: ["企业架构"]
slug: "nutch"
draft: false
lastmod: "2010-04-06T17:59:35Z"
id: 634791
url: "https://jiajun.iteye.com/blog/634791"
---

**如何在eclipse中跑nutch** ：http://jiajun.iteye.com/blog/612023 这里没有翻译，不过应该能看懂

 

那么**如何分布式搭建nutch1.0** 呢？

 

如果进行到这篇文章，就很容易了：**hadoop搭建进阶，多台机器分布式搭建** ：http://jiajun.iteye.com/blog/624559

 

具体如何做呢，参看：**How to Setup Nutch and Hadoop** ：http://wiki.apache.org/nutch/NutchHadoopTutorial

 

可惜的是现在的nutch-1.0使用的hadoop架构不是0.20.2，而是使用的0.19.1，配置文件上有些差别。

 

注意：如果要本地调试nutch1.0，那么需要把conf/hadoop-site.xml删除或者改为其他名字，否则的话就是配置好的环境下跑的nutch，某些在从机器上运行的程序不能够调试。

 

下面说说，如何搭建nutch1.0

 

一些环境这里就不介绍了，这篇文章是接着上面几篇文章的。

 

**一、下载安装文件**

 

1、下载目前最新版本 nutch-1.0：http://lucene.apache.org/nutch

2、下载tomcat6.0:http://tomcat.apache.org

3、解压下载的两个压缩文件到 /home/java

 

**二、配置文件**

 

**1、修改conf/hadoop-site.xml** （配置文件和前面的hadoop-0.20.2不一样，这里的版本0.19.1不够先进）

```
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>

<!-- Put site-specific property overrides in this file. -->

<configuration>

        <property>
                <name>fs.default.name</name>
                <value>hdfs://home0.hadoop:9000</value>
        </property>

        <property>
                <name>mapred.job.tracker</name>
                <value>home0.hadoop:9001</value>
     </property>

        <property>
                <name>dfs.name.dir</name>
                <value>/home/lighttpd/hadoopfs/name_0,/home/lighttpd/hadoopfs/name_1</val       ue>
                 <description>Determines where on the local filesystem the DFS name node
                        should store the name table. If this is a comma-delimited list of        directories
                        then the name table is replicated in all of the directories,
                        for redundancy.
                </description>
        </property>

        <property>
                <name>dfs.data.dir</name>
                <value>/home/lighttpd/hadoopfs/data_0,/home/lighttpd/hadoopfs/data_1</val       ue>
                <description>
                        Determines where on the local filesystem an DFS data node
                        should store its blocks. If this is a comma-delimited list of dir       ectories,
                        then data will be stored in all named directories, typically on d       ifferent devices.
                        Directories that do not exist are ignored.
                </description>
        </property>


</configuration>
```

 **2、修改 JAVA\_HOME变量指向**

    在这个文件中hadoop-env.sh修改，前面配置hadoop已经讲过了。

 

**4、设定主从节点**  
    修改 conf/masters 文件，将其中的 localhost 改为 home0.hadoop 。

    修改 conf/slaves 文件。 删掉其中的localhost, 将我们的另两台机器 home1.hadoop、home2.hadoop 加入, 注意每个机器一行。

    masters里面的是主机节点，slaves里面是从机节点。

 

**5、同样的配置拷贝到其他两台机器上去**

```
scp -r nutch1.0 home1.hadoop:/home/yourname/
```

 注意：可能您每台机器的JAVA安装位置不一样，记得修改conf/hadoop-env.sh,设置export JAVA\_HOME=正确路径

 

**三、启动hadoop服务**

 

**1、** **格式化一个新的分布式文件系统**

```
bin/hadoop namenode -format
```

 **2、** **启动hadoop进程**

```
$ bin/start-all.sh
```

 

**四、抓取、建立索引**

 

**1、配置抓取入口**

    在nutch1.0目录下新建文件夹：urls，在该文件夹下新建文件：urllist.txt，在该文件中写入抓取列表，比如：

```
http://lucene.apache.org
```

   写入分布式文件系统

```
bin/hadoop fs -put urls urls
```

 

**2、设置URL过滤规则**

    编辑conf/crawl-urlfilter.txt

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

 

**3、执行抓取命令**

 

```
bin/nutch crawl urls -dir crawledDir -depth 3 -threads 10
```

 

**五、搜索环境**

 

**1** **、把上面索引后的文件拷贝到本地** ，速度会好些，也可在分布式系统中，参考文档中讲到

```
bin/hadoop fs -copyToLocal crawledDir  /home/yourname/nutch-1.0/crawledData
```

 

2、**把 nutch-1.0.war 文件拷贝到tomcat下webapps目录，启动tomcat解压文件,再关闭服务**

```
$ cp nutch-1.0.war /home/yourname/java/apache-tomcat-6.0.18/
$ cd  /home/lighttpd/java/apache-tomcat-6.0.18/
$ bin/startup.sh
浏览器输入：http://locahost:8080/nutch-1.0 
$ bin/shutdown.sh
```

 

**3、修改/WEB-INF/classes/nutch-site.xml**

```
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>

<!-- Put site-specific property overrides in this file. -->
<!--  /home/lighttpd/nutch-1.0/conf/search-dir/sina.com.cn -->
<configuration>

        <property>
                <name>searcher.dir</name>
                <value>/home/yourname/nutch-1.0/crawledDir</value>
        </property>

</configuration>
```

**4、支持中文** ，在server.xml的 Connector中加入属性

```
URIEncoding="UTF-8" 
useBodyEncodingForURI="true"
```

 

**5、启动tomcat**

```
$ cd  /home/lighttpd/java/apache-tomcat-6.0.18/
$ bin/startup.sh
```

 

**6、浏览器输入：http://locahost:8080/nutch-1.0 搜索试试**

 

**六、注意：**

 

1、这里搜索的索引文件是拷贝到本地的文件，也可以搭建分布式索引文件

2、还可以搭建索引文件在不同的机器上，搜索时候从各个索引文件合并搜索结果

3、eclipse工程中的对应配置文件，都做设置的话，会运行在分布式系统中，如想调试运行在从机器上程序，需要采用本地运行方式，把hadoop-site.xml文件挪走，或者改名。