Apr 11, 2014

Getting Started With Graph Database Neo4j

What is Neo4j? Its a graph database.

What is a graph database?

A graph database is a database that uses graph structures with nodes, edges, and properties to represent and store data. A graph database is any storage system that provides index-free adjacency.This means that every element contains a direct pointer to its adjacent elements and no index lookups are necessary. General graph databases that can store any graph are distinct from specialized graph databases such as triplestores and network databases.WikiPedia

Why do I use it?

Its not a compulsion to use a graph db everytime, it depends on your projects schema and its relations. MySql gets weak in deep relations and graph db get stronger over here. Let me give you a simple example: Consider a social networking schema. I want a user from db who is a friend of my friends friend. This can be done using joins in mysql but it will take very long queries. But now if you use a graph db over here, it would be quick with a very small query. The reason to use a graph database is that the data stored by the system and the operations the system does with the data are exactly the weak spot of relational databases and are exactly the strong spot of graph databases. The system needs to store collections of objects that lack a fixed schema and are linked together by relationships. In graph db everything is represented by a node. If there are 10 users, then 10 nodes can be created. Users information can be stored in nodes property or in new nodes. Each node can be connected to other nodes and this linking is called as Relations. Now using this model its very easy to get a user who is a friend of my friends friend. Many popular websites uses graph database, facebook, linkedin, Cisco, HP, Accenture, Deutsche Telekom and many more.

Neo4j is a open source project developed by Neo Technology using JAVA. The developers describe Neo4j as "embedded, disk-based, fully transactional Java persistence engine that stores data structured in graphs rather than in tables". Neo4j is the most popular graph database.

Neo4j queries are built on Cypher language. Its really simple to learn.

Installation of neo4j is really simple.

So lets create a simple node in neo4j using cypher. After installation, just goto your browser at this URL http://localhost:7474/browser/ This will be the UI for executing neo4j queries.

CREATE (u:USER {uid:1})
Above code will create a simple node in Neo4j. Now lets see the node.
MATCH (u:USER {uid:1}) return u;
  Lets play around with nodes property. We will create few nodes
CREATE (u:USER {uid:1 , firstName:"Niraj", lastName:"Chauhan", gender:"Male"})
CREATE (u:USER {uid:2 , firstName:"Rishi", lastName:"Kulshreshtha", gender:"Male"})
CREATE (u:USER {uid:3 , firstName:"Leroy", lastName:"Bhavighar", gender:"Male"})
CREATE (u:USER {uid:4 , firstName:"Torrie", lastName:"Zacharia", gender:"Female"})
Lets see all nodes together
MATCH (u:USER) return u;

Now lets make some relation with nodes. Each relation needs a name. So I ll create a relation of friends.

MATCH (u1:USER{uid:1}),(u2:USER{uid:2}) CREATE (u1)-[:ARE_FRIENDS]->(u2);
MATCH (u2:USER{uid:2}),(u3:USER{uid:3}) CREATE (u2)-[:ARE_FRIENDS]->(u3);
MATCH (u3:USER{uid:3}),(u2:USER{uid:4}) CREATE (u3)-[:ARE_FRIENDS]->(u4);
So now I have made a simple relation of friends, Niraj has a friend Rishi has a friend Leroy has a friend Torrie. Lets get a user for Niraj who is female in third degree of friendship.
MATCH (niraj{firstName:"Niraj"})-[:ARE_FRIENDS*3..3]-(friend_of_friend)where friend_of_friend.gender = "Female" return friend_of_friend;

So the above query is simple, I selected as "Niraj" as my start and am looking for friends friends friend who is female. This was just a basic of Neo4j. Neo4j is really strong. I have used it in many projects, but only where its essential or in simple words schema with deep relations. You can learn more about queries of neo4j here: http://docs.neo4j.org/chunked/stable/cypher-query-lang.html

Apr 10, 2014

Output user profile picture programmatically in Drupal

Drupal is a wonderful platform to create complex websites in simpler way but sometimes, a simple task is too difficult to achieve in Drupal.
There are many pros of using Drupal in terms of other CMS available in market, such as its:

  • Extremely Flexible
  • Developer Friendly
  • Strong SEO
  • Capabilities Enterprise
  • Friendly Stability 

For instance lets take the example of user profile, if someone wants to show the user profile picture in Drupal then its a complicated task for him/her. Let me simply this for you in the below code. I've created the code in such a way if the user profile picture is not set, then it will show you the default profile pic.

Code:

global $user;
if ($user->uid) {
$user = user_load($user->uid);
print theme(
'image_style',
array(
'style_name' => 'thumbnail',
'path' => !empty($user->picture->uri)?$user->picture->uri:variable_get('user_picture_default'),
'attributes' => array(
'class' => 'avatar'
)
)
);
}
Cheers!
Photo Credit: Instagram