Neo4j图数据库的安装与基本增删改查

版权声明:本文为博主原创文章,欢迎转载,请注明出处 https://blog.csdn.net/mouday/article/details/86593658

安装启动

下载对应平台文件(社区版 neo4j-community-3.5.1)
https://neo4j.com/

neo4j-community-3.5.2-unix:
https://neo4j.com/artifact.php?name=neo4j-community-3.5.2-unix.tar.gz

解压 进入目录

$ ./neo4j start  # 启动
$ ./neo4j stop  # 停止

界面:http://localhost:7474/
首次使用:用户名、密码默认为 neo4j

两种方法清空数据库:
1、Cypher 语句:

match (n) detach delete n

2、删出文件

1、停掉服务
2、删除 graph.db 目录
3、重启服务

重要概念

1、节点
2、关系
3、属性

增删改查

1、添加4个节点

create(student:Student{id:1, name: "李雷"});
create(student:Student{id:2, name: "韩梅梅"});
create(teacher:Teacher{id:1, name: "仓老师"});
create(school:School{id: 1, title: "山东蓝翔"});

小写student 可以理解为面向对象的实体
大写Student 可以理解为面向对象的类
在这里插入图片描述

2、添加3个关系

match(s:Student{id:1}),(t:Teacher{id:1}) create (t)-[r:教授]->(s);
match(s:Student{id:2}),(t:Teacher{id:1}) create (t)-[r:教授]->(s);
match(s:School{id:1}),(t:Teacher{id:1}) create (t)-[r:就职于]->(s);

在这里插入图片描述
3、更新节点 老师改名字了

match(t:Teacher) 
where t.id=1 
set t.name="小泽老师"

4、删除节点 李雷被开除了

match(s:Student{id: 1}) 
detach delete s;

5、删除关系 小泽老师去别的班级上课了

match (t:Teacher)-[r:教授]->(s:Student) 
where t.id=1 and s.id=2 
delete r

6、索引操作

查看所有索引 
:schema

创建索引
create index on:Student(name)

删除索引
drop index on:Student(name)

创建唯一索引
create constraint on (s:Teacher) assert s.name is unique

删除唯一索引
drop constraint on (s:Teacher) assert s.name is unique

修改关系名称

MATCH (p:Person)-[r:投资]->(c:Company)
CREATE (p)-[r2:Invest{name:"投资"}]->(c)
// copy properties, if necessary
SET r2 = r
WITH r
DELETE r

https://stackoverflow.com/questions/22670369/neo4j-cypher-how-to-change-the-type-of-a-relationship

事务操作

from py2neo import Graph, Node, Relationship

g = Graph()
tx = g.begin()
a = Node("Person", name="Alice")
tx.create(a)
b = Node("Person", name="Bob")
ab = Relationship(a, "KNOWS", b)
tx.create(ab)
tx.commit()

批量导入

movies2.csv.

movieId:ID;title;year:int;:LABEL
tt0133093;'The Matrix';1999;Movie
tt0234215;'The Matrix Reloaded';2003;Movie|Sequel
tt0242653;'The Matrix Revolutions';2003;Movie|Sequel

actors2.csv.

personId:ID;name;:LABEL
keanu;'Keanu Reeves';Actor
laurence;'Laurence Fishburne';Actor
carrieanne;'Carrie-Anne Moss';Actor

roles2.csv.

:START_ID;role;:END_ID;:TYPE
keanu;'Neo';tt0133093;ACTED_IN
keanu;'Neo';tt0234215;ACTED_IN
keanu;'Neo';tt0242653;ACTED_IN
laurence;'Morpheus';tt0133093;ACTED_IN
laurence;'Morpheus';tt0234215;ACTED_IN
laurence;'Morpheus';tt0242653;ACTED_IN
carrieanne;'Trinity';tt0133093;ACTED_IN
carrieanne;'Trinity';tt0234215;ACTED_IN
carrieanne;'Trinity';tt0242653;ACTED_IN
The call to neo4j-admin import would look like this:

导入指令


$ bin/neo4j-admin import 
--nodes import/movies2.csv 
--nodes import/actors2.csv
--relationships import/roles2.csv 
--delimiter ";"
--array-delimiter "|" 
--quote "'"

配置外网访问

conf\neo4j.conf

#dbms.connector.http.listen_address=:7474
dbms.connector.http.listen_address=0.0.0.0:7474

#dbms.connector.bolt.listen_address=:7687
dbms.connector.bolt.listen_address=0.0.0.0:7687

配置环境变量:

#neo4j
export NEO4J_HOME=<neo4j_path>
export PATH=$PATH:$NEO4J_HOME/bin

参考:

  1. Mac 搭建Neo4j环境
  2. neo4j 初步认识和使用
  3. Neo4j的简单搭建与使用
  4. Neo4j 简单入门
  5. neo4j----创建索引
  6. https://neo4j.com/docs/operations-manual/current/tutorial/import-tool/
  7. 配置Neo4j服务器允许远程访问

没有更多推荐了,返回首页