编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

Nutch2.3 + HBase 0.94 + Solr 搭建网络数据采集器!

wxchong 2024-06-30 10:41:03 开源技术 12 ℃ 0 评论

Nutch2.3 + HBase 0.94 + Solr 搭建网络数据采集器

介绍

大数据的火热使得人们越来越多地关注我们产生的数据,而爬虫作为数据采集的工具,为大数据提供了方便的获取途径。本文通过结合 Nutch、Solr 及 Hbase 向读者展示如何搭建属于自己的数据采集工具,笔者使用的系统为 Ubuntu 16.04,以下系统安装及编译均在该系统下完成并通过测试,需要的软件包及完整的配置文件可以通过本文的Git仓库地址获得。

术语

?Nutch - 数据采集器(爬取并解析网站数据)

?HBase - 分布式的存储系统,Hadoop 生态系统的组成之一

?Gora - Nutch 用于存储数据的抽象层

?Solr - 高性能的全文搜索服务器,提供数据查询的API接口,用于搜索Nutch爬取的数据。

软件依赖

?OpenJKD 8 & ant

?Nutch 2.3(版本必须为2.3,2.2笔者测试未通过)

?HBase 0.94.26(同样注意版本)

?Solr 4.8.0

通过 Ubuntu 系统自带的包管理器安装 OpenJDK 以及 ant,或者下载 .deb 安装文件自行安装,笔者推荐使用包管理器,可以避免安装过程中的依赖问题。

解压 Nutch 及 HBase 安装包至某一目录下。从现在开始我们通过$NUTCH_ROOT指代 Nutch 文件夹根目录,$HBASE_ROOT指代 HBase 文件夹的根目录。

配置 HBase

1.编辑$HBASE_ROOT/conf/hbase-site.xml文件并将添加

<configuration>

<property>

<name>hbase.rootdir</name>

<value>file:///full/path/to/where/the/data/should/be/stored</value>

</property>

<property>

<name>hbase.cluster.distributed</name>

<value>false</value>

</property>

</configuration>

2.编辑$HBASE_ROOT/conf/hbase-env.sh并将 JAVA_HOME 配置前的注释取消

-# export JAVA_HOME=/usr/java/jdk1.6.0/

+export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/

笔者实际测试中发现,在环境变量中设置了JAVA_HOME后无需在该文件中重新配置该变量。

3.启动 HBase

$HBASE_ROOT/bin/start-hbase.sh

编译 Nutch

Nutch1.x 从1.7版本开始不提供完整的部署文件,只提供源代码文件及相关的 build.xml 文件,这需要用户自己编译 Nutch。

1.编辑$NUTCH_ROOT/conf/gora.properties并添加 HBase 配置。

-#gora.datastore.default=org.apache.gora.mock.store.MockDataStore

+gora.datastore.default=org.apache.gora.hbase.store.HBaseStore

2.编译 Nutch

$ cd$NUTCH_ROOT

$ ant clean

$ ant runtime

编译需要下载一些依赖包,因此会占用大约10分钟左右时间,编译完成后,在$NUTCH_ROOT目录会生成两个文件夹: build 和 runtime。

1.修改 Nutch 配置文件$NUTCH_ROOT/runtime/local/conf/nutch-site.xml

<configuration>

<property>

<name>http.agent.name</name>

<value>mycrawlername</value><!-- this can be changed to something more sane if you like -->

</property>

<property>

<name>http.robots.agents</name>

<value>mycrawlername</value><!-- this is the robot name we're looking for in robots.txt files -->

</property>

<property>

<name>storage.data.store.class</name>

<value>org.apache.gora.hbase.store.HBaseStore</value>

</property>

<property>

<name>plugin.includes</name>

<!-- do **NOT** enable the parse-html plugin, if you want proper HTML parsing. Use something like parse-tika! -->

<value>protocol-httpclient|urlfilter-regex|parse-(text|tika|js)|index-(basic|anchor)|query-(basic|site|url)|response-(json|xml)|summary-basic|scoring-opic|urlnormalizer-(pass|regex|basic)|indexer-solr</value>

</property>

<property>

<name>db.ignore.external.links</name>

<value>true</value><!-- do not leave the seeded domains (optional) -->

</property>

</configuration>

2.配置 Nutch 集成 HBase,编辑$NUTCH_ROOT/runtime/local/conf/hbase-site.xml

<configuration>

<property>

<name>hbase.rootdir</name>

<value>file:///full/path/to/where/the/data/should/be/stored</value><!-- 与上节中 HBase 的配置相同 -->

</property>

<property>

<name>hbase.cluster.distributed</name>

<value>false</value>

</property>

</configuration>

集成Solr

1.下载 Solr 并解压至任意目录,运行 Solr

$ cd solr-4.8/example

$ java -jar start.jar

用浏览器打开 [http://localhost:8983/solr/admin/](http://localhost:8983/solr/admin/),如果页面正常显示,说明启动成功。

抓取数据

1.在$NUTCH_ROOT目录下创建一个空文件夹seed,并在该文件夹下创建文件urls.txt。

$ mkdir seed

$ echo"http://www.xiaotushuo.com">> seed/urls.txt

$ echo"http://cn.chinadaily.com.cn/">> seed/urls.txt

2.启动 HBase

$ $HBASE_ROOT/hbase/bin/hbase-start.sh

$ jps

通过jps命令可以查看 HBase 是否正常启动。

3.抓取数据并创建索引

$ $NUTCH_ROOT/runtime/local/bin/crawl seed/ TestCrawl http://localhost:8983/solr 2

4.打开 Solr 页面,输入关键词即可查询得数据。

命令帮助

?crawl 命令

$ cd runtime/local/bin

$ ./crawl

Missing seedDir : crawl <seedDir><crawlID><solrURL><numberOfRounds>

?notch 命令

./nutch

Usage: nutch COMMAND

where COMMAND is one of:

inject inject new urls into the database

hostinject creates or updates an existing host table from a text file

generate generate new batches to fetch from crawl db

fetch fetch URLs marked during generate

parse parse URLs marked during fetch

updatedb update web table after parsing

updatehostdb update host table after parsing

readdb read/dump records from page database

readhostdb display entries from the hostDB

elasticindex run the elasticsearch indexer

solrindex run the solr indexer on parsed batches

solrdedup remove duplicates from solr

parsechecker check the parser for a given url

indexchecker check the indexing filters for a given url

plugin load a plugin and run one of its classes main()

nutchserver run a (local) Nutch server on a user defined port

junit runs the given JUnit test

or

CLASSNAME run the class named CLASSNAME

Most commands print help when invoked w/o parameters.

文|原创:阡陌网络

------本文由卧龙会团队成员阡陌网络原创,请勿抄袭,转载联系卧龙会进行授权,否则追究法律责任。

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表