windows下neo4j安装及配置,并绘制人物关系图谱
Posted 程序媛一枚~
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了windows下neo4j安装及配置,并绘制人物关系图谱相关的知识,希望对你有一定的参考价值。
neo4j安装及配置,绘制人物关系图谱
- 先升级pip,安装py2neo
pip install py2neo==2021.0.1
- 依赖 jdk1.8, neo4j 3.xx;
或者jdk18,neo4j 4.x,5.x;
官网下载了neo4j4.x,5.x 因为jdk版本原因都不行,最终选择了neo4j3.xx
- 下载:https://neo4j.com/artifact.php?name=neo4j-community-3.5.31-windows.zip
https://neo4j.com/download-center
安装,配置环境变量,启动,浏览器打开
neo4就 5.x,4.x都不行,与java1.8不兼容,最后安了3.5.31 ok,忽略文件夹名称为了不用重复配置环境变量 NEO4j_HOME及path
终于安装好了✌ ✌ ✌
1. 效果图
群山回唱人物关系图谱 效果图如下:
2. 源码
# 生成人物关系图谱
import pandas as pd
from py2neo import Graph, Node, Relationship
# 字段映射字典
mapping_dict = "发生时间": "occurrence_time",
"发布时间": "release_time",
"发生地点": "scene",
"死亡人数": "death_toll",
"受伤人数": "injury_toll",
"直接损失": "direct_loss",
"事故主体单位": "accident_unit",
"灾害事故类型": "accident_type",
"防范与整改": "prevention_rectification",
"处理建议": "treatment_recommendations",
"应急措施": "emergency_measure",
"直接原因": "immediate_cause",
"严重程度": "order_severity",
"应急响应": "emergency_response"
# 读取文件
storage_df = pd.read_csv('files/new.csv', encoding='utf-8')
# 获取列标签
columns_list = storage_df.columns.tolist()
# 获取数据条数
nums = len(storage_df['title'])
# 删除title列标签
columns_list.remove('title')
# 逆转映射字典
new_dic = dict(zip(mapping_dict.values(), mapping_dict.keys()))
# 连接数据库,输入个人配置
graph = Graph("http://localhost:7474/", username='neo4j', password='neo4j123456', run="sub")
# 清空全部数据
graph.delete_all()
# 开启一个新的事务
graph.begin()
for i in range(nums):
data_dict =
title = storage_df['title'][i]
for columns in columns_list:
if str(storage_df[columns][i]) != 'nan':
data_dict[columns] = storage_df[columns][i]
# 创建事件节点(主节点),节点属性
node1 = Node("case", name=title, **data_dict)
graph.create(node1, 'case', 'name')
# 删除id列
data_dict.pop('id')
# 创建关系与副节点
for key, value in data_dict.items():
# 创建副节点
node2 = Node(key, name=value)
graph.create(node2, key, 'name')
# 创建关系
rel = Relationship(node1, new_dic[key], node2, type=key)
print(new_dic[key])
graph.create(rel)
2.1 群山会唱人物关系图谱
# python plot_qshc_rwtp.py
# 对群山回唱人物关系图谱进行绘制
from py2neo import Graph, Node, Relationship
# 连接数据库,输入个人配置
graph = Graph("http://localhost:7474/", username='xx', password='xx', run="sub")
# 清空全部数据
graph.delete_all()
# 开启一个新的事务
graph.begin()
dict_nodes =
with open("qshc.txt", 'r', encoding='utf-8', errors='ignore') as f:
data = f.readlines()
print(data)
for i, line in enumerate(data):
print(i, line.replace("\\n", ""))
node_a = line.split(",")[0]
node_b = line.split(",")[1]
relation = line.split(",")[2]
# 创建副节点
node1 = None
node2 = None
if (dict_nodes.__contains__(node_a)):
node1 = dict_nodes.get(node_a)
else:
node1 = Node(node_a, name=node_a)
graph.create(node1)
dict_nodes[node_a] = node1
if (dict_nodes.__contains__(node_b)):
node2 = dict_nodes.get(node_b)
else:
node2 = Node(node_b, name=node_b)
graph.create(node2)
dict_nodes[node_b] = node2
# 创建关系
rel = Relationship(node1, relation, node2, type='key')
graph.create(rel)
参考
- https://blog.csdn.net/qq_52159080/article/details/124018875
- https://blog.csdn.net/qq_52159080/article/details/124018875
以上是关于windows下neo4j安装及配置,并绘制人物关系图谱的主要内容,如果未能解决你的问题,请参考以下文章
Centos下Docker中运行neo4j 并配置挂载本地文件
2021/8/28-最新教程Windows 安装配置Neo4j图数据库