Neo4J is a graph database, a database aimed at storing and querying graphs rather than tables. Many things in modern life are 'graphy': social network data is a well-known example, where the emphasis lies more on who is connected with who and what than on the data itself, like your name and address.
I recently migrated my spreadsheet analysis tool from SQL Server to Neo4J. It was
1) healthy for my brain to work with a new type of database,
2) fun, and
3) queries turned out to be a lot more intuitive and efficient.
Now Take Simple Example of Neo4j
Starting and stopping the Neo service
service neo4j-service statusservice neo4j-service start
service neo4j-service stop
Neo UI
http://localhost:7474/webadmin/Starting the shell
./bin/neo4j-shellcreting a node
CREATE (ee { name: "Emil", from: "Sweden" }) RETURN ee.name;CREATE clause to create a node
() to indicate a node
{} to add properties to a node
ee a variable to the new node
Retrieve the node
START ee=node(*) WHERE ee.name! = "Emil" RETURN ee;START clause to being a query
ee=node(*) to search through all the nodes
Create many nodes and relations at once
CREATE (ee { name: "Emil", from: "Sweden", klout: 99 }),(js { name: "Johan", from: "Sweden", learn: "surfing" }),
(ir { name: "Ian", from: "England", title: "author" }),
(rvb { name: "Rik", from: "Belgium", pet: "Orval" }),
(ally { name: "Allison", from: "California", hobby: "surfing" }),
(ee)-[:KNOWS {since: 2001}]->(js),(ee)-[:KNOWS {rating: 5}]->(ir),
(js)-[:KNOWS]->(ir),(js)-[:KNOWS]->(rvb),
(ir)-[:KNOWS]->(js),(ir)-[:KNOWS]->(ally),
(rvb)-[:KNOWS]->(ally);
To find Emils friend
START ee=node(*) MATCH (ee)-[:KNOWS]->(friends)WHERE ee.name! = "Emil" RETURN friends;
MATCH clause to describe the pattern from Known nodes to Found nodes
Pattern matching can be used to make recommendations. Johan is learning to surf, so he may want to find a new friend who already does.
START js=node(*)MATCH (js)-[:KNOWS]->()-[:KNOWS]->(surfer)
WHERE js.name! = "Johan" AND surfer.hobby! = "surfing"
RETURN DISTINCT surfer;
() is for ignoring the nodes
No comments:
Post a Comment