图形数据库——neo4j安装与使用

neo4j安装与使用

一、neo4j安装

  • 1、安装jdk1.8
  • 2、下载程序包
    链接:https://pan.baidu.com/s/1eHWtEk222K3SyK8B-wWr1Q
    提取码:mqfz
# 解压
[root@localhost neo4j]# tar -zxvf neo4j-community-3.3.0-unix.tar.gz 
# 修改配置文件
[root@localhost neo4j-community-3.3.0]# vim ./conf/neo4j.conf 
# 放开访问权限
dbms.connectors.default_listen_address=0.0.0.0
# 启动
./bin/neo4j start
# 停止
./bin/neo4j stop

在这里插入图片描述

  • 3、访问WEB管理页面:http://192.168.47.102:7474/browser/

二、neo4j的使用

在这里插入图片描述

  • 1、创建节点
# 其中:pig,指定该节点的标签,{}中就是节点的属性
create (:pig{name: "猪爷爷", age: 100})

在这里插入图片描述

  • 2、创建关系
# 猪爷爷的妻子指向猪奶奶
match (a:pig{name: "猪爷爷"}) match(b:pig{name: "猪奶奶"}) create (a)-[r:妻子]->(b) return r

在这里插入图片描述

  • 3、删除某类节点的某个关系
match (n:pig)-[r:丈夫]-() delete r
  • 4、创建多个复杂关系如下
match (c:pig{name: "猪爷爷"}) match (d:pig{name: "猪奶奶"}) create (a:pig{name: "猪爸爸", age: 50})-[abr:妻子]->(b:pig{name: "猪妈妈", age: 45}) create (b)-[bar:丈夫]->(a)  create (a)-[acr:父亲]->(c) create (c)-[car:儿子]->(a)  create (a)-[adr:母亲]->(d) create (d)-[dar:儿子]->(a) create (b)-[bcr:父亲]->(c) create (c)-[cbr:女儿]->(b) create (b)-[bdr:母亲]->(d) create (d)-[dbr:女儿]->(b) return a,b,c,d

在这里插入图片描述

