知识图谱环境搭建——安装Neo4j

一、下载Neo4j社区版4.2.2

https://neo4j.com/download-center/#community
知识图谱环境搭建——安装Neo4j_第1张图片

二、下载JDK 11

https://www.oracle.com/java/technologies/javase-jdk11-downloads.html
知识图谱环境搭建——安装Neo4j_第2张图片

三、运行Neo4j

打开终端,进入Neo4j目录
cd /Users/zmz/Downloads/neo4j-community-4.2.2
执行命令:
bin/neo4j start
知识图谱环境搭建——安装Neo4j_第3张图片
打开浏览器,进入网址

http://127.0.0.1:7474/browser/

知识图谱环境搭建——安装Neo4j_第4张图片
默认用户名密码均为neo4j,第一次登陆修改密码后即可进入
知识图谱环境搭建——安装Neo4j_第5张图片

四、使用Neo4j

1、创建一个人物节点:在最上方的命令处输入

CREATE (n:Person {name:'John'}) RETURN n

知识图谱环境搭建——安装Neo4j_第6张图片
然后创建更多人物节点

CREATE (n:Person {name:'Sally'}) RETURN n
CREATE (n:Person {name:'Steve'}) RETURN n
CREATE (n:Person {name:'Mike'}) RETURN n
CREATE (n:Person {name:'Liz'}) RETURN n
CREATE (n:Person {name:'Shawn'}) RETURN n

2、创建地区节点

CREATE (n:Location {city:'Miami', state:'FL'})
CREATE (n:Location {city:'Boston', state:'MA'})
CREATE (n:Location {city:'Lynn', state:'MA'})
CREATE (n:Location {city:'Portland', state:'ME'})
CREATE (n:Location {city:'San Francisco', state:'CA'})

知识图谱环境搭建——安装Neo4j_第7张图片
可以看到有人节点,有地区节点
3、让我们创建一个朋友关系

MATCH (a:Person {name:'Liz'}), 
      (b:Person {name:'Mike'}) 
MERGE (a)-[:FRIENDS]->(b)

关系增加属性

MATCH (a:Person {name:'Shawn'}), 
      (b:Person {name:'Sally'}) 
MERGE (a)-[:FRIENDS {since:2001}]->(b)

增加更多的朋友关系

MATCH (a:Person {name:'Shawn'}), (b:Person {name:'John'}) MERGE (a)-[:FRIENDS {since:2012}]->(b)
MATCH (a:Person {name:'Mike'}), (b:Person {name:'Shawn'}) MERGE (a)-[:FRIENDS {since:2006}]->(b)
MATCH (a:Person {name:'Sally'}), (b:Person {name:'Steve'}) MERGE (a)-[:FRIENDS {since:2006}]->(b)
MATCH (a:Person {name:'Liz'}), (b:Person {name:'John'}) MERGE (a)-[:MARRIED {since:1998}]->(b)

这样,图谱就建立好了
知识图谱环境搭建——安装Neo4j_第8张图片
4、建立不同类型节点之间的关系:人物和地点的关系

MATCH (a:Person {name:'John'}), (b:Location {city:'Boston'}) MERGE (a)-[:BORN_IN {year:1978}]->(b)
MATCH (a:Person {name:'Liz'}), (b:Location {city:'Boston'}) MERGE (a)-[:BORN_IN {year:1981}]->(b)
MATCH (a:Person {name:'Mike'}), (b:Location {city:'San Francisco'}) MERGE (a)-[:BORN_IN {year:1960}]->(b)
MATCH (a:Person {name:'Shawn'}), (b:Location {city:'Miami'}) MERGE (a)-[:BORN_IN {year:1960}]->(b)
MATCH (a:Person {name:'Steve'}), (b:Location {city:'Lynn'}) MERGE (a)-[:BORN_IN {year:1970}]->(b)

也可在创建节点时就建好关系

CREATE (a:Person {name:'Todd'})-[r:FRIENDS]->(b:Person {name:'Carlos'})

知识图谱环境搭建——安装Neo4j_第9张图片

五、图数据库查询

1、查询下所有在Boston出生的人物

MATCH (a:Person)-[:BORN_IN]->(b:Location {city:'Boston'}) RETURN a,b

知识图谱环境搭建——安装Neo4j_第10张图片
2、查询所有对外有关系的节点

MATCH (a)--() RETURN a

知识图谱环境搭建——安装Neo4j_第11张图片

六、删除和修改

1、增加/修改节点的属性

MATCH (a:Person {name:'Liz'}) SET a.age=34
MATCH (a:Person {name:'Shawn'}) SET a.age=32
MATCH (a:Person {name:'John'}) SET a.age=44
MATCH (a:Person {name:'Mike'}) SET a.age=25

2、删除节点的属性

MATCH (a:Person {name:'Mike'}) SET a.test='test'
MATCH (a:Person {name:'Mike'}) REMOVE a.test

七、通过python操作Neo4j

1、 neo4j模块:执行CQL ( cypher ) 语句

  # step 1:导入 Neo4j 驱动包
  from neo4j import GraphDatabase
  # step 2:连接 Neo4j 图数据库
  driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "password"))
  # 添加 关系 函数
  def add_friend(tx, name, friend_name):
      tx.run("MERGE (a:Person {name: $name}) "
            "MERGE (a)-[:KNOWS]->(friend:Person {name: $friend_name})",
            name=name, friend_name=friend_name)
  # 定义 关系函数
  def print_friends(tx, name):
      for record in tx.run("MATCH (a:Person)-[:KNOWS]->(friend) WHERE a.name = $name "
                          "RETURN friend.name ORDER BY friend.name", name=name):
          print(record["friend.name"])
  # step 3:运行
  with driver.session() as session:
      session.write_transaction(add_friend, "Arthur", "Guinevere")
      session.write_transaction(add_friend, "Arthur", "Lancelot")
      session.write_transaction(add_friend, "Arthur", "Merlin")
      session.read_transaction(print_friends, "Arthur")
或者:
neo4j.GraphDatabase.driver(xxxx).session().write_transaction(函数(含tx.run(CQL语句)))
或者:
neo4j.GraphDatabase.driver(xxxx).session().begin_transaction.run(CQL语句)

2、py2neo模块:通过操作python变量,达到操作neo4j的目的

  # step 1:导包
  from py2neo import Graph, Node, Relationship
  # step 2:构建图
  g = Graph()
  # step 3:创建节点
  tx = g.begin()
  a = Node("Person", name="Alice")
  tx.create(a)
  b = Node("Person", name="Bob")
  # step 4:创建边
  ab = Relationship(a, "KNOWS", b)
  # step 5:运行
  tx.create(ab)
  tx.commit()

参考资料

干货 | 从零到一学习知识图谱的技术与应用
手把手教你快速入门知识图谱 - Neo4J教程
python操作图数据库neo4j的两种方式
Neo4j之导入数据
schema 介绍
知识图谱Schema
美团大脑:知识图谱的建模方法及其应用
肖仰华.知识图谱:概念与技术.北京:电子工业出版社,2020.2-39.

你可能感兴趣的:(知识图谱,neo4j,知识图谱,自然语言处理)