Skip to content

Instantly share code, notes, and snippets.

@jexp
Created August 9, 2013 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jexp/6193139 to your computer and use it in GitHub Desktop.
Save jexp/6193139 to your computer and use it in GitHub Desktop.
Neo4j 2.0 Webinar Code& Query Samples
// Transactional Http Endpoint DEMO
// execute single statement
POST /db/data/transaction/commit {"statements":[{"statement":"MATCH (u:User) RETURN u"}]}
// create transaction with first statement
POST /db/data/transaction {"statements":[{"statement":"CREATE (u:User {login:{name}}) RETURN u","parameters":{"name":"Peter"}}]}
// check data is visible within transaction
POST /db/data/transaction/X {"statements":[{"statement":"MATCH (u:User) RETURN u"}]}
// check data is NOT visible outside of transaction
POST /db/data/transaction/commit {"statements":[{"statement":"MATCH (u:User) RETURN u"}]}
// rollback transaction
DELETE /db/data/transaction/X
// start a ne transaction with a create statement
POST /db/data/transaction {"statements":[{"statement":"CREATE (u:User {login:{name}}) RETURN u","parameters":{"name":"Andreas"}}]}
// check visibility again
POST /db/data/transaction/X {"statements":[{"statement":"MATCH (u:User) RETURN u"}]}
POST /db/data/transaction/commit {"statements":[{"statement":"MATCH (u:User) RETURN u"}]}
// commit transaction
POST /db/data/transaction/X/commit
// check public visibility
POST /db/data/transaction/commit {"statements":[{"statement":"MATCH (u:User) RETURN u"}]}
// show Location header for newly created transaction
curl -i -H content-type:application/json -H accept:application/json \
-d'{"statements":[{"statement":"MATCH (n:User) RETURN n"}]}' \
http://localhost:7474/db/data/transaction
# download sample dataset from http://www.neo4j.org/develop/example_data
curl -o cineasts.zip http://example-data.neo4j.org/files/cineasts_39_movies_446_actors.zip
# open data, delete old graph.db directory
rm -rf data/graph.db
unzip -d data/graph.db cineasts.zip
# check java 7
java -version
# enable store upgrade
edit conf/neo4j.properties
# Enable this to be able to upgrade a store from an older version
allow_store_upgrade=true
# start the server
bin/neo4j start
# open browser
open http://localhost:7474
# start the shell for terminal console
bin/neo4j-shell
// Upgrade cineasts dataset to labels
// what kind os of node-"types" do we have
START n=node:Movie("id:*") RETURN count(*);
START n=node:Person("id:*") RETURN count(*);
START n=node:Actor("id:*") RETURN count(*);
START n=node:Director("id:*") RETURN count(*);
START n=node:User("login:*") RETURN count(*);
// let's upgrade movies
START n=node:Movie("id:*") SET n:Movie RETURN n.title, labels(n);
// some label queries
MATCH (m:Movie) RETURN count(*);
MATCH (m) WHERE m:Movie RETURN count(*);
MATCH (m) WHERE "Movie" in labels(m) RETURN count(*);
// movie by title
MATCH (m:Movie) WHERE m.title="The Matrix" RETURN m.title;
// how is the lookup done?
profile MATCH (m:Movie) WHERE m.title="The Matrix" RETURN m.title;
// create an index
CREATE INDEX ON :Movie(title);
// is it online ?
schema
// check that the lookup is now done via the index
profile MATCH (m:Movie) WHERE m.title="The Matrix" RETURN m.title;
// some more Label queries
MATCH (m:Movie)<-[:ACTS_IN]-(p) WHERE m.title="The Matrix" RETURN p.name;
// upgrade people
START n=node:Person("id:*") SET n:Person;
// a different way via relationships
MATCH (m:Movie)<-[:ACTS_IN]-(p) SET p:Person;
START n=node:Director("id:*") SET n:Director;
MATCH (m:Movie)<-[:DIRECTED]-(p) SET p:Director;
// Upgrade users
START u=node:User("login:*") RETURN u;
START u=node:User("login:*") SET u:User;
// Show merge
MERGE (u:User {name:"Andreas"}) return u;
// nothing happens the second time
MERGE (u:User {login:"Andreas"}) ON CREATE u SET u.created=timestamp() return u;
MERGE (u:User {login:"Andreas"}) ON MATCH u SET u.accessed=coalesce(u.accessed,0)+1 return u;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment