Saturday, December 2, 2017

graph database basic

1- neo4j is a graph database software to download use link below
https://neo4j.com/download/

2- after download run the binary file,
then it will start downloading more file, java, and install automatically


3-then you can create a new database and start it, we can use its browser to operate the database, to access http://localhost:7474/browser/, to manage it from firefox.

4- basic syntax

- create a record
CREATE (me:Person {name:"me"})
RETURN me



- create a relationship
MATCH  (me:Person {name:"me"})
CREATE (me)-[learn:LEARN]->(school:School {name:"XUnivercity" })
RETURN me,learn,school


MATCH  (me:Person {name:"me"})
CREATE (me)-[friend:FRIEND]->(person:Person {name:"Annaa"})



-create with loop
MATCH  (me:Person {name:"me"})
FOREACH (name in ["Phone","Computer","Watch","Alarmclock"] |
  CREATE (me)-[:Have]->(thing:Thing {name:name}))



-secondary relationship
MATCH (school:School {name:"XUnivercity" })
MATCH (annaa:Person {name:"Annaa"})
CREATE (annaa)-[:FRIEND]->(:Person:Attend {name:"Kyoto"})-[:GOTO]->(school)



- select
MATCH (me {name:"me"})-[:Have]->(belonging)
RETURN me, belonging


MATCH (me {name:"me"})
MATCH (student)-[:GOTO]->(school:School {name:"XUnivercity" })
MATCH path = shortestPath( (me)-[:FRIEND*..5]-(student) )
RETURN school,student,path 


-delete a relationship
MATCH (n { name: 'me' })-[r:FRIEND ]->(person:Person {name:"Annoya"})
DELETE r


-delete all relationship
MATCH (n { name: 'me' })-[r:FRIEND ]->(})
DELETE r


-delete a record and its relationship
MATCH  (n:Animal {name:"Dog"})
DETACH DELETE n



-query the first 100 recored
 MATCH (n) RETURN n limit 100 



5- change database edit ~/.config/Neo4j Desktop/Application/neo4jDatabases/database-fd191822-eb04-4a1d-b628-85f97832f1fe/installation-3.3.1/conf/neo4j.conf
stop database

dbms.active_database=newdabase.db


start database


6- backup and restor database
bin/neo4j-admin dump --database=graph.db --to=/home/user1/neo4jdb/dump1.dump

bin/neo4j-admin load --from=/home/user1/neo4jdb/dump1.dump --database=newdb.db --force



- list database

$ ls data/databases/


No comments:

Post a Comment