文章目录
- neo4j 图数据库使用教程
- 1) 下载
- 2)安装
- 3) 创建数据
- 4)查询数据
- 5) 更新数据
- 6) 建立索引
neo4j 图数据库使用教程
1) 下载
neo4j下载地址
http://dist.neo4j.org/neo4j-community-3.5.21-unix.tar.gz
2)安装
首先:
cd /conf
vi neo4j.conf
修改下面这俩个地方:
dbms.connectors.default_listen_address=0.0.0.0
dbms.connectors.default_advertised_address=bigdata04
启动:
bin/neo4j start
查看:
http://bigdata04:7474
3) 创建数据
- crate指令
create:每次都创建新的点或边
先创建一个点
create (p1:Person {name:"zs"})
再创建一个点
create (p2:Person {name:"ls"})
create (p1:Person {name:"zs"}) -[:like]-> (p2:Person {name:"ls"})
- merge 指令
merge:这个命令在创建节点之前都会先查询一下,如果存在则不创建
merge (p3:Person {name:"jack"})
merge (p4:Person {name:"tom"})
merge (p3) -[:like]-> (p4)
- match指令
match:可以查询之前已有的节点(点)或者关系(边)
match(a:Person {name:"tom"}),(b:Person {name:"jack"})
merge (a) -[:like]-> (b)
4)查询数据
match + return :查看满足条件的数据并返回
match指令不能单独使用
match(p:Person {name:“tom”}) return p
- 初始化数据:
merge(a:User {name:"A"})
merge(b:User {name:"B"})
merge(c:User {name:"C"})
merge(x:User {name:"X"})
merge(y:User {name:"Y"})
merge(z:User {name:"Z"})
merge(a) -[:follow]-> (b)
merge(c) -[:follow]-> (b)
merge(a) -[:follow]-> (x)
merge(a) -[:follow]-> (y)
merge(c) -[:follow]-> (y)
merge(c) -[:follow]-> (z)
-
某个主播的粉丝信息
match (:User {name:“B”}) <-[:follow]- (n:User) return n
还有一种写法是这样的
match (n:User) -[:follow]-> (:User {name:“B”}) return n只返回name的值
match (n:User) -[:follow]-> (:User {name:“B”}) return n.name
二度关系:
我 --> 主播B --> 粉丝
三度关系
我 --> 主播B --> 粉丝 --> 主播N
match (a:User {name:“B”}) <-[:follow]- (b:User) -[:follow]-> (c:User) return a.name as aname,b.name as bname,c.name as cname
对被关注最多的主播进行个筛选:
其实match后面也支持count()、order by、limit等命令
match (a:User {name:“B”}) <-[:follow]- (b:User) -[:follow]-> (c:User) return a.name as aname,c.name as cname ,count(*) as sum order by sum desc limit 3
注意:在这里可以使用count(*) 或者count(cname),效果是一样的
注意:where需要放在return前面
match (a:User {name:“B”}) <-[:follow]- (b:User) -[:follow]-> (c:User) where c.name <> “X” return a.name as aname,c.name as cname ,count(*) as sum order by sum desc limit 3
5) 更新数据
如何修改节点中的属性
match (a:User {name:“X”}) set a.age = 18
如何删除关系
match (:User {name:“A”}) -[r:follow]-> (:User {name:“X”}) delete r
6) 建立索引
1)普通索引
2) 唯一约束(类似主键)
如果某个字段的值是唯一的,可以建立唯一约束,这个查询性能比索引更快
将数据:follower_demo.log 放入import目录下
follower_demo.log数据:
fuid uid
1001 1000
1001 1004
1001 1005
1001 2001
1002 1000
1002 1004
1002 2001
1003 1000
1003 1004
1006 1000
1006 1005
2002 1004
2002 1005
2002 2004
2003 1000
2003 1005
2003 2004
批量导入数据:
针对关键字段建立索引
create constraint on (user:User) assert user.uid is unique;
批量导入语句
:auto using periodic commit 1000
load csv with headers from 'file:///follower_demo.log' as line fieldterminator '\t'
merge (viewer:User {uid: toString(line.fuid)})
merge (anchor:User {uid: toString(line.uid)})
merge (viewer) -[:follow]-> (anchor);
using periodic commit 1000
load csv with headers from 'file:///follower_demo.log' as line fieldterminator '\t'
merge (viewer:Test {name: toString(line.fuid)})
merge (anchor:Test {name: toString(line.uid)})
merge (viewer) -[:follow]-> (anchor);
lower_demo.log' as line fieldterminator '\t'
merge (viewer:Test {name: toString(line.fuid)})
merge (anchor:Test {name: toString(line.uid)})
merge (viewer) -[:follow]-> (anchor);