Last active
August 29, 2015 14:02
-
-
Save cbandy/fc853d1f0de13a4ee3a1 to your computer and use it in GitHub Desktop.
Neo4j in PostgreSQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- CREATE (n:Actor { name:"Tom Hanks" }); | |
| WITH | |
| n AS ( | |
| INSERT INTO node (properties) VALUES ('name=>Tom Hanks') RETURNING id | |
| ) | |
| INSERT INTO node_label VALUES (n.id, 'Actor'); | |
| -- MATCH (actor:Actor { name: "Tom Hanks" }) | |
| -- RETURN actor; | |
| WITH | |
| actor AS ( | |
| SELECT node.* FROM node | |
| JOIN node_label ON (node_label.node_id = node.id) | |
| WHERE node.properties @> 'name=>Tom Hanks' | |
| AND node_label.label IN ('Actor') | |
| ) | |
| SELECT * FROM actor; | |
| -- MATCH (actor:Actor) | |
| -- WHERE actor.name = "Tom Hanks" | |
| -- CREATE (movie:Movie { title:'Sleepless IN Seattle' }) | |
| -- CREATE (actor)-[:ACTED_IN]->(movie); | |
| WITH | |
| actor AS ( | |
| SELECT node.* FROM node | |
| JOIN node_label ON (node_label.node_id = node.id) | |
| WHERE node.properties @> 'name=>Tom Hanks' | |
| AND node_label.label IN ('Actor') | |
| ), | |
| movie AS ( | |
| INSERT INTO node (properties) VALUES ('title=>Sleepless IN Seattle') RETURNING id | |
| ), | |
| movie_label AS ( | |
| INSERT INTO node_label VALUES (movie.id, 'Movie') | |
| ) | |
| INSERT INTO relationship (start_id, end_id, "type", properties) | |
| VALUES (actor.id, movie.id, 'ACTED_IN', NULL); | |
| -- MATCH (actor:Actor { name: "Tom Hanks" }) | |
| -- SET actor.DoB = 1944 | |
| -- RETURN actor.name, actor.DoB; | |
| WITH | |
| actor AS ( | |
| SELECT node.* FROM node | |
| JOIN node_label ON (node_label.node_id = node.id) | |
| WHERE node.properties @> 'name=>Tom Hanks' | |
| AND nodel_label.label IN ('Actor') | |
| ) | |
| UPDATE actor SET properties = properties || 'DoB=>1944' RETURNING properties -> ARRAY['name','DoB']; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CREATE TABLE node ( | |
| id serial PRIMARY KEY, | |
| properties hstore | |
| ); | |
| CREATE TABLE node_label ( | |
| node_id REFERENCES node, | |
| label varchar | |
| ); | |
| CREATE TABLE relationship ( | |
| id serial PRIMARY KEY, | |
| start_id integer REFERENCES node, | |
| end_id integer REFERENCES node, | |
| "type" varchar, | |
| properties hstore | |
| ); | |
| CREATE INDEX ON relationship ("type", start_id); | |
| CREATE INDEX ON relationship ("type", end_id); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://maxdemarzi.com/2012/08/13/neo4j-internals/
http://neo4j.com/docs/2.1.1/graphdb-neo4j/