# 10. Neo4j 图数据库

# 1. 安装配置

# 1.1. neo4j 社区版

  1. 安装配置 JDK 环境,比如需要 jdk11 以上版本
  2. neo4j 官网🔗 (opens new window)下载地址🔗 (opens new window) 下载安装包
  3. 离线安装包解压即可
  4. 启动数据库
    1. ../neo4j-community-4.0.3/bin安装目录下打开命令行

      neo4j console
      
      1
    2. 正常数据库打开效果

      D:\neo4j-community-4.0.3\bin>neo4j console
      2020-07-02 06:57:01.863+0000 INFO  ======== Neo4j 4.0.3 ========
      2020-07-02 06:57:01.868+0000 INFO  Starting...
      2020-07-02 06:57:10.438+0000 INFO  Bolt enabled on localhost:7687.
      2020-07-02 06:57:10.439+0000 INFO  Started.
      2020-07-02 06:57:11.376+0000 INFO  Remote interface available at http://localhost:7474/
      
      1
      2
      3
      4
      5
      6
    3. 在浏览器中打开 http://localhost:7474/ 即可访问数据库

    4. 默认用户名neo4j,密码neo4j,首次登录会要求修改密码

# 1.2. neo4j desktop 版

  1. 下载安装 Destop 版本,需要注册,并复制激活码。下载地址 (opens new window)

  2. 安装完成后输入激活码,进入界面

    图 0

  3. 创建数据库

    1. 新建项目:点击 New->Create Project 图 1

    2. 新建数据库:Add->Local DBMS 图 2

    3. 输入数据库名,点击 Create 创建数据库 图 3

    4. 点击 Start 启动数据库 图 4

    5. 数据库访问地址和端口号 图 5

# 2. py2neo 使用

# 2.1. 安装

  1. 安装py2neo

    pip install py2neo
    
    1
  2. 连接数据库

    from py2neo import Graph, Node, Relationship
    
    # 连接数据库 连接地址,数据库名,密码
    neo_graph = Graph('http://localhost:7474', name='neo4j', password='password')
    
    # 简写 1
    neo = Graph('localhost', auth=('neo4j', 'password'))
    
    # 简写 2
    neo = Graph('localhost', password='password')
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
  3. demo

    from py2neo import Graph, Node, Relationship
    
    # 连接数据库
    neo_graph = Graph(
    'http://localhost:7474',
    username='neo4j',
    password='password'
    )
    db = neo_graph.begin()
    
    # 建立节点
    a = Node('person', name='Alice')
    b = Node('person', name='Bos')
    # 创建关系
    ab = Relationship(a, 'knows', b)
    # 写入数据库
    db.create(ab)
    
    db.commit()             # 旧版函数
    neo_graph.commit(db)    # 新版函数
    
    print(ab)
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22

    (Alice)-[:knows {}]->(Bos)

  4. cypher 写法

    neo_graph.run('create(p:person{name:"Alice", age:16})')
    neo_graph.begin().commit()
    
    1
    2

# 2.2. 增

  1. 创建节点与属性

    1. py2neo 写法

      # 创建节点
      a = Node('person', name='Alice')
      neo_graph.create(a)
      b = Node('person', name='Bob')
      neo_graph.create(b)
      # 创建关系
      ab = Relationship(a, 'knows', b)
      neo_graph.create(ab)
      # 添加属性
      a['age'] = 20
      b['age'] = 21
      ab['time'] = '2020-07-03'
      
      print(a)
      print(b)
      print(ab)
      
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16

      (:person {age: 20, name: 'Alice'}) (:person {age: 21, name: 'Bos'}) (Alice)-[:knows {time: '2020-07-03'}]->(Bos)

    2. 批量创建节点

      from py2neo import Graph, Node, Relationship, NodeMatcher, Subgraph
      
      node_list = []
      for n in nodes:
          node_list.append(n)
      
      sub_graph = Subgraph(node_list, []) # 如果要同步创建关系,后面的 [] 可以换成关系的 list
      neo_graph.create(sub_graph)
      
      1
      2
      3
      4
      5
      6
      7
      8
  2. 设置默认属性 setdefault

    a.setdefault('location', 'beijing')
    print(a)
    
    1
    2

    (:person {age: 20, location: 'beijing', name: 'Alice'})

  3. 属性批量更新 update

    data = {
    'name': 'amy',
    'age': 21
    }
    
    a.update(data)
    
    print(a)
    
    1
    2
    3
    4
    5
    6
    7
    8

    (:person {age: 21, location: 'beijing', name: 'amy'})

# 2.3. 删

  1. 删除节点

    # 删除节点 2
    neo_graph.delete(neo_graph.nodes[2])
    neo_graph.begin().commit()
    
    1
    2
    3
  2. 删库

    neo_graph.delete_all()
    
    1

# 2.4. 改

  1. 修改节点属性

    # 修改节点名称
    node = neo_graph.nodes[1]
    node['name'] = "Tom"
    neo_graph.push(node)
    
    1
    2
    3
    4

