因此,您想从互联网上收集大量数据吗? 有什么比Cassandra更好的存储机制? 使用Nutch可以轻松做到这一点。
人们经常在Nutch后面使用Hbase。 这可行,但是如果您是(或想成为)Cassandra商店,则可能不是理想的解决方案。 幸运的是,Nutch 2+使用Gora抽象层访问其数据存储机制。 Gora支持Cassandra。 因此,对配置进行一些调整后,您可以使用Nutch将内容直接收集到Cassandra中。
我们将从Nutch 2.1开始……我想直接从源代码中获取:
$ git clone https://github.com/apache/nutch.git -b 2.1
...
$ ant
构建之后,您将拥有一个nutch / runtime / local目录,其中包含要执行的二进制文件。 现在让我们为Cassandra配置Nutch。
首先,我们需要通过将以下xml元素添加到nutch / conf / nutch-site.xml中来向Nutch添加代理:
<property><name>http.agent.name</name><value>My Nutch Spider</value>
</property>
接下来,我们需要告诉Nutch使用Gora Cassandra作为其持久性机制。 为此,我们在nutch / conf / nutch-site.xml中添加以下元素:
<property><name>storage.data.store.class</name><value>org.apache.gora.cassandra.store.CassandraStore</value><description>Default class for storing data</description>
</property>
接下来,我们需要向Gora讲述Cassandra。 编辑nutch / conf / gora.properties文件。 注释掉SQL条目,并取消注释以下行:
gora.cassandrastore.servers=localhost:9160
此外,我们需要为gora-cassandra添加依赖项。 编辑ivy / ivy.xml文件,然后取消注释以下行:
<dependency org="org.apache.gora" name="gora-cassandra" rev="0.2" conf="*->default" />
最后,我们要使用新的配置和附加的依赖项重新生成运行时。 使用以下ant命令执行此操作:
ant runtime
现在我们可以运行了!
创建一个名为“ urls”的目录,其中包含名为seed.txt的文件,其中包含以下行:
http://nutch.apache.org/
接下来,将conf / regex-urlfilter.txt中的正则表达式url更新为:
+^http://([a-z0-9]*\.)*nutch.apache.org/
现在,爬行!
bin/nutch crawl urls -dir crawl -depth 3 -topN 5
那将把网页收获到卡桑德拉!!
让我们再看一下数据模型…
您会注意到创建了一个新的键空间:webpage。 该键空间包含三个表:f,p和sc。
[cqlsh 2.3.0 | Cassandra 1.2.1 | CQL spec 3.0.0 | Thrift protocol 19.35.0]
Use HELP for help.
cqlsh> describe keyspaces;
system webpage druid system_auth system_traces
cqlsh> use webpage;
cqlsh:webpage> describe tables;
f p sc
这些表中的每一个都是纯键值存储。 要了解它们中的每个,请查看nutch / conf / gora-cassandra-mapping.xml文件。 我在下面添加了一个代码段:
<field name="baseUrl" family="f" qualifier="bas"/>
<field name="status" family="f" qualifier="st"/>
<field name="prevFetchTime" family="f" qualifier="pts"/>
<field name="fetchTime" family="f" qualifier="ts"/>
<field name="fetchInterval" family="f" qualifier="fi"/>
<field name="retriesSinceFetch" family="f" qualifier="rsf"/>
从该映射文件中,您可以看到它放在表中的内容,但是不幸的是,该架构并没有真正从CQL提示中进行探索。 (我认为这里还有改进的余地)如果有一个CQL友好模式会很好,但是通过gora可能很难实现。 las,这可能是抽象的代价。
因此,最简单的方法是使用螺母工具来检索数据。 您可以使用以下命令提取数据:
runtime/local/bin/nutch readdb -dump data -content
完成后,进入数据目录,您将看到用于提取数据的Hadoop作业的输出。 然后,我们可以将其用于分析。
我真的希望Nutch为C *使用更好的架构。 如果该数据可立即在C *中使用,那将是很棒的。 如果有人进行了增强,请告诉我!
翻译自: https://www.javacodegeeks.com/2013/10/crawling-the-web-with-cassandra-and-nutch.html