Andrew Oliver, Strategic Developer, enlists a graph database to create the killer app – Granny’s Addressbook

Still using an RDBMS for friend-of-a-friend queries? Big mistake. Enlist a graph database using Neo4j instead

My kids’ paternal grandmother — I call her “Mom” — has a problem that is not solved by her killer app, Granny’s Addressbook. Frequently, she asks me what gift to get the kids for the all-too-frequent stream of birthdays, Hanukkah, Christmas, Kwanzaa, Arbor Day, and whatever holidays. Frequently, I inform her that the children are already spoiled and I’ve gotten them anything they need and if not, there is a good reason that I don’t want them to have it. Those reasons vary from “it’s flammable” to “it’s a weapon if you hold it right” to “it makes annoying noises that I don’t want to hear” to the more common “I don’t feel like figuring out how to assemble it and by the time it gets assembled the pieces will have been lost.”

In other words, Grandma has a data problem. If she were to map out what all of the kids’ friends or other children close to their age got for their birthday, she could easily figure out what to buy them without asking me.

As you recall, Grannny’s Addressbook is a sample app I’ve used to demonstrate various technologies. It is essentially a one-table CRUD app that stores addresses in a database. It has no search or security and it doesn’t really need to scale. The GUI looks like this:

In this case, the gift-giving problem has landed Granny in a complex data dilemma. She needs to know the kids’ ages and their relationships to other people. She also needs to know what each kid got for his or her last birthday. We will represent this with three rather naive fields: First is “Friends,” which is a multiselect list box. The second is “Birthday.” The third is “Last Present,” representing gifts from their most recent birthday. So long as those fields are populated, we’ll populate another field called “Suggested Presents,” which is a list of gift ideas. The new GUI looks like this:

We essentially want to recommend a gift for a person based on what each of friend and friends of friends got for their last birthday. We also want to order the gifts by the distance of the relationship.

….

Doing this in a relational database structure is pretty painful. In the JPA/SQL version of granny, I need two tables and multiple trips to the database just to walk the graph of relationships. I also have to do pretty much all the ordering and logic myself.

In other words, this is a classic graphical database problem. Relationships matter as much as, if not more than, the data itself. Neo4j is the most popular graph database on the market these days. While graph databases are part of the NoSQL movement, they really solve different problems than, say, Couchbase or MongoDB. We aren’t necessarily concerned with handling massive scale or doing analytics across terabytes of big data a la Hadoop’s HBase. In fact, most graph databases are transactional, and the reason they are NoSQL is that SQL is simply inadequate to express the problems, as you can see in the amount of code it took in the findSuggestions method.

For the Granny4j version using Neo4j the main query comes down to this:

// select friends and friends of friends, order by depth of the relationship

String findFriendsQuery = "start n=node(*), person=node({userNode}) MATCH p = (person)-[:FRIEND*1..2]-(friend) return distinct p order by length(p)";

As you can see there is a lot less code — and it does the job. It’s also more efficient. Check out all the code for Granny4j.

Read the full article.