Thursday, October 17, 2013

Crawling the Web with Cassandra and Nutch


So, you want to harvest a massive amount of data from the internet?  What better storage mechanism than Cassandra?  This is easy to do with Nutch.

Often people use Hbase behind Nutch.  This works, but it may not be an ideal solution if you are (or want to be) a Cassandra shop.   Fortunately, Nutch 2+ uses the Gora abstraction layer to access its data storage mechanism.  Gora supports Cassandra.  Thus, with a few tweaks to the configuration, you can use Nutch to harvest content directly into Cassandra.

We'll start with Nutch 2.1...  I like to go directly from source:

$ git clone https://github.com/apache/nutch.git -b 2.1
...
$ ant


After the build, you will have a nutch/runtime/local directory, which contains the binaries for execution.  Now let's configure Nutch for Cassandra.

First we need to add an agent to Nutch by adding the following xml element to nutch/conf/nutch-site.xml:
<property>
 <name>http.agent.name</name>
 <value>My Nutch Spider</value>
</property>

Next we need to tell Nutch to use Gora Cassandra as its persistence mechanism. For that, we add the following element to 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>

Next, we need to tell Gora about Cassandra.  Edit the nutch/conf/gora.properties file.  Comment out the SQL entries, and uncomment the following line:
gora.cassandrastore.servers=localhost:9160

Additionally, we need to add a dependency for gora-cassandra.  Edit the ivy/ivy.xml file and uncomment the following line:
<dependency org="org.apache.gora" name="gora-cassandra" rev="0.2" conf="*->default" />

Finally, we want to re-generate the runtime with the new configuration and the additional dependency.  Do this with the following ant command:
ant runtime

Now we are ready to run!

Create a directory called "urls", with a file named seed.txt that contains the following line:
http://nutch.apache.org/

Next, update the regular expression url in conf/regex-urlfilter.txt to:
+^http://([a-z0-9]*\.)*nutch.apache.org/

Now, crawl!
bin/nutch crawl urls -dir crawl -depth 3 -topN 5

That will harvest webpages to Cassandra!!

Let's go look at the data model for a second...
You will notice that a new keyspace was created: webpage.  That keyspace contains three tables: f, p, and 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


Each of these tables is a pure key-value store.  To understand what is in each of them, take a look at the nutch/conf/gora-cassandra-mapping.xml file.  I've included a snippet below:
        <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"/>

From this mapping file, you can see what it puts in the table, but unfortunately the schema isn't really conducive to exploration from the CQL prompt.  (I think there is room for improvement here)  It would be nice if there was a CQL friendly schema in place, but that may be difficult to achieve through gora.  Alas, that is probably the price of abstraction.

So, the easiest thing is to use the nutch tooling to retrieve the data.  You can extract data with the following command:
runtime/local/bin/nutch readdb -dump data -content

When that completes, go into the data directory and you will see the output of the Hadoop job that was used to extract the data.  We can then use this for analysis.

I really wish Nutch used a better schema for C*.   It would be fantastic if that data was immediately usable from within C*.  If someone makes that enhancement, please let me know!

Thursday, September 5, 2013

The Economics of Open Source : Seek Free Code. Find Innovation.

In the past, many technologists had to evangelize open source within the enterprise. We had to justify its use, reassure executives about security, ability to support, etc.  Recently,  I believe those tables have turned.  More and more, businesses are asking the question, "Isn't there an open-source solution for this?".

Not only has open-source been accepted, but its become the preferred solution.  We are certainly in this position.  Thus far, our technology stack is built entirely open-source components.

I see the following motivators driving this direction:

Control Your Own Destiny

With open-source, you are in control.  You are free to fork the code, extend it, destroy it, whatever your pleasure.  If you need a specific feature, you can develop it yourselves.  Your roadmaps and timelines are your own.

If there is a problem in production, you can diagnose and resolve your own issues.  In many cases, enterprises are larger than the emerging technology vendors.  The enterprises can bring more resources to bear than the technology vendor.  What they may lack in expertise, they can make up for in resources.  (and bringing in consultants if the need arises)