三、Neo4j常用的查询

  • 一、添加操作
      1. 添加节点:
        create (x:学生{studentId:‘1001’,age:20}
      1. 添加关系:
        对现有的节点添加关系
        match (x:学生{studentId:1001}),(y:教师{tid:‘09’}) create (x)-[jx:课程{name:‘高数’}]->(y)
        match (x:学生),(y:教师) where x.studentId=‘1001’ and y.tid=‘09’ create (x)-[jx:课程{name:‘高数’}]->(y)
        添加节点并添加关系
        create (x:学生{studentId:1001})-[jx:课程{name:‘高数’}]->(y:教师{tid:‘09’})
      1. 根据节点的ID创建关系
        match (x:学生),(y:教师) where id(x)=254885 and id(y)=554896 create (x)-[jx:课程{name:‘高数’}]->(y)
  • 二、删除操作
      1. 删除节点:
        match (x:学生{studentId:1001}) delete x
      1. 删除关系:
        match (x:学生{studentId:1001})-[jx:课程{name:‘高数’}]->(y:教师{tid:‘09’}) delete jx
      1. 删除关系的同时,删除数据:
        match (x:学生{studentId:1001})-[jx:课程{name:‘高数’}]->(y:教师{tid:‘09’}) delete x,jx,y
  • 三、修改节点
      1. 给节点添加一个新的属性,两种方式:
        match(x:学生{studentId:‘1001’}) set x.age=21 return x
        match(x:学生{studentId:‘1001’}) set x+={age:21} return x
      1. 给节点添加属性并删除现有属性
        match(x:学生{studentId:‘1001’}) set x={age:21,name:‘abc’} //注意这里,会将studentId属性删除
      1. 添加新标签:
        match(x:学生{studentId:‘1001’}) set x:男生 return x //添加一个标签
        match(x:学生{studentId:‘1001’}) set x:男生:团员 return x //添加多个标签
  • 四、查询操作
      1. 根据节点属性查找对应节点:
        match(x:Student{studentId:‘1001’}) return x
        或者
        match(x:Student) where x.studentId=‘1001’ return x
      1. 根据关系查找节点
        match (x)-[r:教学内容]-(y) where r.课程=‘语文’ return x,r,y
      1. 查询单独的节点,即:与其他任何节点没有任何关系
        match(x) where not (x)-[]-() return x
      1. 查询N层关系的节点:
        match q=(x)-[*5…8]-() return q limit 200 这个为查询5到8层关系的
        match q=(dh)-[r]-(jq)-[rr]-()-[]-()-[]-()-[]-()-[]-()-[]-() return q limit 400
      1. 查询节点关系数个数:
        match(dh:学生)-[r]-(jq:老师) with dh, count® as dhs where dhs > 2 return dh
      1. 查询节点个数:
        match(x) return count(x)
      1. 查询所有的关系类型:
        CALL db.relationshipTypes()
      1. 查询所有的节点标签:
        CALL db.labels()
      1. 查询节点关系种类:
        CALL db.schema()
      1. 最短路径查询
        MATCH (x:电话{hm:“02711111111”}),(y:电话{sjdbh:“025111111111”}),p=shortestpath((x)-[*..10]-(y))RETURN p
      1. 查询不存在某个属性的节点
        match(x:电话) where x.repeat is null with x match p=(x)-[r*1..5]-(y) return p
      1. 查询存在关联到某一个节点具有相同属性的其他的节点
        match p=(x)-[]-()-[]-(y) where x.name=y.name return p
      1. 查询两个节点之间存在多个关系的节点
        match p=(x)-[r1]-(y)-[r2]-(x) where id(r1)<>id(r2) return p
      1. 查询某一个节点具有m到n层关系的所有的节点
        match q=(x:学生)-[*1..5]-() where x.no =‘201921011XXXX’ return q #查询某学生有关系的节点,最多五层关系
      1. 查询标签以son结尾的所有节点
        match (n) where ANY(l in labels(n) WHERE l =~ “.*son”) return n
      1. 查询关系以R_开头的1-6层关系
        match p=(a:P_Person)-[r*1..6]->(b) where a.identityCard=‘433130199406230031’ and all(l in r where type(l)=~‘R_.*’) return p
      1. [*…10]表示查询路径长度10以内的关系同时返回最短路径长度:
        MATCH (p1:Person {name:“aaaaaaa”}),(p2:Person{name:“bbbbbb”}),
        p=shortestpath((p1)-[*..10]->(p2))
        RETURN p,length§
      1. 查询两个节点间是否存在关系
        match (a:P_Person), (b:P_Person) where not(a)-[:R_relatives_Children]-(b) return a,b

四、检查集合元素

all()表示所有的元素都满足条件,any()表示至少一个元素满足条件,none()函数表示没有一个元素满足条件,single()表示只有一个元素满足条件

例如,ALL谓词表示,在路径中,所有节点都必须具有age属性,并且age属性值都必须大于30:

MATCH p =(a)-[*1..3]->(b)
WHERE a.name = 'Alice' AND b.name = 'Daniel' AND ALL (x IN nodes(p) WHERE x.age > 30)
RETURN p

ANY谓词表示,节点的array属性中至少有一个元素值是one:

MATCH (a)
WHERE a.name = 'Eskil' AND ANY (x IN a.array WHERE x = 'one')
RETURN a.name, a.array

NONE谓词表示,在路径中,没有节点的age属性等于25

MATCH p =(n)-[*1..3]->(b)
WHERE n.name = 'Alice' AND NONE (x IN nodes(p) WHERE x.age = 25)
RETURN p

SINGLE谓词表示,在路径中,只有一个节点的eyes属性是blue:

MATCH p =(n)-->(b)
WHERE n.name = 'Alice' AND SINGLE (var IN nodes(p) WHERE var.eyes = 'blue')
RETURN p

批量删除节点

match(n:Node) with n limit 100000 DELETE n;

批量删除关系

match ()-[r:Type]-() with r limit 100000 DELETE r;

统计节点数量

MATCH (n:P_Person) 
RETURN count(n)

五、在线批量导入数据

load csv命令详解

  • 1、load csv 在配置文件中由两个参数控制dbms.security.allow_csv_import_from_file_urlsdbms.directories.import
    dbms.security.allow_csv_import_from_file_urls是否允许文件导入,默认为true
    dbms.directories.import是本地导入csv文件目录,默认值为import目录,可以更改目录

  • 2、csv文件必须是UTF-8,如果由xlsx文件另存,请选择CSV-UTF-8,逗号分离

  • 3、不带标题的CSV文件导入
    load csv from "file:///ci2_withoutheader.csv" as line CREATE (:Ci {title: line[1], content: line[2], author: line[3], dynasty: line[4]})
    远程导入csv文件
    load csv from "http://www.baidu.com/ci2_withoutheader.csv" as line CREATE (:Ci {title: line[1], content: line[2], author: line[3], dynasty: line[4]})

  • 4、默认每1000行分批导入
    :AUTO USING PERIODIC COMMIT 5000 LOAD CSV 'file:///ci_withoutheader.csv' as line CREATE (:Ci {title: line[1], content: line[2], author: line[3], dynasty: line[4]})
    注意:官方文档有些问题,必须在命令前加上:AUTO否则会报错

  • 5、带标题的CSV文件导入
    LOAD CSV WITH HEADERS FROM 'file:///poem.csv' as line with line create (:Poem{title: line.title, content: line.content, author: line.author, dynasty: line.dynasty})

  • 6、创建索引,再进行导入
    创建索引:create index on :Attacker(id)
    创建唯一索引:create constraint on (n:ITEM) ASSERT n.itemid is unique
    导入数据:using periodic commit 1000 load csv from 'file:///attacker.csv' as line merge (attack:Attacker2{id:toInteger(line[0])}) on create set attack.ip=line[1],attack.country=line[2],attack.province=line[3],attack.city=line[4];

apoc导入

  • 1、安装apoc插件
      将mysql-connector-java-5.1.21.jar和apoc-3.4.0.3-all.jar放到plugins目录下即可。其中mysql-connector是用来进行数据初始化的,即把MySQL数据库中的数据导入到Neo4j中。
  • 2、修改配置文件,conf/neo4j.conf
dbms.security.procedures.unrestricted=apoc.*
apoc.import.file.enabled=true
apoc.export.file.enabled=true
  • 3、在浏览器上localhost:7474/browser中运行
return apoc.version()
有版本号则说明成功
  • 4、导入数据到neo4j
# 插入数据到neo4j
CALL apoc.periodic.iterate("CALL apoc.load.jdbc('jdbc:mysql://172.35.1.202:3306/zkbh-bigdata-cdjw?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&user=root&password=123456',\"SELECT IFNULL(`spouse_identity_card`, '') as spouse_identity_card,IFNULL(`father_identity_card`, '') as father_identity_card,IFNULL(`mother_identity_card`, '') as mother_identity_card,IFNULL(`political_status`, '') as political_status,IFNULL(`identity_card`, '') as identity_card,IFNULL(`sex`, '') as sex,IFNULL(`address`, '') as address,IFNULL(`name`, '') as name,IFNULL(`id`, '') as id FROM sta_gaj_population WHERE 1 = 1  and !ISNULL(identity_card) and identity_card != '' limit 1300000, 100000 \")","MERGE(n:P_Person{spouse_identity_card: row.spouse_identity_card,father_identity_card: row.father_identity_card,mother_identity_card: row.mother_identity_card,political_status: row.political_status,identity_card: row.identity_card,sex: row.sex,address: row.address,name: row.name})",{batchSize:50000,iterateList:true})

neo4j配置文件优化

在conf文件夹下的neo4j.conf文件中如下配置

# 说明:官方建议堆内存不要过大,过大容易产生full gc,
# 导致程序停顿。在总物理内存足够的情况下(>56G),官方的建议值是:16G。
dbms.memory.heap.initial_size=16384m
dbms.memory.heap.max_size=16384m
# 页缓存(pagecache)
# 在neo4j中,"页面缓存”用来缓存索引和数据。通过该参数的设置,
# 可以把数据都加载到内存中。该参数的大小需要根据数据量和索引量,
# 以及物理内存的大小来设置。
dbms.memory.pagecache.size=80g

设置开机自启动

1.启动关闭脚本

vi start.sh

#!/bin/bash

export JAVA_HOME=/opt/jdk1.8.0_161
export PATH=$JAVA_HOME/bin:$PATH

sh /opt/neo4j-enterprise-3.5.3/bin/neo4j start

vi stop.sh

#!/bin/bash

export JAVA_HOME=/opt/jdk1.8.0_161
export PATH=$JAVA_HOME/bin:$PATH

sh /opt/neo4j-enterprise-3.5.3/bin/neo4j stop

2.开机启动脚本服务neo4j.servic

cd /usr/lib/systemd/system #服务目录
vi neo4j.service

[Unit]
Description=neo4j #描述
After=network.target remote-fs.target nss-lookup.target #执行前验证网络等服务是否正常

[Service]
Type=forking #开启后台进程
ExecStart=/opt/neo4j/bin/start.sh #启动脚本
ExecStop=/opt/neo4j/bin/stop.sh  #停止脚本
PrivateTpm=true #临时缓存文件开启默认缓存到/tmp下

[Install]
WantedBy=multi-user.target

注意,这里可能由于创建的文件没有权限,所有需要对start.sh、stop.sh以及neo4j.service赋予权限

chmod 777 start.sh
chmod 777 stop.sh
chmod 777 neo4j.service

3.开启服务

systemctl enable neo4j.service

4.验证

reboot
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值