# 2.5. 查

  1. 查询节点

    1. 通过 nodes 的 id 查询

      from py2neo import Graph
      
      neo = Graph(
          'http://localhost:7474',
          username='neo4j',
          password='password'
      )
      
      print(neo_graph.nodes[1])
      print(neo_graph.nodes.get(1))
      
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10

      (_1:person {age: 21, name: 'Bob'})

    2. 通过 Cypher 语言查询

      n = neo_graph.run("MATCH (a:person) RETURN a.name,a.age LIMIT 4").data()
      for i in n:
          print(i)
      
      1
      2
      3

      {'a.name': 'amy', 'a.age': 21} {'a.name': 'Bob', 'a.age': 21}

    3. 通过 NodeMatcher 查询

      matcher = NodeMatcher(neo_graph)
      find = matcher.match('person').where('_.name="Bob"').first()    # 查找第一个匹配项
      # 或
      find = matcher.match('person').where(name="Bob").first()
      print(find)     # (_1:person {age: 21, name: 'Bob'})
      
      # 批量查找某个类型节点
      nodes = matcher.match('person')
      for p in nodes:
          print(p['name'])
      
      # 查询全部节点
      node = neo_graph.nodes.match().all()
      print(len(node))
      
      # 多条件搜索
      nodes = matcher.match('person', age=20) # 只能用等号的
      # 或
      nodes = matcher.match('person').where(age=20)
      # 如果是非确定的条件搜索,用`_. 参数`
      nodes = matcher.match('person').where('_.age > 20')
      
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
  2. 查询关系

    1. 直接查询

      # 查询全部关系
      rs = neo_graph.relationships.match().all()
      print(len(rs))
      # 查询特定关系
      rs = neo_graph.relationships.match(None, r_type='knows')
      print(len(rs))
      
      
      1
      2
      3
      4
      5
      6
      7
    2. 使用 RelationshipMatcher 查询

      # 查询某一类关系
      rm = RelationshipMatcher(neo_graph)
      rs = rm.match(None, r_type='knows').first()
      
      # 查询某个节点的关系
      node = neo_graph.nodes.match('people').first()
      print(node['name'])
      rm = RelationshipMatcher(neo_graph)
      rs = rm.match([node], r_type='knows').first()
      
      1
      2
      3
      4
      5
      6
      7
      8
      9
  3. 查询路径

  4. 查询类型

    # 查询所有节点类型
    print(neo_graph.schema.node_labels)
    # 查询所有关系
    print(neo_graph.schema.relationship_types)
    
    
    1
    2
    3
    4
    5

# 2.6. 参考资料

# 3. Neomodel 使用

# 3.1. 安装

  1. 安装 neomodel

    pip install neomodel
    
    1
  2. 连接数据库

    from neomodel import config
    # 默认用户名密码
    config.DATABASE_URL = 'bolt://neo4j:neo4j@localhost:7687'
    
    1
    2
    3

    from neomodel import db
    db.set_connection('bolt://neo4j:neo4j@localhost:7687')
    
    1
    2

# 3.2. 创建模型

  1. 定义关系和模型,命名为 models.py

    from neomodel import (config, StructuredNode, StringProperty, IntegerProperty,
        UniqueIdProperty, RelationshipTo)
    
    # 节点 1 country
    class Country(StructuredNode):
        code = StringProperty(unique_index=True, required=True)
    
    # 节点 2 person
    class Person(StructuredNode):
        uid = UniqueIdProperty()
        name = StringProperty(unique_index=True)
        age = IntegerProperty(index=True, default=0)
    
        # traverse outgoing IS_FROM relations, inflate to Country objects
        country = RelationshipTo(Country, 'IS_FROM')
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
  2. 关系类型

    关系 方向 备注
    Relationship 双向 双向查询
    RelationshipTo 正向 可以从当前节点查到对象节点
    RelationshipFrom 反向 可以从对象节点查询当前节点

# 3.3. 使用模型

  1. 创建模型

    from neomodel import config
    import models
    
    config.DATABASE_URL = 'bolt://neo4j:password@localhost:7687'
    
    jim = test.Person(name='jim', age=3).save()
    
    1
    2
    3
    4
    5
    6
  2. 查询节点

    # 单一查询(如果有多个会报错)
    jim = models.Person.nodes.get_or_none(name='jim')
    # 查询第一个
    ch = models.Country.nodes.first_or_none(name='China')
    # 多项查询,返回一个对象列表
    ch = models.Country.nodes.filter(name='China')
    # 全部
    all_nodes = Person.ndoes.all()
    
    1
    2
    3
    4
    5
    6
    7
    8
  3. 创建关系

    # 获取节点
    jim = models.Person.nodes.get_or_none(name='jim')
    # 创建节点
    ch = models.Country(name='China').save()
    # 创建节点关系
    jim.country.connect(ch)
    
    1
    2
    3
    4
    5
    6
  4. 更新节点

    # 获取节点
    jim = models.Person.nodes.get_or_none(name='jim')
    # 更新数据
    jim.age = 4
    # 保存更新
    jim.save()
    
    1
    2
    3
    4
    5
    6
  5. 删除

    am = models.Country.nodes.get_or_none(name='American')
    am.delete()
    
    1
    2
  6. 官方文档🔗 (opens new window)

# 4. Neo4j 使用

# 4.1. 安装配置

  1. 安装

    pip install neo4j
    
    1
  2. 连接数据库

from neo4j import GraphDatabase

driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "password"))
1
2
3

# 4.2. 增删改查

# 4.3. 参考资料

# 5. Cypher

# 5.1. 增

  1. 创建节点

# 5.2. 删

# 5.3. 改

# 5.4. 查

  1. 查询孤立节点

    match (n) where not(n)-[]-() return n;
    
    1
  2. 根据属性查询节点

    MATCH (n:people {name:'Bob'}) RETURN n;
    
    1
lastUpdate: 2/28/2024, 4:17:55 PM