Nutch2.2.1的配置(使用MySQL作为数据存储)

首先先从http://www.apache.org/dyn/closer.cgi/nutch/下载安装包

这里假定nutch的根目录为:${APACHE_NUTCH_HOME}

配置${APACHE_NUTCH_HOME}/ivy/ivy.xml,确保Nutch使用MySQL作为数据存储

[html]  view plain  copy
  1. <dependency org="org.apache.gora" name="gora-core" rev="0.3" conf="*->default"/>  
改成
[html]  view plain  copy
  1. <dependency org="org.apache.gora" name="gora-core" rev="0.2.1" conf="*->default"/>  
取消以下行的注释
[html]  view plain  copy
  1. <dependency org="org.apache.gora" name="gora-sql" rev="0.1.1-incubating" conf="*->default" />  
取消以下的行注释使用Mysql作为gora存储
[html]  view plain  copy
  1. <!-- Uncomment this to use MySQL as database with SQL as Gora store. -->  
  2. <dependency org="mysql" name="mysql-connector-java" rev="5.1.18" conf="*->default"/>  

编辑${APACHE_NUTCH_HOME}/conf/gora.properties 

添加以下代码激活MySQL的配置,

连接串url?后的参数中不指定字符集为utf8(characterEncoding=utf8),因为数据库表为utf8mb4,配置中不支持,若配置characterEncoding=utf8则会报错,若自动建表也可能出现字符集存储中文报错等问题,

导致错误情况详见:Nutch抓取错误java.sql.BatchUpdateException: Incorrect string value: '\xF2\xA3\xAC\xB7\xEF\xBF

最好手动建表,具体数据配置和建表过程详见: Nutch之MySQL数据库的配置

[html]  view plain  copy
  1. ###############################  
  2. # MySQL properties #  
  3. ###############################  
  4. gora.sqlstore.jdbc.driver=com.mysql.jdbc.Driver  
  5. gora.sqlstore.jdbc.url=jdbc:mysql://localhost:3306/nutch?createDatabaseIfNotExist=true  
  6. gora.sqlstore.jdbc.user=xxxxx  
  7. gora.sqlstore.jdbc.password=xxxxx  
编辑 ${APACHE_NUTCH_HOME}/conf/gora-sql-mapping.xml ,将主键的长度由512改成767
[html]  view plain  copy
  1. <primarykey column="id" length="767"/>  

配置${APACHE_NUTCH_HOME}/conf/nutch-site.xml 

在 http.agent.name字段下增加一个名字,可以是任意值但不能为空!  如果需要的话可以添加额外的语言(例如en为英语),同时也可以设置默认编码格式为utf-8

[html]  view plain  copy
  1. <property>  
  2. <name>http.agent.name</name>  
  3. <value>YourNutchSpider</value>  
  4. </property>  
  5.   
  6. <property>  
  7. <name>http.accept.language</name>  
  8. <value>ja-jp, en-us,en-gb,en;q=0.7,*;q=0.3</value>  
  9. <description>Value of the "Accept-Language" request header field.  
  10. This allows selecting non-English language as default one to retrieve.  
  11. It is a useful setting for search engines build for certain national group.  
  12. </description>  
  13. </property>  
  14.   
  15. <property>  
  16. <name>parser.character.encoding.default</name>  
  17. <value>utf-8</value>  
  18. <description>The character encoding to fall back to when no other information  
  19. is available</description>  
  20. </property>  
  21.   
  22. <property>  
  23. <name>storage.data.store.class</name>  
  24. <value>org.apache.gora.sql.store.SqlStore</value>  
  25. <description>The Gora DataStore class for storing and retrieving data.  
  26. Currently the following stores are available: ....  
  27. </description>  
  28. </property>  


在命令行下输入 sudo apt-get install ant 安装配置ant

在命令行界面使用cd切换到nutch的根目录

可以在终端中输入以下指令开始你的第一个爬虫工作


[html]  view plain  copy
  1. cd ${APACHE_NUTCH_HOME}/runtime/local  
  2. mkdir -p urls  
  3. echo 'http://nutch.apache.org/' > urls/seed.txt  



Nutch 2.2使用以下命令开始爬虫,设置线程数为30
[html]  view plain  copy
  1. bin/nutch crawl urls -threads 30  

要查看爬取的数据时,进入数据库中输入以下指令即可查看

mysql -u xxxxx -p
use nutch;
SELECT * FROM nutch.webpage;


翻译源:http://wiki.apache.org/nutch/#Nutch_2.X_tutorial.28s.29

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值