COVID-19KG/main.py

51 lines
2.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from py2neo import Graph, Node, Relationship
import csv
# 连接neo4j数据库输入地址、用户名、密码
graph = Graph("bolt://localhost:7687", auth=("neo4j", "463060687"))
graph.delete_all()
# 分别打开四个CSV文件并读取数据
with open('E:/KG/neo4j-community-3.5.28/import/病例地点行程信息文件.CSV', 'r') as f1:
reader1 = csv.reader(f1)
data1 = list(reader1)
with open('E:/KG/neo4j-community-3.5.28/import/病例基本信息文件.CSV', 'r') as f2:
reader2 = csv.reader(f2)
data2 = list(reader2)
with open('E:/KG/neo4j-community-3.5.28/import/时间信息文件.CSV', 'r') as f3:
reader3 = csv.reader(f3)
data3 = list(reader3)
with open('E:/KG/neo4j-community-3.5.28/import/公共交通工具信息文件.CSV', 'r') as f4:
reader4 = csv.reader(f4)
data4 = list(reader4)
# 利用两个FOR_IN循环来获取各个文件中的节点信息
for i in range(1,len(data2)):
person = Node('person', name=data2[i][0], severity=data2[i][1], age=data2[i][2], gender=data2[i][3],
confirmed_time=data2[i][4])
graph.create(person) # 构建人物信息节点
for j in range(0,30):
place=Node('place',name=data1[i][j])
date=Node('date',name=data3[i][j])
transportation=Node('transportation',name=data4[i][j])
graph.create(place) #构建地点信息、日期信息和交通工具信息节点
graph.create(date)
graph.create(transportation)
# 构建四个类型数据之间的关系
graph.create(Relationship(person,'到达过',place))
graph.create(Relationship(person,'',date))
graph.create(Relationship(person,'乘坐',transportation))
graph.create(Relationship(place,'L',date))
graph.create(Relationship(place,'L',transportation))
graph.create(Relationship(transportation,'L',date))
# 下面三句代码分布实现合并三个类型数据的相同节点操作
graph.run(
'MATCH (n:place) WITH n.name AS name, COLLECT(n) AS nodelist, COUNT(*) AS count WHERE count > 1 CALL apoc.refactor.mergeNodes(nodelist) YIELD node RETURN node')
graph.run(
'MATCH (n:date) WITH n.name AS name, COLLECT(n) AS nodelist, COUNT(*) AS count WHERE count > 1 CALL apoc.refactor.mergeNodes(nodelist) YIELD node RETURN node')
graph.run(
'MATCH (n:transportation) WITH n.name AS name, COLLECT(n) AS nodelist, COUNT(*) AS count WHERE count > 1 CALL apoc.refactor.mergeNodes(nodelist) YIELD node RETURN node')
#注意在python运行完这段代码后如果构建的图谱含有空节点需要到neo4j中进行空节点删除操作