Innovation Enabler

Honestly, this one is always under-valued.  Open-source technologies enable innovation, by attracting talent, and seeding ideas.  Open-source technologies provide a treasure trove of ideas for tinkerers/inventors.  Developers can look under the covers, mine ideas, and come up with new ways to combine and extend the technology.  

It is often impossible to predict innovation, but enabling the right talent with the right technology certainly improves your chances.  Talent often differentiates one company from another.  It is the crazy idea from the morning shower and the ability to execute on it, that keeps technology companies innovating.  IMHO, open-source technology stacks attract the best talent.  Open-source communities are meritocracies that allow the best of the best to succeed, which results in commensurate accolades.  This attracts the best of the best, and that's who you want working/innovating for you.

Support Costs determined by Free Market

Open-source is the anti-monopoly.  With proprietary software vendors, you need to pay their licensing and support costs.  If they are entrenched, the vendor can theoretically charge a large percentage of whatever the replacement cost would be, and the enterprise has no other option but to comply.  That becomes a tough pill to swallow.

With open-source, support contracts are new the license fee.  But unlike license fees,  an enterprise is free to shop around, selecting the best value for the dollar.  Companies providing support must compete on customer service and price point.

The Decision

In the end, an enterprise needs to make a decision.  Inevitably, there will be scenarios where time-to-market, project maturity, and/or core-competencies influence the decision to build, buy or leverage open-source, and those may prevent an enterprise from selecting an open-source solution. But let's take a quick look at the economics of such a decision, considering the motivations outlined above.

Let's assume there is an Open-Source project, OpenZee, that is deficient in feature and function to a proprietary product ClosedOrb.  Let's assume there is an annual license fee of $25K for ClosedOrb.  Over five years, that would cost an enterprise $125K.  Because time-to-market is a concern, for the first year it may make sense to go with ClosedOrb and shell out the $25K, but strategically, at the same time, it might make sense to dedicate a resource to OpenZee to close the feature function gap and replace ClosedOrb in the stack.   And although it might not be feasible to close the gap with a single enterprise's cost-savings (e.g. $100K), if we recognize that other enterprises are in the same situation, it is likely that the a collective effort *can* close the gap.  If there are a couple dozen companies all making the same trade-off, that would result in $2.4M annually of resources to apply to the project.  That rivals many small-company R&D budgets.

In this scenario, OpenZee did not yet have the minimal set of functionality to produce a viable product.   The project needed a set of early adopting companies that were willing to invest in the strategic vision.  But once the project has the minimal amount of functionality, (The 80% in the 80/20 rule) the dynamics change quite a bit.   While the company behind ClosedOrb continues to build out the 20%, more and more companies begin using OpenZee because the criticality of the functional differences begin to diminish.  OpenZee eventually outpaces ClosedOrb and takes over the world. (okay -- that's hyperbole, but you get the point)

The Conclusion

Now, I want to be clear.  Although the scenario above focused on the financial motivation to invest in/contribute to an open-source project, the motivations outlined previously were not based on any financial incentive.  I don't believe enterprises are necessarily looking for "free software" as in "free beer".  The motivations above in fact are derived solely from the code being "free" as in "free speech".  (More on this topic here)

In many cases, enterprises will happily pay for support for software because that is a service that they would otherwise have to fill on their own.  In fact, at larger enterprises, being able to pay for commercial support is actually a requirement for technology adoption!  But as the world shifts from a "why open source" to a "why not open source", more companies are going to take a strategic view and demand open-source solutions to "free" their development/innovation.  They are also going to be more willing to invest in those projects that need it.

This bodes well for projects like Druid, which may just take over the world of Big Data Analytics. (yes, more hyperbole ... maybe... just to emphasize the point =)

I think companies like Datastax have played their cards right.   They understand that with community comes momentum and with momentum comes revenue.  Sometimes that revenue comes directly from licensing proprietary extensions, sometimes indirectly via support, services and education.  The color of the money is the same.

My advice:
Seek free code.  Find innovation.